03 July 2017

A single expression

While I'm on the topic of code and code clarity, I should mention logical expressions. I often see complicated expressions with multiple variables and'd or or'd together. This isn't very helpful or clear. Each of these should be extracted to a method with a clear and simple name. You want to do this because it brings clarity to the code. It also results in several small, re-useable methods. 

You should apply this technique everywhere, not just IF statements, but in loops as well. Ideally, each of these extracted methods has only one logical test within it. Also, the logic should be as simple as possible. Avoid negatives and especially double negatives. Each condition should be as simple and clear as possible so that the reader doesn't need to put forth much effort.