-3

Which one is correct and WHY

in both examples we have a function that determines if a certain string is valid...

(using some other function that's not defined here)

private Validator = new Validator();

public Boolean IsValid(String foo)
{
    if (Validator.Validate(foo))
    {
        return true;
    }
    else
    {
        return false;
    }
}

in the second scenario we have a function that ends with a TRUE statement and with no else.

private Validator = new Validator();

public Boolean IsValid(String foo)
{
    if (!Validator.Validate(foo))
    {
        return false;
    }

    return true;
}

NOW INB4 please dont say that you can simply do it this way

return Validator.Validate(foo);

How to save a few lines its not what i want to know...but the implications and unknown consecuences ( to me ) of using one method or the other.

Fex Rex
  • 17
  • 6
  • It's a matter of preference, there's no functional difference. – Jeroen Vannevel Oct 02 '13 at 20:29
  • 5
    But the third way *is* better - it's simpler. They will all achieve the same result, so go for the simplest option. The second version uses more negation, so personally it takes me longer to understand - but it depends. If you had a bunch more validators, it might make sense. It's not really clear what you're unclear about... – Jon Skeet Oct 02 '13 at 20:29
  • The second is better than the first because it can help avoid additional nesting. The third is the best though. :) – telkins Oct 02 '13 at 20:31
  • @trevor-e The second has no less nesting than the first... – Servy Oct 02 '13 at 20:32
  • @Servy I mean that because they are equivalent statements, the second does not contain the `else` block so it is better. I see people unnecessarily nest `if/else` statements all the time when they could all be on the same level. Avoiding unnecessary blocks, like `else` in this case, can help prevent that. – telkins Oct 02 '13 at 20:35
  • @trevor-e But an else isn't nested, it is sequential. Nesting would be `if(a){if(b){doSomething();}}` which isn't happening here. – Servy Oct 02 '13 at 20:37
  • I would say that in the simple case presented then indeed there is no difference. Getting away from the IsValid specifically presented here, if additional processing needed to be done in the different blocks than i think which ever is clearer is the better choice, e.g. for the examples presented 3 is the clearest. If however additional processing needed to be done on the true case, than example 2 might be the clearest – Iddillian Oct 02 '13 at 20:39
  • @Servy I know, this case is not an example. I'm just saying that in general it is best to choose the solution with less logic. I see a lot of beginner programmers always attach an `else` to an `if`, and later on that leads to lots of nesting and ugly code. – telkins Oct 02 '13 at 20:41

2 Answers2

0

Because both IsValid() methods do nothing else they are equivalent.

All 3 are correct. The third is my preference because it's less code and still very readable in this case.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73
-1

I think that the best solution is:

public bool IsValid(String foo)
{
    return (Validator.Validate(foo))? true : false;
}

In addition the conditional expression is easy to understand and it's inline

Tinwor
  • 7,765
  • 6
  • 35
  • 56
  • 1
    This is inferior to simply writing `return Validator.Validate(foo);`. Your code, like the other two examples, adds pointless code that accomplishes nothing but wasting a few processor cycles and (much more importantly) some of the developer's time when reading it. – Servy Oct 02 '13 at 20:50
  • @Servy Totally agree with you on this one. What I was trying to ask is that if the methods I described were totally and ABSOLUTELY equivalent. – Fex Rex Oct 02 '13 at 20:54
  • @FexRex Technically it's possible for the first two to take an extra few processor cycles, unless they're just optimized out entirely. If there is a difference, it's entirely negligible. – Servy Oct 02 '13 at 20:56