jeudi 16 juin 2016

Should I use else when I don't need to? [on hold]

I know this is a dumb question probably already answered, but I can't find an easy to search for it. My question is should I be using "else" when I don't need to? Using VB.NET...

Function IsHappy(hasBeer As Boolean)
        If hasBeer = True Then Return "Happy"
        msgbox("I'm sad")
        Return "Sad"
End Function

Or...

Function IsHappy(hasBeer As Boolean)
    If hasBeer = True Then
        Return "Happy"
    Else
        msgbox("I'm sad")
        Return "Sad"
    End If
End Function

These look the same to me, except that the first one is just a bit less code to look, but the second one is a bit clearer though more verbose. Is there a reason to pick one of these over the other?

Edit: Removed the obvious short cut of just returning hasBeer and made it slightly more complex

To clarify, I'm just trying to figure out if there is a good reason to use an else statement when the IF is going to exit your early if you don't. Is it just a style choice with no clear preference?

Aucun commentaire:

Enregistrer un commentaire