0

This is a question I've been facing for a while in my college class that I've been thinking about, and I need to get other people's point of views. I searched and couldn't find another question very similar to this one in terms of coding practices.

(Examples are written in Java)
When I write my code, I will generally write methods like this:

public void myMethod(int one, int two) {
    if (one >= two) return;
    // do things

    if (two != one) return;
    // do other things
}

Instead of writing methods like this:

public void myMethod(int one, int two) {
    if (one < two) {
        // do things
        if (two != one) {
            // do other things
        }
    }
}

Similarly, I will write my loops like this:

for (int i = 0; i < x.length; i++) {
    if (x[i].getValue() > 4) continue;
    // do things

    if (!xConditionTwo) continue;
    // do other things
}

Instead of like this:

for (int i = 0; i < x.length; i++) {
    if (x[i].getValue() <= 4) {
        // do things

        if (xConditionTwo) {
            // do other things
        }
    }
}

As seen here, I look at things sequentially (from top line to bottom line), and it makes sense to me to exit out of the method/loop if that something is not what we're looking for, and we don't require an else statement.

However, when I spoke to my college instructors, they all agreed that if somebody were to write code like this, they would not be hired because said person cannot figure out an algorithm that wouldn't require the use of a continue/return.

My question to everyone is: When is it acceptable to use continue/return to break like this, if at all? Why is this considered bad practice, and how can I avoid using if statements without an else to avoid extra indentation?

FireController1847
  • 663
  • 1
  • 7
  • 20
  • have you tried googling this [w3schools](https://www.w3schools.com/js/js_break.asp), [codeburst](https://codeburst.io/javascript-continue-vs-break-47b5c15cacc6), [tutorialspoint](https://www.tutorialspoint.com/javascript/javascript_loop_control), or [stackoverflow](https://stackoverflow.com/questions/11728757/why-are-continue-statements-bad-in-javascript) – depperm Jul 09 '19 at 18:59
  • I understand the difference between continue & break, and the first three links don't give an objective overview as to why this is bad practice. However, the fourth link looks promising, I'll take a look. EDIT: The fourth link does not detail return statements in methods. That may be a mute point to some however I feel the application is majorly different between loops & methods. – FireController1847 Jul 09 '19 at 19:01

1 Answers1

1

This is going to be a question mostly dealing with opinions, but for both of your examples I'd use the continue/return in the first line like you have it, but use an if statement for the other.

public void myMethod(int one, int two) {
    if (two >= one) {
       return;
    }
    // do things
    if (two != one) {
        // do other things
    }
}

To me, this reads better and limits the nested if statements

Jake Luby
  • 1,550
  • 3
  • 14