Drunk me coding.
Next day: WTF, dude…?

  • 14
    Well.. that's a efficient function
  • 8
    i see nothings wrong
  • 17
    @virustark24 no cause the compiler would optimize this away
  • 9
    @virustark24 yeah, it's pretty cool actually! It's basically a form of constant propagation because the compiler knows that
    A: the function returns 3
    B: the function doesn't make any calls to other functions

    Afaik javac does not provide any option to prevent this optimization. It doesn't really matter for java, as javac keeps the method signature for reflection sake.

    GCC on the other hand allows the prevention of constant folding/propagation! With the flag -flto you can disable LinkTimeOptimization. Idk why you'd need it but I think it's pretty cool!
  • 2
    @virustark24 yeah but.. These days do we really have to worry that much about tiny optimizations unless we are Facebook or Twitter.

    Even then..

    You get shit ton traffic then you have money. Buy a decent server, no?
  • 1
    @virustark24 that is a sensible thing to do...but the general consensus is that no one is as good as gcc -O3
  • 1
    When you just start with functional programming
  • 2
    This won't even return 3 from another function. You will have to write return returnthree()
  • 0
    I never write code like this while drunk...
    maybe I need to drink more...
  • 0
    Maybe you wanted to avoid magic numbers, so a function improves that even if this one didn't ;)
  • 3
    Maybe add some comments to clarify?
  • 0
    It dawns on me drunk you did not only write such a function for the number three....
  • 0
    Your method should have been private static int.
  • 0
    compiles without errors, whats the Problem?
  • 0
    Nothing wrong. It'll help your Compiler optimize it.

    Const it too while you're at it.
  • 0
    Wait. I muddled up java and c++. 😂

    Do one of the two, depending on your language.
    The Compiler will take care of the rest.
  • 1
    Perfectly readable, does what it says.
  • 1
    private int getthree(int number) {
    if (number == 3) {
    return number;
    } else {
    return 3;
    //fixed your drunk code!
  • 1
    @dmoa Oh, now it works 😉👍
Add Comment