Discussions

News: A return to good code

  1. A return to good code (12 messages)

    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 (12)

  2. PASCAL![ Go to top ]

    Can we call this article "The return of Pascal" ?

  3. PASCAL![ Go to top ]

    Can we call this article "The return of Pascal" ?

  4. A return to good code[ Go to top ]

    Isn't "Don't repeast yourself" one of those good code things.

    Yeah, DRY humor.

  5. double double[ Go to top ]

    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.

     

  6. double double[ Go to top ]

    If you had a "return" after processing a comment you would not had this problem:)

  7. Return do "good" code?[ Go to top ]

    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 

  8. Another reason[ Go to top ]

    "Perhaps it's just that people are lazy. Perhaps people don't know any better."

    Or perhaps they need to get laid.

  9. This code is horrible[ Go to top ]

    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.

  10. A return to good code[ Go to top ]

    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.

  11. 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.

     

  12. If statements with OR[ Go to top ]

    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...

     

  13. That is not good code[ Go to top ]

    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.