News:

Happy New Year! Yes, the current one, not a previous one; this is a new post, we swear!

Main Menu

Making Wrong Code Look Wrong

Started by iago, March 19, 2007, 05:04:21 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

iago

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!

MyndFyre

He says:
Quote
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:

char *dest, src;

which would declare dest as char* and src as char.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

iago

Quote from: MyndFyrex86] link=topic=8846.msg112413#msg112413 date=1174338591]
He says:
Quote
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:

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.

rabbit

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.

iago

Quote from: 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.
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.

rabbit

I just think single line declarations of multiple variables just looks ugly :\

Joe

Quote from: MyndFyrex86] link=topic=8846.msg112413#msg112413 date=1174338591]
He says:
Quote
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:

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.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Warrior

I'm with iago, I never declare more than one variable per line. It's just an awkward thing to do, imho.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Sidoh

Quote from: Warriorx86] link=topic=8846.msg112507#msg112507 date=1174412541]
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.

MyndFyre

Quote from: Warriorx86] link=topic=8846.msg112507#msg112507 date=1174412541]
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?

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:

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.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Warrior

Yes if I theoretically ever found the need to use 10 strings as members for my class it'd probably look like that.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

cheeseisfun

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.

iago

Quote from: MyndFyrex86] link=topic=8846.msg112541#msg112541 date=1174436692]
So do your guys' classes that might have 10 member variables of which 8 are the same type have 10 lines?

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:

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.

rabbit


Chavo

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.