#### Archived

This topic is now archived and is closed to further replies.

# Code so ugly it makes my eyes bleed!

## Recommended Posts

Well, I picked up Core C++: A Software Engineering Approach. I must say that some of his written code is the most ugly and unnatural looking syntax I have ever seen. In fact, if I worked with a programmer who programmed like him, I would kick the person in the head. It is a good book in the fact that he explains some good programming techniques, but the way he writes code is so freaking ugly! Here is an example:


int main()
{
const int NUM = 100;
double total, amount data[NUM]; int count;
char buff[20];
total = 0.0; count = 0;
do {
cout << "Enter amount (or 'end' to finish): ";
cin.get(buff,20); cin.ignore(2000, '\n');
// cout << "You entered '" << buff << "'" << endl;

if (strcmp(buff, "end")==0) break;
amount = atof(buff);
// cout << "Amount: " << amount << endl;

if(amount <= 0)
cout << "This value is discarded as incorrect.\n"
else
{ total += amount;
data[count] = amount;
count++; }
} while (1 == 1);
cout << "\nTotal of " << count << " values is "
<< total << endl;
if (count == 0) return 0;
cout << "\nTran no. Amount\n\n";
for (int i = 0; i < count; i++)
{ cout.width(4); cout << i + 1;
cout.width(11); cout << data[i] << endl; }
return 0;
}


Now I left out the right side comments, but look at that freaking do-while loop! Eww Eww Eww! This is exactly how it looks. [edited by - Zefrieg on August 14, 2002 3:36:56 AM]

##### Share on other sites
I think I''m gonna cry seeing that....

I couldn''t write code that bad if I tried.

##### Share on other sites
I''ve written code worse than that, although mine was deliberately bad.

##### Share on other sites
damn that code is horrible

I took the red pill and here i am.

##### Share on other sites
what the hell is that grotness, wouldnt want to have that stuff on my resume for a job interview LOL :D

##### Share on other sites
Humm? That piece of code isn't that bad IMO - pretty damn straightforward to see what it does. The only bad things are
-(1==1) instead of (true) in the loop ( while(true){} )
-no string class but a char array
-there's an obvious memory overwrite hazard
-data[] should've been replaced with an STL container to get rid of the hazard and to simplify things (no need for 'count')
-more than one command per line
-the if() blah; else {blah;} -structure. One should always use {} when using else statements
All in all, to me it looks like traditional C code with some cout/cin in there. Those things I mentioned aren't *that* horrible like you people are saying.. So educate me. Tell me what's so horrible about that code snippet.

The indenatition is bad though, but I don't think that's what bugged you ppl the most

[edited by - civguy on August 14, 2002 5:34:19 AM]

##### Share on other sites
The formatting!

It''s absolutely HORRENDOUS.

It''s almost an obfuscated C contest entry.

##### Share on other sites
ACCU Review.

quote:
Original post by civguy
Humm? That piece of code isn''t that bad IMO

There''s always one...

##### Share on other sites
quote:
The formatting!
Ah.. I didn''t pay attention to it that much since the source tags in gamedev tend to break formatting every once in a while (especially tabs)

##### Share on other sites
*hits self*
I didn''t even read the first post carefully enough to see that Zefrieg was talking about the syntax of the code (instead of semantics)..

##### Share on other sites
So you have to admit that it''s pretty disgusting

I don''t understand how someone could consciously write stuff THAT bad when writing a book...

##### Share on other sites
quote:
So you have to admit that it''s pretty disgusting
Yeah I admit it . Personally I always use K&R formatting but my friend keeps telling me how ugly that style is

##### Share on other sites
The formatting actually gets worse in some places. The guy expressed how important it is that code is "easily read and understandable" about two hundred times. Well, I can understand what is happening, but damn, reading it is a pain. The thing that bothers me the most about the book is that the writer only seems to show you things that are coded wrong! He will go, "Now let me show you an example of doing this." Then you look through the code and then you read the next paragraph where he says, "Yeah, don''t do that, it is bad programming." Just avoid this book.

##### Share on other sites
quote:
Original post by Zefrieg
He will go, "Now let me show you an example of doing this." Then you look through the code and then you read the next paragraph where he says, "Yeah, don''t do that, it is bad programming." Just avoid this book.

Scott Meyers uses the "Heres one way of doing this:" ...code... "This is bad. Its bad because: " ...reasons why its bad... "Heres a better way: " ...code...

I like this approach. Of course, if your author doesnt explain why its bad, then its a pretty useless book.

##### Share on other sites
hmm.. where's that while keyword... i just can see the do .

[EDIT] Oh man... I finally found it. Using the Search from Explorer.

[edited by - DerekSaw on August 14, 2002 7:01:09 AM]

##### Share on other sites
Yeah, it looks like the while is after the closing brace for the else, heh. Plus it''s an odd mix of C and C++ stuff there, I mean he''s got cout''s and all, but he''s embedded \n''s in there and all his variables are declared at the top (which isn''t always bad, I''ll admit)... I don''t know, he just seems really confused to me.

I guess he''s squashed everything up so it doesn''t take up as much page real estate, but it still could have been done a lot better than that.

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

##### Share on other sites
quote:
Original post by Dean Harding
he''s got cout''s and all, but he''s embedded \n''s in there

What''s wrong with doing that?

##### Share on other sites
double total, amount data[NUM]; int count;

Shouldn''t there be a comma between ''amount'' and ''data[NUM]''?
Other than that, you may not like it, but I could still read it just fine.

##### Share on other sites
quote:
Original post by SabreMan
[quote]Original post by Dean Harding
he''s got cout''s and all, but he''s embedded \n''s in there

What''s wrong with doing that?

Wouldn''t endl be more portable?

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

##### Share on other sites
quote:
Original post by Dean Harding
Wouldn''t endl be more portable?

No. endl and ''\n'' are both defined by the C++ Standard (so are equally portable) and do slightly different things. Either way results in inserting a newline into the stream, but endl also flushes the stream. Unless you want to flush the stream, you should prefer ''\n''.

##### Share on other sites
I would be interested in seeing a couple of examples of how some of you would RE-format (but keep the symantecs as close to the posted code as you can without vommiting). I have been told (and put down [not here]) that my coding style is redundant and bloated, but I always try to Keep It Simple. eg. I always line up my braces, indent, braces always get their own line [when reasonable], One statement ";" per line. Code Segs seperated Ala Lamothe style "/// D E F I N E S //////", comments & somtimes text files that detail what & how & why certain classes were designed.
I know some people will laugh at me for this, and say that It looks like a program right from a College Beginner Class, but I''m not out to make my codes "cryptic" like some of my friends seem to think shows more knowledge.
Should I change my style? Will, if I ever decide to move to a programming position, employers laugh at this?

##### Share on other sites
Well, bad formatting really isn''t a problem since there are many tools that can reformat code. I think IDEs should automatically reformat any source file you open. This way everyone could format their code the way they want and IDE would make it good looking for the rest of the world.

##### Share on other sites
Kinda like saying ''we don''t need grammar, we just need a spellchecker and grammar checker'', isn''t it....

##### Share on other sites
Well, kinda, but there's only one correct grammar in natural languages. I'm not suggesting changing the syntax, just the style.

I can understand that people program with different formatting styles, so it'd be best if everyone could just see the code the way they want. I see it more like changing a skin in GUI than what you said. Or like syntax highlighting colors in an IDE. Or like representing statistics with pie versus pillar versus number representation. Different people like different representations.

[edited by - civguy on August 14, 2002 9:57:38 AM]

##### Share on other sites
i don't think that code is that bad, but it ain't pretty either. i always write really neat code, otherwise it just feels gross and i can't continue working, knowing something isn't 'perfect'. usually when i take some1's snippet of code, i don't ctrl-c+ctrl-v it, i rewrite it in my own style (unless it's too much to rewrite ).

take this as an example of my work.

edit: actually, that's an older version of CLog, but you get the idea.

---
shurcool
wwdev

[edited by - shurcool on August 14, 2002 11:14:09 AM]

• ### Forum Statistics

• Total Topics
628320
• Total Posts
2982057

• 12
• 9
• 9
• 13
• 11