BigBug
BigBug

Reputation: 6290

Different use cases of return statement

Recently, I can across some code that made me a little confused:

I was always under the impression that doing return this way was bad practice. For one, there is no way to know if something went wrong other than looking in the logs. I asked around a bit and the general notion seems to be that throwing an exception would lead to bad code because people may catch the exception and do nothing with it. Is this really a good reason to not throw an exception? If so, is there any other way that this can be handled?

public void checkThisOut() {
   String str = null; 
   str = someMethodThatCanReturnNull(); 
   if(str == null) {
      //log error
      return;
   }
}

Another example would be the following: What is the difference between break and return in this context? Is one preferable over the other?

public void update() {
    List<Object> objects = SomeClass.findObjects();
    for( Object o : objects ) {
        if( o.getSomething().equals("some value" ) ) {
            o.doSomething();
            return;
        }
    }
}

Upvotes: 0

Views: 97

Answers (5)

GhostCat
GhostCat

Reputation: 140543

When code throws an exception, the caller has the choice how to react. If errors are just logged but not propagated to the caller - then the caller does not even know about the error.

Therefore that argument to not throw is nonsense. When you are afraid that other people write bad code - then you educate them to do better. You don't start writing bad code yourself. But when throwing an exception properly documented why and when that happens.

For the second case: no difference in resulting behavior. So style only - do what your team does normally.

Upvotes: 2

QuentinC
QuentinC

Reputation: 14772

For your second question: break and return don't make any difference here because there is nothing after the for loop. It would make a difference if you add code after the for loop, in which case return exits the method completely, while break jumps at the end of the for loop.

None is better than the other, it depends on what you intent to do. Return is probably clearer and quickier (simpler to understand), but many people think that a method should only have a single return at its end and having two or more is bad practice (because it makes the code harder to understand)

Think what would seem the most logical to you if you had to return a value in case the object you are searching isn't found, like indexOf which returns -1: store the found item in a variable, break the loop and have a single return at the end, or have two return statements in the method. Both are correct ways to go.

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83557

throwing an exception would lead to bad code because people may catch the exception and do nothing with it

I disagree with writing bad code to avoid other bad code. If the null return value is indicates an error, then you should throw an exception.

Upvotes: 1

A1m
A1m

Reputation: 3117

Throwing an exception is not bad code. You just should not use exceptions for regular controlflow.

For your second example, break and return are equal, but I would use return as it does make the intention slightly clearer. If you want to do something after the loop, you can change it to break then.

Upvotes: 1

Kevin Hooke
Kevin Hooke

Reputation: 2621

Throwing an exception with the intent that it is used to control flow (e.g. if(a) then do b, else if exception then do c) in your app is generally considered bad practice.

However in your first example, if you're expecting a result to be not null and you do receive a null, and if this is an unexpected error condition from which you can't continue, then this is a good example of when to throw an exception.

Upvotes: 2

Related Questions