Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to handle problems/errors in algorithms

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?

like image 309
Vince Avatar asked Oct 21 '22 10:10

Vince


1 Answers

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?

like image 197
npinti Avatar answered Oct 23 '22 10:10

npinti