-
A return to good code (14 messages)
- Posted by: Cameron McKenzie ( @potemcam )
- Posted on: March 04 2011 11:03 EST
Sometimes the simplest concepts get overlooked. Perhaps it's just that people are lazy. Perhaps people don't know any better. But really, there's no excuse for writing bad code other than ignorance. Here's a hint on how to write better methods:
A return to good code.
Threaded Messages (14)
- PASCAL! by Mauricio Ga on March 04 2011 13:19 EST
- PASCAL! by Mauricio Ga on March 04 2011 13:19 EST
- A return to good code by Andrew Clifford on March 04 2011 17:08 EST
- double double by Cameron McKenzie ( @potemcam ) on March 04 2011 18:32 EST
- double double by Tsolak Petrosian on March 04 2011 06:42 EST
- double double by Cameron McKenzie ( @potemcam ) on March 04 2011 18:32 EST
- Return do "good" code? by Dmitriy Setrakyan on March 04 2011 18:57 EST
- Another reason by Anders Grusell on March 07 2011 07:58 EST
- This code is horrible by James Watson on March 10 2011 17:29 EST
- A return to good code by James Watson on March 10 2011 17:47 EST
- ...and an unintentionally awful example, as well! by Andres F. on March 11 2011 08:38 EST
- If statements with OR by Peter Salomonsen on March 30 2011 07:17 EDT
- That is not good code by Julio G. on April 21 2011 13:06 EDT
- codes by club stork on January 30 2013 01:34 EST
- lazy coders by matt coleman on January 31 2013 00:23 EST
-
PASCAL![ Go to top ]
- Posted by: Mauricio Ga
- Posted on: March 04 2011 13:19 EST
- in response to Cameron McKenzie ( @potemcam )
Can we call this article "The return of Pascal" ?
-
PASCAL![ Go to top ]
- Posted by: Mauricio Ga
- Posted on: March 04 2011 13:19 EST
- in response to Cameron McKenzie ( @potemcam )
Can we call this article "The return of Pascal" ?
-
A return to good code[ Go to top ]
- Posted by: Andrew Clifford
- Posted on: March 04 2011 17:08 EST
- in response to Cameron McKenzie ( @potemcam )
Isn't "Don't repeast yourself" one of those good code things.
Yeah, DRY humor.
-
double double[ Go to top ]
- Posted by: Cameron McKenzie ( @potemcam )
- Posted on: March 04 2011 18:32 EST
- in response to Andrew Clifford
I might say that a well coded commenting system would catch a double post and not allow it. I'd blame the double on the system, not the user. I'll see if I can dig into the admin console and delete it.
I might say that a well coded commenting system would catch a double post and not allow it. I'd blame the double on the system, not the user. I'll see if I can dig into the admin console and delete it.
-
double double[ Go to top ]
- Posted by: Tsolak Petrosian
- Posted on: March 04 2011 18:42 EST
- in response to Cameron McKenzie ( @potemcam )
If you had a "return" after processing a comment you would not had this problem:)
-
Return do "good" code?[ Go to top ]
- Posted by: Dmitriy Setrakyan
- Posted on: March 04 2011 18:57 EST
- in response to Cameron McKenzie ( @potemcam )
I had to check if today is April's fools day after reading the article. If this is a return to "good" code, then I am staying on the dark side :)
Dmitriy
www.gridgain.com -
Another reason[ Go to top ]
- Posted by: Anders Grusell
- Posted on: March 07 2011 07:58 EST
- in response to Cameron McKenzie ( @potemcam )
"Perhaps it's just that people are lazy. Perhaps people don't know any better."
Or perhaps they need to get laid.
-
This code is horrible[ Go to top ]
- Posted by: James Watson
- Posted on: March 10 2011 17:29 EST
- in response to Cameron McKenzie ( @potemcam )
Wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong wrong.
Ahem...
Wrong. If you submitted your 'good' code where I work it would fail to meet quality standards.
First of all you don't need an if statement. If you have a boolean state, return it. Why write code to check the state for no reason at all.
Secondly, putting completely unnecssary 'flag' variables in your code:
1. It f*cks the compilers ability to catch that you haven't accounted for a logical path.
2. Makes the code much harder to follow. I can't just look and see what a given path returns, I have to read through a bunch of irrelevant code following the logic branch to make sure you aren't goofing with 'flag' later in the method. Say what you mean and mean what you say. Why force a clean logical tree to look like an onion?
The reason people think this is a good way to code is because it was a best practice in languages that didn't check to see that all paths return. If you had a path without a return, the code could bomb or worse. Java checks for this. Somone went through the effort to make the compiler do that. Use the features of the language and stop using stupid 'flag' variables.
-
A return to good code[ Go to top ]
- Posted by: James Watson
- Posted on: March 10 2011 17:47 EST
- in response to Cameron McKenzie ( @potemcam )
Sorry but I just have to reiterate how wrong this article is.
"This is Java 101. Actually, it's not even Java 101. It's grade school Java."
Frankly, I think your article is 'novice developer 101' advice. I'm getting tired of having to help people unlearn this kind of nonsense.
If you haven't already you should read section 10.5.2 Returning Values:
http://www.oracle.com/technetwork/java/codeconventions-137265.html#333
It doesn't give an exact example but as it says "Try to make the structure of your program match the intent." Using 'flag' variables violates this aside from making you look like a noob.
-
...and an unintentionally awful example, as well![ Go to top ]
- Posted by: Andres F.
- Posted on: March 11 2011 08:38 EST
- in response to James Watson
In addition to what James Watson said, the example is horrible. I get that it is supposed to be pseudocode, but the author could have chosen something that can't be simplified to a simple:
public boolean foo() { return true; }
Also, the example is so trivial that it doesn't merit an entire article by itself; maybe as part of a collection of pitfalls for newbies.
-
If statements with OR[ Go to top ]
- Posted by: Peter Salomonsen
- Posted on: March 30 2011 07:17 EDT
- in response to Cameron McKenzie ( @potemcam )
Do you mean that we also should stop using if statements with OR?
The nature of the OR condition is to exit the evaluation as soon as one criteria is true. If we were to follow your example of good code, we should rather set a flag, and keep on evaluating the other criterias as well (even though we now that the OR condition is already met)?
A waste of CPU resources if you ask me...
-
That is not good code[ Go to top ]
- Posted by: Julio G.
- Posted on: April 21 2011 13:06 EDT
- in response to Cameron McKenzie ( @potemcam )
This might be good code in a structured programming language. I strongly disagree with the author about his example and the whole idea behind is actually "java 101".
I'd advise to check this exact idea about multiple exit point on SO: http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement
Besides, I'd like to add that Martin Fowler has a very different opinion than the author's.
-
codes[ Go to top ]
- Posted by: club stork
- Posted on: January 30 2013 01:34 EST
- in response to Cameron McKenzie ( @potemcam )
yes!!i so agree with this return to good coding
-
lazy coders[ Go to top ]
- Posted by: matt coleman
- Posted on: January 31 2013 00:23 EST
- in response to Cameron McKenzie ( @potemcam )
i agree...laziness is an excuse for bad coding