Clan x86

Technical (Development, Security, etc.) => General Programming => Topic started by: iago on March 19, 2007, 05:04:21 pm

Title: Making Wrong Code Look Wrong
Post by: iago on March 19, 2007, 05:04:21 pm
http://www.joelonsoftware.com/articles/Wrong.html

This is a fantastic article!

I think it's the first thing I read that articulates why operator overriding is a terrible thing.

I also agree with his version of "Hungarian Notation" - as I was reading it, I was thinking "why aren't I doing this?" It could save me a TON of headaches! And I do do it sometimes, but without really thinking about it.

Anybody who is/wants to program should read this complete, despite its length!
Title: Re: Making Wrong Code Look Wrong
Post by: MyndFyre on March 19, 2007, 05:09:51 pm
He says:
Quote
Code: [Select]
char* dest, src;
This is legal code; it may conform to your coding convention, and it may even be what was intended, but when you’ve had enough experience writing C code, you’ll notice that this declares dest as a char pointer while declaring src as merely a char, and even if this might be what you wanted, it probably isn’t. That code smells a little bit dirty.
I thought that this code would declare both dest and src as char*.  What he meant was:
Code: [Select]
char *dest, src;
which would declare dest as char* and src as char.
Title: Re: Making Wrong Code Look Wrong
Post by: iago on March 19, 2007, 06:06:39 pm
He says:
Quote
Code: [Select]
char* dest, src;
This is legal code; it may conform to your coding convention, and it may even be what was intended, but when you’ve had enough experience writing C code, you’ll notice that this declares dest as a char pointer while declaring src as merely a char, and even if this might be what you wanted, it probably isn’t. That code smells a little bit dirty.
I thought that this code would declare both dest and src as char*.  What he meant was:
Code: [Select]
char *dest, src;
which would declare dest as char* and src as char.
Both
char *dest, src;
and
char* dest, src;
are equally right/wrong. They're both syntactically right, but are almost guaranteed to produce the wrong result.
Title: Re: Making Wrong Code Look Wrong
Post by: rabbit on March 19, 2007, 10:00:11 pm
This is one of the reasons I like to stick my *'s with the variables!  char *dest, src and char* dest, src are the same thing.
Title: Re: Making Wrong Code Look Wrong
Post by: iago on March 19, 2007, 10:06:48 pm
This is one of the reasons I like to stick my *'s with the variables!  char *dest, src and char* dest, src are the same thing.
For me, it's a reason I don't declare variables on the same line:
char *dest;
char src;

Pretty obvious :)

That's not the main reason, it's just a personal thing.
Title: Re: Making Wrong Code Look Wrong
Post by: rabbit on March 19, 2007, 10:20:00 pm
I just think single line declarations of multiple variables just looks ugly :\
Title: Re: Making Wrong Code Look Wrong
Post by: Joe on March 20, 2007, 10:20:17 am
He says:
Quote
Code: [Select]
char* dest, src;
This is legal code; it may conform to your coding convention, and it may even be what was intended, but when you’ve had enough experience writing C code, you’ll notice that this declares dest as a char pointer while declaring src as merely a char, and even if this might be what you wanted, it probably isn’t. That code smells a little bit dirty.
I thought that this code would declare both dest and src as char*.  What he meant was:
Code: [Select]
char *dest, src;
which would declare dest as char* and src as char.

That's quite similar to something that happened quite often in programming class:

Dim i, j as Integer

i would be a Variant and J would be an Integer, whereas they were both (obviously) intended for Integers.
Title: Re: Making Wrong Code Look Wrong
Post by: Warrior on March 20, 2007, 01:42:21 pm
I'm with iago, I never declare more than one variable per line. It's just an awkward thing to do, imho.
Title: Re: Making Wrong Code Look Wrong
Post by: Sidoh on March 20, 2007, 03:33:41 pm
I'm with iago, I never declare more than one variable per line. It's just an awkward thing to do, imho.

Yeah, I agree.  I do it sometimes when the code I'm writing isn't meant to be reused or used for an extended period of time, but if what I'm writing is "serious business," then I declare one per line.  I've seen it cause too many problems.
Title: Re: Making Wrong Code Look Wrong
Post by: MyndFyre on March 20, 2007, 08:24:52 pm
I'm with iago, I never declare more than one variable per line. It's just an awkward thing to do, imho.

So do your guys' classes that might have 10 member variables of which 8 are the same type have 10 lines?
Code: [Select]
private string m_first;
private string m_last;
private string m_suffix;
private string m_adr1;
private string m_adr2;
private string m_city;
private string m_state;
private string m_zip;
Or:
Code: [Select]
private string m_first, m_last, m_suffix, m_adr1, m_adr2, m_city, m_state, m_zip;

I don't really see how the second is more awkard then the first.
Title: Re: Making Wrong Code Look Wrong
Post by: Warrior on March 20, 2007, 08:59:01 pm
Yes if I theoretically ever found the need to use 10 strings as members for my class it'd probably look like that.
Title: Re: Making Wrong Code Look Wrong
Post by: cheeseisfun on March 20, 2007, 09:05:28 pm
Word up. These are good ways to organize your code. Organization is one of the biggest problems in programming. Thanks for posting this article, iago.
Title: Re: Making Wrong Code Look Wrong
Post by: iago on March 20, 2007, 09:38:09 pm
So do your guys' classes that might have 10 member variables of which 8 are the same type have 10 lines?
Code: [Select]
private string m_first;
private string m_last;
private string m_suffix;
private string m_adr1;
private string m_adr2;
private string m_city;
private string m_state;
private string m_zip;
Or:
Code: [Select]
private string m_first, m_last, m_suffix, m_adr1, m_adr2, m_city, m_state, m_zip;

I don't really see how the second is more awkard then the first.
I would declare them the first way, definitely. But it's a personal thing.
Title: Re: Making Wrong Code Look Wrong
Post by: rabbit on March 20, 2007, 11:14:04 pm
First way for me as well.
Title: Re: Making Wrong Code Look Wrong
Post by: Chavo on March 21, 2007, 10:15:04 am
Same, using vertical space never bothers me, but I almost always go back and change my code if a line is longer than my horizontal page width.
Title: Re: Making Wrong Code Look Wrong
Post by: mynameistmp on March 27, 2007, 09:56:56 pm
Haha, good read. Anything I ever wrote was always horrible. Try looking at the ncurses code for slackchat (if i recall correctly, the pscr() function to be specific) ;P
Title: Re: Making Wrong Code Look Wrong
Post by: rabbit on March 28, 2007, 07:19:13 am
I would, if I could find Slackchat anywhere.
Title: Re: Making Wrong Code Look Wrong
Post by: mynameistmp on March 28, 2007, 08:53:30 pm
http://www.javaop.com/~tmp/index.htm