I get the feeling that a lot of the people on this forum are somewhat new to programming, so I decided to give all of you a tip that no one gave me when I was first learning to program.
Most beginning programmers are unaware of this technique.
Consider the following construct:
if (foo == null) {
bar = 2;
} else {
bar == foo;
}
what you have is a five line construct. There has to be a simpler way to declare this little variable. It's simple, use a tertiary.
those lines above become one line in this way:
bar = foo == null ? 2 : foo;
what happens here, you may ask...
Well, your compiler sees the boolean statement foo == null and does the same thing you would have initially done and turns it into an if (foo == null) and then builds the rest of the if construct for you. Which is my point, why write the 5 lines of code when the compiler will do it for you.
-BigDick
p.s. I know this may insult some of your intelligence, if it does I apologize, but also think that it may provide help to someone as well.




Reply With Quote