Alternative Control Structures

Posted by Brock Woolf on Stack Overflow See other posts from Stack Overflow or by Brock Woolf
Published on 2010-05-31T16:19:05Z Indexed on 2010/05/31 16:23 UTC
Read the original article Hit count: 371

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about c++