Am I Writing Conditionals Correctly in My Code?

I always had this question “Am I writing Conditionals Correctly  in My Code?” and I used to ask this question from myself and show my conditions to my teammates sometimes for a feedback of improvement. I remembered once our old teach lead, Kosala recommended me to read “Code Complete 2 by Steve McConnell ” in order to learn better coding. Thank you Kosala for recommending me this great book. I started reading it few days ago.

Steve McConnell  has some guidelines to follow when it comes to conditionals (if conditions and case statements) in his book. Those set of guidelines cleared some of the questions I had and I am about to go and refactor my recent code.

Here are my notes taken from “Code Complete 2 by Steve McConnell “

If-then-else

  • Write the nominal path through the code first and then write the unusual cases
  • Make sure that you branch correctly on equality – off-by-one error (using < or <= and > or =>)
  • Put the normal case after the if rather than after the else
  • Follow the if clause with a meaningful statement – inverted condition if the if condition is null
  • Consider the else clause –  have else case and put a comment
  • Test the else clause for correctness
  • Check for reversal of the if and else clauses
  • Simplify complicated tests with boolean function calls
  • Put the most common cases first
  • Make sure that all cases are covered – Code a final else clause with an error message or assertion to catch cases you didn’t plan for, for the user
  • Replace if-then-else chains with other constructs if your language supports them

Case Statements

  • case Statements – use whenever available, Some languages does not support switch-case, here is how it is written in javascript
switch(expression) {

case n:

code block

break;

case n:

code block

break;

default:

default code block

}
  • Choosing the Most Effective Ordering of Cases
  • Order cases alphabetically or numerically
  • Put the normal case first – indicate the normal case and unusuals in comments
  • Order cases by frequency – Put the most frequently executed cases first and the least frequently executed last. (quick search for human reader)

Tips for Using case Statements

  • Keep the actions of each case simple – write a routine and call the routine from the case
  • Don’t make up phony variables in order to be able to use the case statement –  A case statement should be used for simple data that’s easily categorized, if not use if-then-else
  • Use the default clause only to detect legitimate defaults –  dont put remaining case into default
  • Use the default clause to detect errors
  • Break the cases as required

 

Advertisements

Author: Harshani Nawarathna

Sri Lankan woman in Computing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s