Programming peeve of the Day
-
Chris Maunder wrote:
Your rewrite is how it should be done.
Why? It's not as clear as to what the code will do. Plus, returning from inside an if is bad form.
Sure, in a function that is 1000 lines long, it isn't clear. In one that fits on the screen is quite obvious. But I'd argue that a function that long isn't clear either way. :-D
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
It all depends on which measuring stick is used... - # exits in a function - # lines of code - # lines of comments - # paths in the code - readability, - maintainability, modifiability - only use positive condition tests (! using ! and certainly ! !!) And of course if you (think you) are paid by lines of code produced there are many other "solutions"
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
Visual Studio would show you that the else branch in unnecessary.
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
Hmm, to trying to please people who want a single exit point and clear intent when processing is completed:
if (condition)
{
goto returnStatement;
}
// Do something else:returnStatement
return;
X| :laugh: X|
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
My career best:
function doNothing(something) {
return something;
}Nothing succeeds like a budgie without teeth.
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
Just to annoy everyone :laugh:
switch (condition)
{
case true: return;default: // do something else
} -
I believe to be totally proper it should be;
if ( !condition )
{
// Do something else
return;
}return ;
The less you need, the more you have. JaxCoder.com
What is the value of the redundant return statement inside the braces? One of my first jobs was processing in a 16K space, and the data was 12K per record. We coded very concisely as we had no room for extraneous code. Today's compilers are far more efficient, but making the processing straightforward and eliminating redundant code is still a good idea.
-
What is the value of the redundant return statement inside the braces? One of my first jobs was processing in a 16K space, and the data was 12K per record. We coded very concisely as we had no room for extraneous code. Today's compilers are far more efficient, but making the processing straightforward and eliminating redundant code is still a good idea.
I'm retired but I work a lot with embedded systems that have small memory spaces so every byte counts. The redundant return statements where a joke...to see how screwed up we could make it!
The less you need, the more you have. JaxCoder.com
-
I'm retired but I work a lot with embedded systems that have small memory spaces so every byte counts. The redundant return statements where a joke...to see how screwed up we could make it!
The less you need, the more you have. JaxCoder.com
Mike, you're not even close to how screwed up someone could make this! :laugh: I worked with a guy who was intent on cover EVERY possibility. His program executed 700 lines of code when tabbing between fields. If anyone actually clicked something, it go ugly ....
-
Mike, you're not even close to how screwed up someone could make this! :laugh: I worked with a guy who was intent on cover EVERY possibility. His program executed 700 lines of code when tabbing between fields. If anyone actually clicked something, it go ugly ....
I've no doubt that it could be more screwed up and I've seen it when I was getting payed to program. I wonder how some people get into the field and even more how they stay.
The less you need, the more you have. JaxCoder.com
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
if(the_code_below_can_not_be_executed) return;
// now do what must be done -
I've no doubt that it could be more screwed up and I've seen it when I was getting payed to program. I wonder how some people get into the field and even more how they stay.
The less you need, the more you have. JaxCoder.com
Yeah, the lack of ability in some cases is amazing, and not in a good way. My boss tells a story of a former employee who made cut-n-paste an art form. Modern art form. This person appears to have never written a line of code -- everything was copied from other programs and web sites, and this person could not understand why the program would not work. In another situation I taught a COBOL programmer with 5 years experience how to program. I am not a COBOL programmer and have never compiled a single line. This is not picking on COBOL -- this guy did not understand program flow. OTOH, he was fantastic at phone support, which is where he should have been.
-
Hmm, to trying to please people who want a single exit point and clear intent when processing is completed:
if (condition)
{
goto returnStatement;
}
// Do something else:returnStatement
return;
X| :laugh: X|
That's so, so wrong on so many levels...
cheers Chris Maunder
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
-
Chris Maunder wrote:
Your rewrite is how it should be done.
Why? It's not as clear as to what the code will do. Plus, returning from inside an if is bad form.
Agreed! That’s what GOTO is for. :laugh:
Time is the differentiation of eternity devised by man to measure the passage of human events. - Manly P. Hall Mark Just another cog in the wheel
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
-
if (condition)
{
return;
}
else
{
// Do something else
}cheers Chris Maunder
Agreed. I like to avoid (complex) negative conditions; so I might have:
if ( negative condition as a positive) {
// continue.
else {
return;
}(Don't like your braces tho ... bad Feng Shui)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
Yeah, the lack of ability in some cases is amazing, and not in a good way. My boss tells a story of a former employee who made cut-n-paste an art form. Modern art form. This person appears to have never written a line of code -- everything was copied from other programs and web sites, and this person could not understand why the program would not work. In another situation I taught a COBOL programmer with 5 years experience how to program. I am not a COBOL programmer and have never compiled a single line. This is not picking on COBOL -- this guy did not understand program flow. OTOH, he was fantastic at phone support, which is where he should have been.
My first developer job at Microsoft required me to analyze code written by a 'developer who got promoted so is no longer available. I and another dev spent hours analyzing this C language mess. Finally, after hours of analysis, we traced deep enough to get to the root of what the code was doing. Result? It returned the number 1. Yup, that is all the entire 20K lines of code did. The reason it never worked? The array this was used to dereference only had 1 element. Sheesh.
-
Chris Maunder wrote:
Your rewrite is how it should be done.
Why? It's not as clear as to what the code will do. Plus, returning from inside an if is bad form.
Why does this now come to mind? [https://static.wikia.nocookie.net/company-bumpers/images/7/7d/TCFHE-Australia-1995-Rewind.png\](https://static.wikia.nocookie.net/company-bumpers/images/7/7d/TCFHE-Australia-1995-Rewind.png) Money makes the world go round ... but documentation moves the money.