0

Hey I just came across an function and I was confused as which one is better?

private static bool IsEqual(TypeABC output, TypeABC input)
{
    if( conditions....)
    {
       return true;
    }

    return false;
}

or

private static bool IsEqual(TypeABC output, TypeABC input)
{
    bool isEqual = false;

    if( conditions....)
    {
       isEqual = true;
    }

    return isEqual;
}

Both perform the same thing but

I want to know from from any perspective may it be coding standards, performance or garbage collection. Which one of the above is better and why?

Or if its too generic to answer, then advantages / disadvantages of each code over the other.

Also suggest a few tags I should put for this question.

Pratyush Dhanuka
  • 1,405
  • 2
  • 11
  • 21
  • _"which is better"_ - that's not the approach to take for such problems. List the pros and cons of both methods, and decide which pros and cons you find important. Then you can select which one is "better" _for you_. Keywords to do more research: [single entry, single exit](http://stackoverflow.com/questions/4838828/why-should-a-function-have-only-one-exit-point) and [fail fast](http://stackoverflow.com/questions/10296961/using-fail-fast-approach-when-developing-modular-applications), see also [C# return variables](http://programmers.stackexchange.com/questions/141711/c-return-variables). – CodeCaster Mar 08 '16 at 09:00
  • I think *single entry, single exit* is legacy from procedural programming language like Pascal, where it's hard to control when to return the result in a function/method. I found some Pascal functions here https://en.wikibooks.org/wiki/Pascal_Programming/Syntax_and_functions and it's weird (IMO) because there is no ***return*** keyword to stop the function. – tdat00 Mar 08 '16 at 09:13

1 Answers1

1

This is an extremely useless question in terms of performance or garbage collection because you're making a HUGE assumption that the C# compiler team didn't just recompile your code into the IL version of:

private static bool IsEqual(TypeABC output, TypeABC input)
{
  return ( conditions....);
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Erik, your code is best optimized one of all 3, and yes I was confused as to how the code will be optimized in both, is there somewhere we can see the IL version of a code ? – Pratyush Dhanuka Mar 08 '16 at 09:09
  • There is no point. Your time trying to super optimize this is gaining almost literally nothing in return. As a developer there are much larger problems we should be dealing with in c#. – Erik Philips Mar 08 '16 at 09:10
  • I understand what you are saying, but I would still want to try optimize my code to the best of my ability before leaving it for the compiler and I do not think its pointless as I don't believe in working with no knowledge of whats happening behind the scenes. Anyway thanks because I was able to find both the answer and the reason behind it – Pratyush Dhanuka Mar 08 '16 at 09:19
  • [Compilers - What Every Programmer Should Know About Compiler Optimizations](https://msdn.microsoft.com/en-us/magazine/dn904673.aspx). *Rather than manually optimizing code, you should consider aspects of your design, such as using faster algorithms, incorporating thread-level parallelism and using framework-specific features (such as using move constructors). Still, instead of spending time manually tweaking a program, it’s usually much more fruitful to use specific features provided by the compiler and let the compiler tweak the code.* – Erik Philips Mar 08 '16 at 09:20
  • .Net 4.5.1 Runtime can do some pretty crazy stuff... *For example, at run time, RyuJIT might be able to determine that the condition of an if statement is never true in this particular run of the application and, therefore, it can be optimized away.* – Erik Philips Mar 08 '16 at 09:23
  • nice stuff, Thanks Erik :) . – Pratyush Dhanuka Mar 08 '16 at 09:38