3

So I've been programming for a few years mostly as an amateur/student, and I'm aware of the fact that using exceptions is generally frowned upon when used as a lack of consideration for input and just a catch-all (e.g., lazy programming). But I have a specific question regarding a programming interview question:

Given a linked list, find if there is a cycle in it.

The solution being to use two pointers, iterate the first by 1 and the second by 2 positions, and if they ever meet then there is a cycle. If you get a null pointer then there is no cycle.

In the context of an interview, if I wrote the following code saying something like:

"I'm aware that this may not be the best use of exceptions, but since a NullPointerException specifically is the exit condition, it seems the best way to just leverage the language in order to get what I want in an expeditious way. In this way I'm leveraging exceptions in order to check null input, as well as for the exit condition of reaching the end of the list."

Would this be acceptable? Or would this be considered lazy?

boolean hasCycle(Node head) {

try{
    Node start = head;
    Node runner = head;
    do{
        start = start.next;
        runner = runner.next.next;
        if(start == runner) {
            return true;
        }

    }while(runner != start)   ;
}
catch(NullPointerException ex){
    return false;
}

return false;
}
6
  • 5
    You are using exceptions as flow control here, see: programmers.stackexchange.com/questions/189222/…
    – Caleth
    Sep 30, 2016 at 8:53
  • On the other hand, in this very specific case, it works and isn't particularly unclear. Wouldn't be my personal first choice. It's certainly lazy, but then it just comes down to how much laziness you find acceptable, as well as the weight you want to put on philosophical correctness. You ought to just do it the "right" way though, so that you can distinguish between actual bugs and desires behavior.
    – Jason C
    Sep 30, 2016 at 8:59
  • 1
    I'd consider this a prime example for why the "Don't use exceptions for control flow" guideline exists. Sep 30, 2016 at 9:00
  • Eh, there's probably better examples with a clearer downside. This one is too straightforward to really nail a negative consequence home.
    – Jason C
    Sep 30, 2016 at 9:02
  • 1
    The negative consequence is that in many languages, exceptions have a significant impact on performance, and using them for flow control is a great way to build a very slow system.
    – Phoshi
    Sep 30, 2016 at 11:09

1 Answer 1

8

I would consider this lazy.

If I'm the person maintaining your code when I see an Exception, I expect that something exceptional has happened. I then look for what this exceptional case is and how the code deals with it. At this point I'd realise this is not exceptional and just regular code flow, so my thought process has to backtrack and re-review the code in the light of this new information. This all makes the code slower for me to understand, and if I'm just skimming through it, I may miss this entirely.

All of that can be avoided if your loop condition explicitly checks for the null condition.

Not the answer you're looking for? Browse other questions tagged or ask your own question.