Thread: which way you prefer

  1. #1
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Wasn't it discussed not long ago?

    Code:
    void somefunc() 
    {
    	if (something)
    	{
    		return;
    	}
    	
    	/*
    	do
    	a 
    	lot
    	of
    	things
    	*/
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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,366
    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, 06: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, 04: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, 10:42 PM
  4. what do you prefer
    By whistlenm1 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-28-2002, 09:27 PM