which way you prefer

This is a discussion on which way you prefer within the C++ Programming forums, part of the General Programming Boards category; I prefer this way (I dont like too many tabs when not needed): Code: void somefunc() { if (something) return; ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    which way you prefer

    I prefer this way (I dont like too many tabs when not needed):
    Code:
    void somefunc() {
    	if (something) return;
    	/*
    	do
    	a 
    	lot
    	of
    	things
    	*/
    }
    Are there any particular reasons so that I should use this one:
    Code:
    void somefunc() {
    	if (!something) {
    		/*
    		do
    		a 
    		lot
    		of
    		things
    		*/
    	}
    }
    Thanks for help

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Wasn't it discussed not long ago?

    Code:
    void somefunc() 
    {
    	if (something)
    	{
    		return;
    	}
    	
    	/*
    	do
    	a 
    	lot
    	of
    	things
    	*/
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    I seem to remember reading somewhere that most compilers optimize on the assumption that the if condition will be true. This means that you should put the most likely outcome in the if part of the statement, rather than the else.

    Of course, this is severely compiler dependent, but interesting nonetheless.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The only reason I can think of to use the second version is if you aren't using good RAII techniques and returning from the function early could leave dangling resources.

    I generally prefer the first (with different formatting, though). If the return is nested deep and would be hard to see, then I might use the second. You want the return to be easy to see and obvious (which is why I use different formatting).

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I prefer the first since each level of indentation makes it a little harder to understand the code. If the "!something" branch does a lot of work, then even if it's the high-probability branch any efficiency loss from not hitting it first will be negligible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why do you prefer C#?
    By Welder in forum C# Programming
    Replies: 38
    Last Post: 12-06-2007, 05:54 AM
  2. What Font Size do you prefer?
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 03-23-2003, 03:58 AM
  3. Which one will you prefer: cout or printf
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 02-06-2002, 09:42 PM
  4. what do you prefer
    By whistlenm1 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21