Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Agreed.

Some other heuristics:

* Every if statement is a chance of a bug because the code has two or more paths to follow. Keep the choice making at the business/requirements level of the code, not hidden inside lower level decomposition.

* A switch statement that is not exhaustive (ie covers all possible values) is a change of a bug, especially if there is no default case.

Modern languages with better type systems make the second point less relevant because they require exhaustive pattern matching.



Every if statement is a chance of a bug because the code has two or more paths to follow

This is known as the cyclomatic complexity of a program: https://en.wikipedia.org/wiki/Cyclomatic_complexity

A corollary to this is that it is also beneficial to converge separate paths as quickly as possible (e.g. using non-nullable types and default values) or converge them all to the same place (e.g. nonlocal exception handling).


I often abbreviate that to "Psychosomatic Complexity" because more complex code is likely to give the programmer a headache.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: