2 ms·
Something more useful I've found with DeMorgan is to flatten nested ifs: if (hasBread) { // do something } Can be flattened to: if (!hasBread)
by AYBABTME 13y ago
Something more useful I've found with DeMorgan is to flatten nested ifs:
if (hasBread) {
// do something
}
Can be flattened to:
if (!hasBread) {
return
}
As you start your function, flush out all the edge cases, invalid conditions and errors, then at every step forward in the function, you know you're always in a state were the odd balls have been taken care of and you deal with the general case.
This has two advantages:
- it keeps the code tidy (the important parts are pretty
much never in a nested block).
- it makes you handle the odd balls explicitely.
I really hate seeing functions such as :
func cookBread() {
if (hasBread) {
do()
a()
bunch()
of()
stuff()
}
}