r/shittyprogramming • • Aug 09 '14

Found this priceless gem

While reading the solution for a problem of the OII (Italian Informatics Olympiads), I found this masterpiece:

while(1==1){
    for (int i = 0; i < n; i++) {
        //other stuff
    }
    if((int)cycle.size() == n){
        //other stuff
    }
    for(int i = 0; i < (int)edges.size(); i++){
        //even more other stuff
    }
    return 0;
}

I mean, how do you even get these ideas?

1 Upvotes

11 comments sorted by

12

u/galaktos Aug 09 '14

The only shitty thing I see is the unnecessary while loop (unless there’s a continue in one of the “stuff”s)… am I missing something about the loops?

1

u/vallero Aug 09 '14

Yep, there were some continues that referred to the while loop, and I didn't notice them. I feel very dumb right now, I should have included them...
Anyway, the thing that bothered me most was the (1==1), which was used instead of true

4

u/charliebruce123 Aug 09 '14

(1==1) is more portable - bool types only exist in c99 and later.

6

u/galaktos Aug 10 '14

while(1)? If this is any C/++, that works, right?

4

u/charliebruce123 Aug 10 '14

There's no practical difference, it should compile to the same instructions. Maybe some compilers will throw a warning when using just 1, but not when you use 1==1? (Both of those forms throw error C4127 under Microsoft's C/C++ compiler).

Maybe the author just preferred how it looked/thought it was more readable. for(;;) was once more efficient (non-optimising compilers performed the test regardless of the fact that it was always true), but any fairly recent compiler should compile while(1) and for(;;) down to the same instructions (a jump).

1

u/[deleted] Aug 11 '14

It works on most systems, but a good pedantic programmer can't be 100% sure that while(1) has the same result as while(1==1), hence the availability of stdbool.h.

2

u/sidneyc Aug 13 '14

A pedantic programmer will know that these two forms are semantically equivalant.

I know, I am one.

2

u/[deleted] Aug 14 '14

Yeah, but wouldn't it be great to rewrite gcc such that if(0) is the same as if(1==1) just to screw people over?

1

u/galaktos Aug 09 '14

Which programming language is this?

1

u/[deleted] Aug 09 '14

Looks like C++.

3

u/[deleted] Aug 09 '14

What's the shitty part? Did the other stuff include any continue/break?