Elegant Code pt 2 of 2(jordansebastian.com)

submitted by jsebastjsebast(30) 3 years, 4 months ago

The second installment of my guide to basic code elegance.

1 comment |category: |Views: 10

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by Tr3vTr3v(376) 3 years, 4 months ago 0

You could improve this further by replacing your second version:

int something(int goofy, int mickey)
{
bool isBigger = (goofy > mickey);
if(isBigger)
return goofy;
return mickey;
}

with this:

int something(int goofy, int mickey)
{
if(goofy > mickey)
return goofy;
return mickey;
}

As there is no point caching the result of the goofy > mikey test if you are not going to use that result again within the method.

You could go one step further and use the ternary operator and write the method like this:

int something(int goofy, int mickey)
{
return (goofy > mickey) ? goofy : mickey;
}

Reply

information Login or create an account to comment on this story