Author Topic: Making Wrong Code Look Wrong  (Read 6402 times)

0 Members and 1 Guest are viewing this topic.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Making Wrong Code Look Wrong
« 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!

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Making Wrong Code Look Wrong
« Reply #1 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.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Making Wrong Code Look Wrong
« Reply #2 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.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #3 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Making Wrong Code Look Wrong
« Reply #4 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.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #5 on: March 19, 2007, 10:20:00 pm »
I just think single line declarations of multiple variables just looks ugly :\

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Making Wrong Code Look Wrong
« Reply #6 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.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #7 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.
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

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Making Wrong Code Look Wrong
« Reply #8 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.

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Making Wrong Code Look Wrong
« Reply #9 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.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #10 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.
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

Offline cheeseisfun

  • Full Member
  • ***
  • Posts: 102
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #11 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.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Making Wrong Code Look Wrong
« Reply #12 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.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #13 on: March 20, 2007, 11:14:04 pm »
First way for me as well.

Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: Making Wrong Code Look Wrong
« Reply #14 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.

Offline mynameistmp

  • Full Member
  • ***
  • Posts: 111
  • Hi! I'm new here!
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #15 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

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #16 on: March 28, 2007, 07:19:13 am »
I would, if I could find Slackchat anywhere.

Offline mynameistmp

  • Full Member
  • ***
  • Posts: 111
  • Hi! I'm new here!
    • View Profile
Re: Making Wrong Code Look Wrong
« Reply #17 on: March 28, 2007, 08:53:30 pm »