Alternative Control Structures
- by Brock Woolf
I've been wondering about alternative ways to write control structures.
One that you learn early on for if statements is a replacement for this:
if ( x ) {
// true
} else {
// false
}
with this (sometimes this is more readable compared to lots of brackets):
x ? true : false
It got me thinking. Can we replace anything else incase it's more readable.
We of course replace if statements like this:
if (a < b) {
return true;
} else {
return false;
}
with things like this:
return a < b;
We can save a long if with something like this (pretty much same as the one above):
bool xCollisionTrue = (object.xPos < aabb.maxX && object.xPos > aabb.minX);
So those are the ones I can think of off the top of my head for the if statement and doing comparisons. So I'm wondering what about looping constructs, for, while, etc.
Maybe the code obfuscators might have some ideas.