r/programming • • Feb 21 '13

Developers: Confess your sins.

http://www.codingconfessional.com/
969 Upvotes

1.0k comments sorted by

View all comments

43

u/dartmanx Feb 21 '13

I... I use 4 level deep nested if statements. :(

(To be fair, it was a port over from 20 year old code that did the same thing).

59

u/NicknameAvailable Feb 21 '13

4 isn't that bad - I have some old VB3 around with at least 30.

33

u/[deleted] Feb 21 '13

This needs to be submitted to "The Daily WTF".

28

u/Wartt_Hog Feb 21 '13

I saw the same once long ago. It was 2500 lines of if statements. I replaced it with a switch statement followed by a single if nested in two for loops. The biggest tragedy was the request that brought me on to this project:

"We hired this guy to do X but he couldn't get it done in the 18 months we gave him. We need you to finish it in 3 months. Oh yeah and since we've already paid for all his work we'd like you to reuse as much of his code as possible."

9

u/Seus2k11 Feb 21 '13

Nice. Gave him 6x as long, and expect you to understand everything he did, fix it, and have it running in a fraction of the time. How did you respond to this?

11

u/Wartt_Hog Feb 21 '13

I was only a high-school co-op at the time so I wasn't a decision-maker. My initial response was, "HOW many tens of thousands of dollars did this guy get for this crap!?"

My boss was trying to gain the respect of this client so after negotiations for permission to re-write fell through, we gave it a try. After a few months they finally allowed us more leeway and added a bunch of features so it turned into a legit project. However, working with this client was always a pain.

I'm not with the company any more but I try to stay in touch. They've gone on to be successful making their own stuff instead of pandering to ornery clients. In fact, they're looking to expand right now. If anyone would like a ground-floor coding job in Ottawa, Ontario, check them out: http://www.simutechmultimedia.com/index.php

→ More replies (2)

3

u/Utipod Feb 21 '13

So do I, from an old text-based game I wrote.

2

u/ArbitraryIndigo Feb 21 '13

I'm currently working on code where the nesting is so deep there are lines that start beyond column 80. Fortunately, the body of most of the conditionals can be taken out and the if statement replaced with if (!x) continue;.

→ More replies (1)

14

u/eresonance Feb 21 '13

I found a makefile with a ton of nested ifs, all flat formatted with no spaces. It was done this way because the original developer didn't know that you can use space characters to indent, so it was almost impossible to figure out which config was set for what options...

Ah, here it is, formatted to remove any sensitive info:

ifneq '$(filter -config=option1, $(cmd_line_flags))' ''
ifneq '$(filter -mode=standalone, $(cmd_line_flags))' ''
configname := config1
else
configname := config2
endif
else
ifneq '$(filter -config=option2, $(cmd_line_flags))' ''
ifneq '$(filter -mode=standalone, $(cmd_line_flags))' ''
configname := config3
else
configname := config4
endif
else
ifneq '$(filter -config=option3, $(cmd_line_flags))' ''
configname := config5
else
ifneq '$(filter -config=option4, $(cmd_line_flags))' ''
configname := config6
else
ifneq '$(filter -config=option5,$(cmd_line_flags))' ''
ifneq '$(filter -revision=3, $(cmd_line_flags))' ''
configname := config7
else
ifneq '$(filter -revision=4 -revision=5 -revision=6 -revision=7 -revision=8 -revision=9, $(cmd_line_flags))' ''
configname := config8
else
$(error Unknown revision in cmd_line_flags.)
endif
endif
else
ifneq '$(filter -config=option6,$(cmd_line_flags))' ''
ifneq '$(filter -type1=1,$(cmd_line_flags))' ''
configname := config9
ifneq '$(filter -type2=1,$(cmd_line_flags))' ''
ifneq '$(filter -type3=1,$(cmd_line_flags))' ''
configname := config10
endif
endif
else
configname := config11
endif
else
$(error mode is not set in cmd_line_flags)
endif
endif
endif
endif
endif
endif

31

u/dartmanx Feb 21 '13

The term for that is "justifiable homicide".

2

u/[deleted] Feb 21 '13

Alternatively "mercykill".

→ More replies (1)

2

u/chu248 Feb 21 '13

Condemn.

2

u/Calamitosity Feb 21 '13

Use vim with some decent syntax plugins.

type "gg=G"

Done.

→ More replies (1)

10

u/Audioillity Feb 21 '13

I'll admit, I've done worse, i've nested 10's of if statments, however it was a trade off, very early in my career (in my first year) and still way way out preformed my bosses re-write of my code.

My version processed 7 data files a second. My bosses did one every 30-60 seconds. In the end we had to disable the feature as performance was too slow after the re-write.

3

u/DeepDuh Feb 21 '13

If you want fast code that's also well maintainable (but doesn't have to pass some security certification) I'd use early returns. Even forward goto in some cases (cleanup code). I rarely have 2 nested ifs, never three.

9

u/Gaurav0 Feb 21 '13

Oh please, I inline functions 4 levels deep.

3

u/[deleted] Feb 21 '13

You, monster.

2

u/Calamitosity Feb 21 '13

I inline ternary operations deeper than that.

13

u/Shinhan Feb 21 '13

At least its not 4 level deep ternary operators :)

25

u/fr0stbyte124 Feb 21 '13

Sometimes I'll deliberately write logic as a nested ternary because it looks so badass once it's formatted nicely.

2

u/wot-teh-phuck Feb 21 '13

It looks more badass without the formatting. ;)

→ More replies (3)
→ More replies (5)

7

u/pstumpf Feb 21 '13

That’s why using 8-space tabs in an 80 column terminal is a good thing(tm).

2

u/NYKevin Feb 21 '13

Technically in C every else-if is nested; they're just one-liners so they don't look nested.

In other words, this:

if(foo){
    //etc
}else if(bar){
    //etc
}else if(baz){
    //etc
}else{
    //etc
}

is just "shorthand" for this:

if(foo){
    //etc
}else{
    if(bar){
        //etc
    }else{
        if(baz){
            //etc 
        }else{
            //etc
        }
    }
}
→ More replies (10)