29 Nov 2004

Getting equality straight

I must suffer from some rare and highly specific form of dyslexia. Even though I know exactly what the difference is, I keep using = when I mean ==. I reckon that about 80% of the problems I've had while writing Ruby have come from using the assignment operator when I really want a comparator.

I need to tattoo = != == on the back of my hand.

  1. 1

    One of the few bits of coding standard that I've seen that I actually found worthwhile taking up is to always put the constant on the left So rather than writing if(x = SOME_CONSTANT) ... which compiles and is wrong, I put if(SOME_CONSTANT = x) which throws a wobbly when it tries to assign to the constant and reminds me that I meant if(SOME_CONSTANT == x) P.S. The fix for the WordPress glitch is working just fine.----- This is one reason I detest the idiom that NSEnumerator forces on programmers: NSEnumerator * e = [array objectEnumerator]; id obj; while(obj = [e nextObject]) { [obj doSomething]; } imo using = in a boolean expression like that should be avoided if at all possible, just to help avoid the confusion you mention. If if weren't for a = b = c; I wouldn't even mind making = not return a value. Casting to/from bools annoys me to; while(1){ bla(); } makes no sense to someone who doesn't know the underlying implementation of the type.

    by David Smith @ 29/11/2004 8:11 pm • Permalink

  • 2

    Well, printing it in your retinia would even help more wink The other confusing thing in languages is the string concatenation operator +, ., .., strcat, etc. Not even a hope it gets better...

    by iiome @ 30/11/2004 2:12 am • Permalink

  • 3

    Don't worry, I do the same in PHP, although once you've spent six hours debugging something only to realise that it was meant to be == instead of = then you do tend to remember it afterwards. It's still nice though when you can pick up someone else who has done the same thing - I did it recently on a forum where everyone else had gone all intricate and complex to sort out a problem, then muggins here piped up "use == instead of =" and you could hear the sound of slapped foreheads and "doh!" from around the country...

    by Lyle @ 30/11/2004 11:11 am • Permalink

  • 4

    You know when you listen to 'surround sound' and the noise goes right over your head? That's how these posts are to me. Yet I feel strangely drawn back each day - just to see if I ever understand one... {8¬)

    by Mr.D. @ 30/11/2004 2:12 pm • Permalink

  • 5

    I'm almost with Mr.D on this one... But you HAVE got me thinking about the ultimate geek tattoo. Hmmmm

    by Gordon @ 30/11/2004 4:12 pm • Permalink

  • 6

    Ultimate (Unix) geek tattoo: /dev/tattoo

    by Brian @ 30/11/2004 6:12 pm • Permalink

  • 7

    steve: That's a good tip! But then I'd have to relearn which side the constant goes on... David Smith: [shudders] I'm glad I don't have to deal with this kind of thing. iiome: For some reason, I don't suffer the same problem with concatenation. Lyle: Nice to know I'm not the only one wink Mr. D.: Sorry! You must get the same kind of feeling I get when listening to Gardener's Question Time. I don't know anything about gardening, and yet I can't stop listening... For any curious non-programmers, = as an assignment operator just puts the value of the thing on the right hand side into the variable on the left hand side (usually). So if you say x = "foo" then asking for x gives you the word "foo". == as an equality operator tests whether the objects on either side are equal, and returns true if they are and false if they aren't. Simple, and therefore amazing that I get it wrong so often. Gordon & Brian: Hehe grin Or, for the web designer, this one. Or perhaps you're a mathematician or architect? Then this tattoo of the Golden Ratio might be just the thing. It's odd that as soon as I saw the latter tattoo, I thought that it was a really odd place to put it. I don't know why I think this, but I'm positive that a Golden Mean tattoo should go over the pectoral muscle.

    by bsag @ 30/11/2004 9:11 pm • Permalink

  • 8

    I just had to approve my own comment, because it had 2 links in it hmmm

    by bsag @ 30/11/2004 9:11 pm • Permalink

  • 9

    You captivated me with Balti, and music, guaranteeing that I'll keep returning. Of course. I have NO idea what the heck you're talking about when you geek out, but I love it anyway--I think it's the accent!:)

    by Tony Iovino @ 30/11/2004 10:12 pm • Permalink

  • 10

    The way I do this is a simple, stupid mnemonic device: one = means 'is' because 'is' is one syllable and there is only one =, and two == is 'equals' because 'equals' has two syllables and there are two ==. so saying 'foo is PI' will return true as long as foo is not a constant, and saying 'foo equals PI' only returns true if foo is actually 3.1415.... Works for me, at any rate... nice job on the gtd app btw

    by Matt @ 01/12/2004 4:13 am • Permalink

  • 11

    I sympathise entirely having been inflicted with a frighteningly similar strain of dyslexia. I feel this qualifies me to offer three little words... Can you guess what they are?

    by Jann @ 01/12/2004 5:13 pm • Permalink

  • 12

    Tony Iovino: I have no idea what I'm on about either, sometimes. Matt: Nice mnemonic. However, I can remember the difference fine, I just don't seem able to spot it when I make a mistake! Jann: Err, no... Go on, what are they?

    by bsag @ 01/12/2004 7:13 pm • Permalink

  • 13

    Yeah, = and == are stupid IMO. I would prefer assignements to be := (like Pascal and Smalltalk) and equality to be =, that'd be simpler, and less errors could arise because you didn't double the equal sign.

    by Vincent Foley @ 02/12/2004 5:12 am • Permalink

  • 14

    Get out more. :o)

    by Jann @ 02/12/2004 3:13 pm • Permalink

  • 15

    Vincent Foley: Ah, Pascal... I remember those days. Jann: Hehe :-D We need someone to monitor the level of geekisms, and ensure that they are within safe operating parameters. I hereby appoint you Geekism Safety Levels Inspector (motto: "Keeping you safe from technobabble since 2004!").

    by bsag @ 02/12/2004 8:12 pm • Permalink

  • 16

    Can I just include management speak within that framework, just to make sure we're all singing from the same hymn sheet?

    by Jann @ 03/12/2004 10:12 am • Permalink

  • 17

    Jann: ...and that we know what we're doing going forward? -----

    by bsag @ 04/12/2004 10:12 am • Permalink

  • 18

    I was reading this post before I turned 18. On my 18th birthday I got inked. Here is what I got: /dev/tattoo I have you people to thank for it. Pictures, if you want. $ lolly

    by Hari Schelldon @ 03/04/2007 7:16 pm • Permalink