2007-01-18 15:25:37 if/else function vs readability
style #1. Easier to read, but inefficient code:
( javascript )
  1  if (isNaN( cost )) {
2 cost_element.value = 0
3 cost = 0
4 }
5 if (isNaN( number )) {
6 number_element.value = 0
7 number = 0
8 }
9 profileCost[id] += cost * number
10 profileNumber[id] += number


style #2. Better code-wise, but easy enough to read at 1st glance?
( javascript )
  1  if (isNaN( number ))
2 number_element.value = 0
3 else {
4 if (isNaN( cost ))
5 cost_element.value = 0
6 else
7 profileCost[id] += cost * number
8
9 profileNumber[id] += number
10 }
Leave a comment