Code Smells: If Statements


:star: :star:

Notes

  • If statements can go wild!
  • Steps to refactor:
    • Place guard conditions at the start. Can go with @NotNull
    • Remove logic for controlling iteration. Use “normal” syntax man
    • Extract method for readability. Give it an expressive name
    • Replace multiple checks of the same value, try to short circuit
    • Collapse multiple conditionals into one if/else
    • Nice
  • Differences between good and bad conditionals:
    • More else statements to make clearer distinction between code paths
    • Move early-exit code as close to the top as possible
    • Extract methods to encapsulate conditions