View Full Version : Any C/C++ programmers out there?
Xpirate
10-21-2009, 08:49 PM
Have you ever seen anything like this?
for ( ; ; ) // this sets up an infinite for loop just like while(true)
{
if (something)
{
// do something
break;
}
// processing
if (something else)
{
// do something else
break;
}
// even more processing
if (another thing)
{
// do the other thing
break;
}
// some more processing
break; // THIS BREAK STATEMENT ENDS THE INFINITE FOR LOOP
// the infinite loop only runs one time!
}
The above logic is acting like a goto statement that would look like this:
if (something)
{
// do something
goto label;
}
// processing
if (something else)
{
// do something else
goto label;
}
// even more processing
if (another thing)
{
// do the other thing
goto label;
}
// some more processing
label:
I thought that this was just limited to one nasty function, but this garbage is a design pattern for at least 5 or 6 other functions!
When I first started maintaining this code, I had to reverse engineer a large case statement that was completely made up of hard coded constants that is made to behave like a state machine. I have had to make extensive notes on how the legacy code works just to survive because none of the crap is documented.
All of this code is made up of pure global variables and static global variables. Each source file has at least 4000 lines of non commented code and very few of the functions pass parameters. Everything is glued together with the global variables and it makes it an unmaintainable mess. I honestly had no idea that code could be written this poorly.
My boss gave me this book (http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052/ref=sr_1_1?ie=UTF8&s=books&qid=1256171926&sr=8-1) because I was complaining about the code. The book tells you that you just need to test legacy products more thoroughly. There's also garbage in it about getting back in touch with your "inner programmer" when the stress gets too bad.
I will leave this place when the economy gets better and more software jobs become available. My managers believe that the product I am working on is pristine quality even thought the original author said that he just slapped the junk together for us to use. I fully expect to be scapegoated for this horrible unmaintainable garbage. I'm just glad that my wife makes enough money to support ourselves when the firing takes place.
mDust
10-21-2009, 09:36 PM
I'll start by saying I'm not a programmer, but I've had many (boring) classes on C++, Java and VB. I understand the gist of it.
// the infinite loop only runs one time!
HAHA! Makes sense, right?
I have had to make extensive notes on how the legacy code works just to survive because none of the crap is documented.Poor or non-existent documentation is a deal killer for me. In class we were forced to dissect undocumented code to fix it and add our own comments to it; either proving we knew what was going on or maybe in preparation for the job opening you've been suckered into. (kidding) It can't get worse than that.
I honestly had no idea that code could be written this poorly.I could give it a run for it's money.:)
My managers believe that the product I am working on is pristine quality even thought the original author said that he just slapped the junk together for us to use.Are they programmers as well? Because a lot of people think 'it's golden if it works'...although, like in this case, it's a bitch to maintain.
I feel for ya man.
mtekk
10-21-2009, 11:31 PM
When I first started maintaining this code, I had to reverse engineer a large case statement that was completely made up of hard coded constants that is made to behave like a state machine. I have had to make extensive notes on how the legacy code works just to survive because none of the crap is documented.
All of this code is made up of pure global variables and static global variables. Each source file has at least 4000 lines of non commented code and very few of the functions pass parameters. Everything is glued together with the global variables and it makes it an unmaintainable mess. I honestly had no idea that code could be written this poorly.
I've never seen anything exactly like that, but I have seen some terrible code that had no comments, odd loops, a ton of definitions that shifted arrays from 0 to n-1 to 1 to n, and only used global variables. What I found out was it was converted from Basic to C (explained the shifted arrays) by someone that did not know what they were doing. 99% of the time starting over from scratch is the best way to approach old/legacy code, that's what I ended up doing with the project I had (and found that no one in the company was competent enough to divulge the algorithm that I needed to implement).
Who ever wrote that code should be forced to rewrite it properly as punishment for their incompetence. Every time I see for( ; ; ) I want to smack someone, why not use while(1) instead. It looks better and is readable, for(;;) is somewhat ambiguous as to what it does. Anyways, unless you are an OS there are few cases where you really need an infinite loop (hand coding your own event driven GUI may be another, but why would reinvent the wheel?).
IIRC, the 'while' function wasn't added to C until fairly recently (at least relatively; I honestly don't remember when it was), so that could explain the prevalence of 'for( ; ; )'s. One of my Java profs in college used those a lot...drove us absolutely nuts...
The only reason I can think of for that weird 'infinite once' loop being used instead of goto's is that it's drilled into our heads constantly that 'goto's are, like, the spawn of satan, or something...so I can see why he might have tried to find some other way to do it (maybe consciously, maybe not). He did say that he "just slapped the junk together", so maybe that was just the first thing that popped into his head :P
Trace
10-22-2009, 01:23 AM
That's some pretty epic coding right there!
There are many better ways to do that :D
mtekk
10-22-2009, 09:09 PM
IIRC, the 'while' function wasn't added to C until fairly recently (at least relatively; I honestly don't remember when it was), so that could explain the prevalence of 'for( ; ; )'s. One of my Java profs in college used those a lot...drove us absolutely nuts...
The only reason I can think of for that weird 'infinite once' loop being used instead of goto's is that it's drilled into our heads constantly that 'goto's are, like, the spawn of satan, or something...so I can see why he might have tried to find some other way to do it (maybe consciously, maybe not). He did say that he "just slapped the junk together", so maybe that was just the first thing that popped into his head :P
The while construct is in the 1978 edition of "The C Programming Language". See page 56 for the description. This is the first book on C by the creator of C, so I'm pretty sure while has been there forever. What you are thinking of is the ability to declare a new variable within the for construct, e.g. for(int i =0; i < n; i++). Oh and the for(;;) thing for an infinite loop is on page 57, so I guess that's why everyone likes it (well it's shorter than while(1), but I prefer to use less non-alphanumeric characters if possible). Oh, and yeah I have an original copy of that book (was given to my dad from a friend for me to read an learn C from).
There are so few instances when a real goto is needed (if you think they don't, in most cases, make your code unmanageable, do some assembly programming and get back to me).
Hmmm, maybe I was thinking LISP...or maybe that just spontaneously generated itself in my head and attached itself to C... :P
There are so few instances when a real goto is needed (if you think they don't, in most cases, make your code unmanageable, do some assembly programming and get back to me).
Heheh, I did some work with MIPS for a little while. Great experience; loved doing it, but would not want to do that on a regular basis. MIPS and bat scripting are the only two times I've ever used goto's.
Xpirate
10-23-2009, 12:54 PM
I could give it a run for it's money.:)
In college we had a contest on who could write the ugliest code. The winner had this blob of text that somehow used a recursive algorithm to print out "Mary had a little lamb." But that was an ugly code contest. This code I am working on is for customers, and it should not be this nasty.
Are they programmers as well? Because a lot of people think 'it's golden if it works'...although, like in this case, it's a bitch to maintain.
Some of them are and they do have the "it's golden if it works" attitude.
I'm okay with the infinite for loop vs the infinite while. A teacher showed us that in college and I did not believe that it would compile. I was shocked to see for ( ; ; ) compile.
mtekk
10-26-2009, 08:40 PM
Heheh, I did some work with MIPS for a little while. Great experience; loved doing it, but would not want to do that on a regular basis. MIPS and bat scripting are the only two times I've ever used goto's.
We're doing things with MIPS in my Computer Architecture class this semester, though most of the Assembly coding part is done, we're now concentrating on how to implement the ISA, pipelineing, etc. Also, MIPS has j and jal, which are more or less goto's (or ways to call subroutines).
Drum Thumper
10-26-2009, 08:51 PM
I honestly had no idea that code could be written this poorly.
You should see the lovely legacy code I deal with on a daily basis. I sneaked a peak at the source code once. That **** will make a person go blind.
Powered by vBulletin® Version 4.2.1 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.