125

While reading through the Elasticsearch (Java search engine) source code a while ago I found this gem:

return i == -1? -1: i;

I think someone should stop drinking while coding.

Some other nice lines:

int i = 0;
return j + 1000 * i;

Are these guys high?

Comments
  • 35
    I didn't expect the tag
  • 8
    @Alice there are 2 lines in between but i isnt changed or accessed in any way. I guess it once had a non zero value and somebody forgot to remove it.
  • 9
    Have you created a pull request yet?
  • 4
    The compiler should be able to recognice/fix this, right?
  • 0
    never trust the compiler! @Wack
  • 0
    @vadimir but my "Computer Architecture and Systems Programming" teacher allways tells us that the compiler is our friend!

    @irene why not? It's a pretty quick fix, so that should be optimizable, hell a c++ compiler even replaces a recursive call to a function calculating fibonacci, if it detects that it's called by a constant, with just the result. So those should be fixable too...
  • 1
    @irene so let's say you have a funtion to calculate fibonacci something like
    function int calculate(n){
    if (n < 2) {return 1;}
    return calculate(n-1)+calculate(n-2);
    }

    And it's somewhere called with something like
    int n = 2*3;
    int result = calculate(n);

    A compiler will know n is static, and won't change, so first it replaces that line with
    int n = 6;
    It will also replace the line with the result woth simply
    int result = 13;
    As it will follow recursive functions up to some point if it sees a static input to them. Compilers are smart. Compilers are your friend.
  • 0
    @host127001 where is this gem buried? Which version?
  • 1
    @avalanche cant remember, it was half a year ago. Should be 5.something
  • 1
    Does reading The source of OSS actually make you a better coder? I remember I read a few years ago the code for Hadoop but all I see is lots of interfaces and methods calling other methods which eventually comes down to methods that contain a few lines of code and not clear what exactly it's doing and why it's a method that seems to just swap some variables...
  • 2
    @billgates that's a pretty interesting question!

    I've seen a ton of open source projects with really bad (code) quality, so I can only hope that nobody considers them a "role model".

    The more abstract code you mentioned has _probably_ higher code quality, but one has to really dive into the source code to understand how things work. Disclaimer: Code with abstractions and interfaces can be really really bad, too ;)

    You can't have it both ways I guess...
Add Comment