推荐的方法来处理算法中的问题/错误 [英] Recommended way to handle problems/errors in algorithms

查看:135
本文介绍了推荐的方法来处理算法中的问题/错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保持堆栈跟踪,让我们说,'错误'的想法是一个你不想发生的问题,但是这样做。



如果我是使用一个布尔系统来检查操作是否成功完成,它看起来像这样:

  String [] array = new String [10]; 
int i = 0;

public boolean accessValue(int id){
if(id // do something
return true;
}
返回false;
}

while(true){
if(!accessValue(i ++)){
//倾向于情况
}
}

如果我使用例外,它将如下所示:

  class InvalidAccessException extends Throwable {

}

public boolean accessValue(int id)throws InvalidAccessException {
if(!id throw new InvalidAccessException();

//做某事
}

while(true){
try {
accessValue(i ++);
} catch(InvalidAccessException e){
//趋向于
}
}

对我来说唯一重要的是当发生问题时,我以某种方式通知,我将有一个选项来处理这种情况。哪种方式更实践?这是否取决于情况,还是有原因选择一个?

解决方案

你提到的第一种方法,更多的是 C ,其中您有函数产生各种整数,以表示函数在执行过程中如何公平。



虽然这个工作它(在我看来)引入了额外的问题,开发人员需要通过其他文档或其他开发人员代码来理解为什么返回特定值。



在Java中,据我所知,出现错误的时候总是会抛出异常(即使你希望它出错)。使用异常的明显优点是代码更易于阅读,只要看到您的方法签名即可知道您的方法可能导致什么潜在问题。这将允许我更快地编码,因为我不需要挖掘自己的文档或代码只是来查看你的方法的行为(尽管我可能需要挖掘文档/代码看看我是否可以找到一个解决方案,为什么是你的代码抛出异常)。



另外,由于Java没有一个元组的实现返回您需要创建自己的错误代码和值,这些错误代码和值可能会影响代码的可用性和可读性,这在我看来总是应该避免的。



编辑:


如果我的意图不是回到我的代码,找到
错误被抛出来修复它,该怎么办?只是想通知一个错误
发生了,在某种程度上,我可以以某种方式轻松处理这种情况。
而不是我进入代码并手动修复它,我想要
能够触发另一组代码(如handleError()方法),
它有一个算法将甚至事情出来。 (可以选择算法
)。是否应用异常处理可以让您处理 b
$ b $ p

em>特殊事件,所以在你的代码中,你可以这样做:

  while(true){
try {
accessValue(i ++);
} catch(InvalidAccessException e){
//在这里执行算法
}
}

正如您所说,堆栈跟踪有助于您调试问题,因为它提供了在程序崩溃时调用哪些方法的信息。就是说,它们不是使用异常的唯一好处(如上所述)。



使用返回值看到的另一个潜在问题是当不同的开发人员在相同的功能。所以你可以有一个开发人员设计的东西:

  function int doSomething()
{
//如果它工作,产生0
//否则产生1到10之间的数字
}

然后另一位开发人员认为,错误应该有负数并扩展上述方法,

 函数int doSomething()
{
//如果它有效,则生成0
// else产生一个1到10之间的值
//其他一些问题,返回-1
}

上面的内容将意味着您需要通过调用 doSomething(),看到他们现在处理返回值为负数的情况。这个 很麻烦,而且也容易出错。



编辑2:



我希望我得到你的观点。返回true / false时,我看到这个问题:
假设:

  public boolean foo(arg1,arg2) 
{
if(arg1无效)return false;
if(arg2无效)return false;
}

在上面的例子中, false 是什么意思?这是否意味着arg1无效或arg2?如果您需要为不同的参数有效性触发不同的算法呢?


Keeping stacktrace out of it, lets say that the idea of 'error' is a problem that you didn't want to occur, but did.

If I were to use a boolean system to check if the action successfully completed, it would look something like this:

String[] array = new String[10];
int i = 0;

public boolean accessValue(int id) {
     if(id < array.length) {
          //do something
          return true;
     }
     return false;
}

while(true) {
     if(!accessValue(i++)) {
          //tend to situation
     }
}

If I were to use Exceptions, it would look like this:

class InvalidAccessException extends Throwable {

}

public boolean accessValue(int id) throws InvalidAccessException {
     if(!id < array.length || !id >= 0) 
          throw new InvalidAccessException();

     //do something
}

while(true) {
     try {
          accessValue(i++);
     }catch(InvalidAccessException e) {
          //tend to situation
     }
}

The only thing that matters to me is that when a problem occurs, I'm notified in some way, and I will have an option to handle the situation. Which way is more practiced? Does it just depend on the situation, or are there reasons for picking one over the other?

解决方案

The first approach you mention, is more C oriented, in which you have functions yielding various integers to denote how did the function fair during its execution.

Although this worked it (in my opinion) introduced extra problems where the developer would need to go through other documentation or other developer code to understand why was the particular value returned.

In Java, as far as I know the way to go is always to throw exceptions when something goes wrong (even when you expect it to go wrong). The obvious advantage of using exceptions is that the code is more readable, just by seeing your method signature I know what potential issues could your method cause. This would allow me to code quicker since I do not need to dig through your own documentation or code just to see what the behaviour of your method is (although I could potentially need to dig through documentation/code to see if I can find a solution to why is your code throwing exceptions).

Also, since Java does not have an implementation of a tuple to return error codes and values you would need to create your own which could affect code re usability and readability, which in my opinion is always something you should avoid.

EDIT:

What if my intention isn't to go back into my code and find where the error was thrown to fix it. just wanted to be notified that an error happened, in a way I can easily handle the situation in some way. Rather than me going into the code and fixing it manually, I want to be able to trigger another set of code (like a handleError() method), which has an algorithm that will even things out. (whichever algorithm I may choose). Will handling with Exceptions give me any advantage in this case?

Yes it should since exception handling will allow you handle exceptional events, so in your code, you could have this:

while(true) {
     try {
          accessValue(i++);
     }catch(InvalidAccessException e) {
          //Execute algorithms here
     }
}

Having a stack trace is helpful when, as you are saying, you are debugging a problem since it provides information of which methods where called when your program crashed. That being said, they are not the only benefit of using exceptions (as mentioned above).

Another potential problem I see with using return values is when different developers work on the same function. So you could have something like so designed by one developer:

function int doSomething()
{
     //if it worked, yield 0
     //else yield some number between 1 and 10
}

Then another developer comes along which believes that errors should have negative numbers and extends the above method,

function int doSomething()
{
     //if it worked, yield 0
     //else yield some number between 1 and 10
     //something else went wrong, return -1
} 

The above would mean that you would need to go through all other functions calling doSomething() and see that they now handle the case where the return value is negative. This is cumbersome and is also error prone.

EDIT 2:

I hope I am getting your point. I see this issue when you return true/false: Assume this:

public boolean foo(arg1, arg2)
{
     if(arg1 is invalid) return false;
     if(arg2 is invalid) return false;
}

In the above example, what does false mean? Does it mean arg1 is invalid or arg2? What if you need to trigger different algorithms for different parameter validity?

这篇关于推荐的方法来处理算法中的问题/错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆