Daily DRY tip

September 17, 2008 at 7:30 pm Leave a comment

Hello, back again with the daily, erm, monthly DRY tip.

Again, a short piece of C++ code.

if( bBool )
  a = some_constant ;
else
  b = some_constant ;

Auwch, such repeatingness is almost unbearable to even glance at.
To the rescue, our dear friend the ternary operator.

( bBool ? a : b ) = some_constant ;

The brackets are there so the ternary operator does not get confused and tries to evaluate b = some_constant as one of the parts of the ternary expression.

Advertisements

Entry filed under: Command Line.

Why? You heard it here first

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Categories


%d bloggers like this: