That moment.

  • 4
    A classmate almost failed his test because of this. He forgot the else and with it, the code would have worked. :)
  • 1
    @filthyranter Now, does that make him stupid is the question! 😂
  • 1
  • 0
  • 2
    I NEVER use else, only modifying & guard ifs with early returns. I find it makes code easier to understand, and often prevents you from executing unnecessary code.

    How? You first do your safety checks and return special values. If you still feel like you need else, maybe swap a bool using not. And finally, if you STILL need else, your method is probably too long and has too many responsibilities.

    I only make exceptions for simple ternaries.
  • 2
    @bittersweet I totally agree! If you need some fast branch prediction you can always use PGO ;)
  • 2
    @bittersweet this is how it should be done

    Also TryXY() methods with out parameters for things that can fail, but aren't critical to fail
Add Comment