63

Seen in code:

public String getX(){
if (x != null){
return x;
}
else return null;
}

... I'm not gonna like looking at the rest of this, am I?

Comments
  • 2
    Tell whoever wrote that to use a linter.
  • 6
    *sigh* That code annoyed me, I came over here to post... and forgot I was looking for an infinite loop somewhere in it and let the tests run for a couple minutes... :( maybe I should sleep... debug... sleep... :-/
  • 1
    Just pray its left over from removing some other check.

    But prepare for the worst.

    At least its more or less harmless except in hi performance scenarios.
  • 1
    Why is "x" global? 😢
  • 1
    Where did x come from?
    Is it global?
    Is it just left behind from a refactor that someone didn't finish?

    Is this legit?
  • 0
    @BluePanda
    The code smell...
    Have fun!
  • 4
    @C0D4

    @juneeighteen

    It looks and reads like Java or .Net to me. which means the "x" is most probably a member variable in a class, maybe something like class Vector() or something that has x/y...

    my assumption is that someone made the x/y Objects instead of primitives, so they are nullable, and wanted to assign a default value if its null, but instead screwed up and just returned "null" instead of "0" or something...
  • 1
    Yes, it was Java, and it was new code not refactored legacy... just a horrible clueless getter for a member variable from someone who doesn't seem to understand how 'null' works. It was just supposed to not have an initial value and they didn't seem to think you could return null by returning the object....
  • 0
    @BluePanda so many of my colleagues forget how initial values work. I was tested on initial values of each data type in school. It’s important to memorize, and easy to test if you forget!
  • 2
    "they didn't seem to think you could return null by returning the object"

    Actually, they knew the object can be null - they compared it to null in the if. Maybe it's just lack of coffee at 6am? Maybe the rest of it is better?

    I'd just refactor as I read it. People hate me for it. I hate them for their code so we're even.
  • 2
    I saw this in two more people's code today....
Add Comment