• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Which of these is more readable to you?

presidentender

Golden Member
Function 1:

private bool Login()
{
bool loginSucceeded;

//Rest of function; check to see whether the login actually succeeded
//and set loginSucceeded accordingly

return loginSucceeded;
}

Function 2:

private bool Login()
{
bool loginSucceeded;

//Rest of function; check to see whether the login actually succeeded
//and set loginSucceeded accordingly

if(loginSucceeded) { return true; }
else { return false; }
}

Right now, I'm using the style of function 1, but I ran across some code that did it the other way. I couldn't figure out why, but it might be for readability. What do y'all think?
 
Number 1. There is no reason for the conditional to essentially convert a bool to a bool.
 
If it's C or C++, beware of cases like:

<a target=_blank class=ftalternatingbarlinklarge href="http://rinkworks.com/stupid/cs_programming.shtml">/* This function is BOOL but actually returns TRUE,
FALSE and -2 because I've no time to change it
to int */</a>

The extra if would flush out a -2 or other number (since -2 when tested as bool is true). But hopefully that's not the case here, and/or it works anyway.
 
In this situation, function 1 definitively. Single entry, single exit is usually the best IMO. I also find it more important the bigger the function. (Small functions don't bug me so much). The large functions are the ones where I find it important to ensure that your functions have only one exit, it can be a nightmare trying to track down why a function is returning a certain value when you don't know exactly where it is exiting.
 
Code should be maintinable and easy to understand. Honestly, the shorter version of (1) is easier to understand simply due to the time that is needed to read the extra crap in example (2). Example (2) would probably cause me to look at it for several minutes out of confusion thinking that i am missing something like a ! symbol somewhere.
 
Originally posted by: Ken g6
If it's C or C++, beware of cases like:

<a target=_blank class=ftalternatingbarlinklarge href="http://rinkworks.com/stupid/cs_programming.shtml">/* This function is BOOL but actually returns TRUE,
FALSE and -2 because I've no time to change it
to int */</a>

The extra if would flush out a -2 or other number (since -2 when tested as bool is true). But hopefully that's not the case here, and/or it works anyway.

ditto. if using the built-in boolean type everywhere, definitely 1. but in C++ there are often custom defined boolean types, and sometimes you have to mix them, in which case 2 would be safer and often remove compiler warnings.
 
Originally posted by: dighn
Originally posted by: Ken g6
If it's C or C++, beware of cases like:

<a target=_blank class=ftalternatingbarlinklarge href="http://rinkworks.com/stupid/cs_programming.shtml">/* This function is BOOL but actually returns TRUE,
FALSE and -2 because I've no time to change it
to int */</a>

The extra if would flush out a -2 or other number (since -2 when tested as bool is true). But hopefully that's not the case here, and/or it works anyway.

ditto. if using the built-in boolean type everywhere, definitely 1. but in C++ there are often custom defined boolean types, and sometimes you have to mix them, in which case 2 would be safer and often remove compiler warnings.

Looks like Java to me anyway. I vote, like others, for #1 for readability and to avoid the control instructions.
 
I personally like #1 but I have had developers make comments that they don't like that style. I never quite understood that.
 
Originally posted by: degibson
This has to be the most unanimous vote I've seen.

Yeah, I'm surprised, since at least one (two if whoever posted about it up above speaks truth) person does it that way. Maybe people who don't like the winning style also aren't the type to frequent coding fora.
 
Sometimes neither. It depends on the login code.

private bool Login()
{
return doLogin(whatever, parameters, are, needed);
}

EDIT: For this particular question, I prefer #1 by far.
 
Back
Top