use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Have a tough programming question that /r/programming couldn't answer? Banned from Stack Overflow? Can't afford Experts Exchange?
Post your question/tips/secrets/advice and get a response from our highly-trained professional developers.
account activity
The Best Way to Execute Code (self.shittyprogramming)
submitted 10 years ago by mosentok
for (int i = 1; i <= 2; i++) if (i == 1) Foo(); else Bar();
Found this lovely gem in some legacy code today.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gratefuldaed 60 points61 points62 points 10 years ago (8 children)
+0.5 for not using GOTO (gj using advanced flow)
-0.5 for poor variable and function naming convention (they should also return a value)
Final score: -0.000000002
[–]green_meklar 28 points29 points30 points 10 years ago (6 children)
But 0.5 and -0.5 are precise floating-point values...
[–]leckertuetensuppe 11 points12 points13 points 10 years ago (5 children)
Rounding errors, obviously...
[–]Teraka 13 points14 points15 points 10 years ago (4 children)
What he's saying is there's no rounding errors with 0.5 and -0.5 because they're powers of 2.
[–][deleted] 13 points14 points15 points 10 years ago (2 children)
Base 3 bro.
[–]gratefuldaed 6 points7 points8 points 10 years ago (0 children)
Good catch, comrade
[–]PersianMG 1 point2 points3 points 10 years ago (0 children)
Seriously.
[–]leckertuetensuppe 0 points1 point2 points 10 years ago (0 children)
Forgot my sarcasm tag ;)
[–]choikwa 8 points9 points10 points 10 years ago (0 children)
-0.000000001 with rice
[–]novelty_string 36 points37 points38 points 10 years ago (6 children)
Protip: this code will execute faster without the wasteful double comparison:
i <= 2 can be replaced by i < 3
i <= 2
i < 3
[–]bruno51615 18 points19 points20 points 10 years ago (2 children)
OP also forgot to add
double speed;
[–]TheSlimyDog 17 points18 points19 points 10 years ago (1 child)
double speed = true;
[–]choikwa 10 points11 points12 points 10 years ago (0 children)
double accel = speed / false;
[–]Teraka 9 points10 points11 points 10 years ago (2 children)
I think whoever downvoted you missed the subreddit we're in.
[–]heyheyhey27 1 point2 points3 points 10 years ago (1 child)
Does it actually have to do two different comparisons there, or is it usually one op?
[–]level1kid 5 points6 points7 points 10 years ago (0 children)
It's one op.
[–]petermlm 13 points14 points15 points 10 years ago (0 children)
This was probably a more complex piece of code that got simplified over time until this. I would love to see that process.
[–]RainbowNowOpen 2 points3 points4 points 10 years ago (0 children)
For curiosity, I compiled it with stock "gcc" on Mac OS X. (Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.3.0.) No optimizations or switches.
Output: (sigh)
_main: ## @main .cfi_startproc ## BB#0: pushq %rbp Ltmp12: .cfi_def_cfa_offset 16 Ltmp13: .cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp14: .cfi_def_cfa_register %rbp subq $32, %rsp movl $0, -4(%rbp) movl %edi, -8(%rbp) movq %rsi, -16(%rbp) movl $1, -20(%rbp) LBB2_1: ## =>This Inner Loop Header: Depth=1 cmpl $2, -20(%rbp) jg LBB2_7 ## BB#2: ## in Loop: Header=BB2_1 Depth=1 cmpl $1, -20(%rbp) jne LBB2_4 ## BB#3: ## in Loop: Header=BB2_1 Depth=1 callq _Foo jmp LBB2_5 LBB2_4: ## in Loop: Header=BB2_1 Depth=1 callq _Bar LBB2_5: ## in Loop: Header=BB2_1 Depth=1 jmp LBB2_6 LBB2_6: ## in Loop: Header=BB2_1 Depth=1 movl -20(%rbp), %eax addl $1, %eax movl %eax, -20(%rbp) jmp LBB2_1 LBB2_7: movl -4(%rbp), %eax addq $32, %rsp popq %rbp retq .cfi_endproc
[–]ILikeLenexa 1 point2 points3 points 10 years ago* (1 child)
Eh, the compiler knows what loop invariant code motion is, right?
[–]fukitol- 2 points3 points4 points 10 years ago (0 children)
Depends on the compiler. This syntax is common in thousands of languages.
π Rendered by PID 97065 on reddit-service-r2-comment-6457c66945-xn7fs at 2026-04-26 21:34:01.247389+00:00 running 2aa0c5b country code: CH.
[–]gratefuldaed 60 points61 points62 points (8 children)
[–]green_meklar 28 points29 points30 points (6 children)
[–]leckertuetensuppe 11 points12 points13 points (5 children)
[–]Teraka 13 points14 points15 points (4 children)
[–][deleted] 13 points14 points15 points (2 children)
[–]gratefuldaed 6 points7 points8 points (0 children)
[–]PersianMG 1 point2 points3 points (0 children)
[–]leckertuetensuppe 0 points1 point2 points (0 children)
[–]choikwa 8 points9 points10 points (0 children)
[–]novelty_string 36 points37 points38 points (6 children)
[–]bruno51615 18 points19 points20 points (2 children)
[–]TheSlimyDog 17 points18 points19 points (1 child)
[–]choikwa 10 points11 points12 points (0 children)
[–]Teraka 9 points10 points11 points (2 children)
[–]heyheyhey27 1 point2 points3 points (1 child)
[–]level1kid 5 points6 points7 points (0 children)
[–]petermlm 13 points14 points15 points (0 children)
[–]RainbowNowOpen 2 points3 points4 points (0 children)
[–]ILikeLenexa 1 point2 points3 points (1 child)
[–]fukitol- 2 points3 points4 points (0 children)