Code so ugly it makes my eyes bleed!

Started by
47 comments, last by Zefrieg 21 years, 8 months ago
quote:Original post by CWizard
Well... I do It''s a habit of mine.


...And it''s a good habit, or at least I think so. I comment my code in much the same way .

Especially the deal about dividing the code into "paragraphs" greatly improves the readability, IMHO.
Advertisement
The following stuff is highly a matter of taste, not something that I consider 'correct' in any way
quote:Original post by CWizard
Well... I do It's a habit of mine.
Hmm, overcommenting is actually kind of bad. With such simple code, it's actually faster to read the code to understand what it does, than to read all those comments. And editing the code will be easier with less comments, since one doesn't have to change the comments after every small change in the code.

However, I commented too little (that is, none ). I should've commented what the first loop does and what the last loop does, since those are quite big structural elements. But in real life, I would've extracted those two parts in to their own methods and given them descriptive names like
readUserInputTo(data, total);
and
showDataInTableForm(data);
Then I probably would've just added a small comment before the readUserInputTo() declaration. Like
//reads positive float numbers until "end" is typed
quote:I always declare all variables I'm using in the function at the top with an explaination of it's usage in the function, so I easily can see what a variable is used to by looking at the top.
That's not a good way to do it IMO. Giving variables too much scope will more likely confuse the reader about the intent of the variable. (S)he may have to check through a lot of code to see if that variable is still used somewhere else, when it's not. If a variable has little scope, you can immediately say what it does, and that it doesn't do anything else. E.g. I declared "string buffer" inside that while loop, since it's only used there. Sometimes (but rarely) I move declarations outside loops though (right before them), since I think it's faster.

[edited by - civguy on August 15, 2002 4:25:08 PM]
quote:Original post by civguy
Hmm, overcommenting is actually kind of bad. With such simple code, it''s actually faster to read the code to understand what it does, than to read all those comments.
That is quite true, and why I applied the . I have my habit from my assembly coding, where you need good commenting. I''ve coded only assembly since my 11th winter or so (heading for my 25th now), and only recently started to code in C[++]. But I also need lots of commenting, as my coding style includes all sorts of hacks, tricks, and techniques that are very easy to miss if you''ve been away a while.
quote:Giving variables too much scope will more likely confuse the reader about the intent of the variable. (S)he may have to check through a lot of code to see if that variable is still used somewhere else, when it''s not.

I know this is the main intent for allowing variables to be declared in that way in C++, but in reality I''ve very seldom found myself with a variable that I''m only using in a small part of the function code; they tend to stretch from the beginning to the end, mostly. That could be because my tendency to structure code into preparation, checking, and execute parts, and most variables are needed in one or more parts, leave counters aside. But being an hopeless optimizer, I cannot resist reusing variables (calculations, counters, etc.), it is needed in my code.
I also think it''s nice to have all declarations in one place, of the same reason one put all classes, structs, typedefs, includes, defines, etc. in a header file, properly sectioned. And, I just hate finding declarations in the middle of the mess, and have no clue of what it is doing, or what will happen if I do something with it.
If you want to reuse variables, then declare them within scopes.

Like this:


  void foo() {    {        int f = getF();        doSomething(f);    }    {        int q = getQ();        doSomethingElse(q);    }}  

Presto! q and f will, on most compilers, use the same memory. In some cases, the compiler is smart enough that the scopes aren''t necessary; but this isn''t as likely.

Reusing variables is yucky, since it means your variables can''t be as precisely named.



Don''t listen to me. I''ve had too much coffee.
quote:Original post by Sneftel
Don''t listen to me. I''ve had too much coffee.
I take that as an excuse to continue coding my way

I''ve found a great many cases where declaring all the variables at the start is a very BAD idea. For instance; if your function is going to be creating a large class, and is also dependent on a parameter passed in. You would want to check that parameter before creating the large class, incase you must return with an errorcode. That way, you avoid allocating a large block of memory for no reason whatsoever.

class really_big_class {lotsofstuff;}void ugly_function(int variable) {if (variable == 0) return; //invalid parameter, for whatever reasonreally_big_class memory_waster;do_some_stuff_with(memory_waster, variable);} 
I thought that the stack space for local objects gets allocated when the scope with the declaration becomes active, i.e. if you declare 10 objects at various times in a block of code, the stack allocation is done all at once when the block is first entered. However, the constructor gets called when the declaration code is executed, so if you have a complicated object construction, it''s best to delay the construction until the object is actually needed (so I''m both agreeing and disagreeing with deyja here).

I think an example is needed...

void MyFunc( )
{
object myObj; // stack space for myObj+myObj2 reserved
// constructor for myObj executes

DoSomething( ) ;

object myObj2; // no stack allocation done here
// constructor for myObj2
}

... or am I talking out my arse???

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
I didn''t check any of it for correctness, just formatted it. It has to be about eleventy billion times clearer than what the author wrote. Someone should find that guy and give him a good smacking. Anyone who wants to become a developer that picks up that book to learn with is going to have a HORRIBLE time fixing the things that he''s taught them incorrectly.



  int main(){	const int NUM = 100;	double total, amount;	double 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"       << "Please reenter it correctly.\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;}[/source  
quote:Original post by CWizard
I take that as an excuse to continue coding my way
NOOO! Damn that loophole!


Don''t listen to me. I''ve had too much coffee.

This topic is closed to new replies.

Advertisement