Author Topic: One Pet Peeve.. Stop doing this!  (Read 1571 times)

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 588
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
One Pet Peeve.. Stop doing this!
« on: April 12, 2008, 07:10:03 PM »
Little rant here, I see a LOT of this when I look at code.. Lets say you do a query and one of the field names you are getting is username, the query itself is thrown into a recordset called $logged. A ***LOT*** of code I see these days does this....

Code: [Select]
echo $logged[username];
Do you see the problem? No? <me gets out the ruler and pounds your knuckles>

It's WRONG, it's the WRONG SYNTAX... Just cuz you put error_reporting( E_NOTICES ) and you don't see it doesn't mean its not there!!!! The correct syntax is

Code: [Select]
echo $logged['username'];
A Key/Value array is expecting a key, a key which is a string! If you are referencing it by number then fine you don't need the single quotes. Though using a number makes the code obtuse but thats another peeve all together.

Put the single quotes around your key value, thats the correct way of doing it. Even when you have error reporting skipping notices you are, in fact, still throwing an exception and it's slowing down you code. Doesn't matter if you see it or not, it's going slower.

In fact, IMHO, you should always develop with error_reporting( E_ALL );

If you are not using the single quotes go fix your code now and I mean right now. Those of you that do use the correct syntax well grab a beer on me then :)

Creating online addictions, one game at a time:

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: One Pet Peeve.. Stop doing this!
« Reply #1 on: April 12, 2008, 08:40:05 PM »
It's wrong, unless, of course, it is preceeded with:

define ('username', *Some username variable*);

however, I prefer that my constants be in all caps to distinguish them ;)
Additionally, you should really turn on E_ALL | E_STRICT because it will provide you with some extra information that will assist in making your code run cleanly.

When your code is ready for prime-time, by all means, remove the unnecessary logging. But, until then, you should be informed of every nuance of your code.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 588
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: One Pet Peeve.. Stop doing this!
« Reply #2 on: April 13, 2008, 12:29:11 PM »
There are always execptions to the rules.. $$var is another one, but that wasn't the point. Most code I see these days are not using the single quote around the key when dealing with field names returned from a query.
Creating online addictions, one game at a time:

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: One Pet Peeve.. Stop doing this!
« Reply #3 on: April 13, 2008, 02:01:06 PM »
I understood, it was a dry attempt at sarcasm but the real portion of the message was to make sure others realized that E_ALL doesn't really show you *ALL* errors as the name would imply.

And for $$var to have worked, they'd need to include the delimiters in the code example. I was trying to make mine work with the code sample provided. :D

And, you'd probably be surprised at the amount of code that I've seen where double quotes are used in place of single quotes simply because the programmer doesn't understand the difference.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline FirePenguins

  • Level 10
  • *
  • Posts: 59
  • Reputation: +0/-0
    • View Profile
    • The Iron Helm
Re: One Pet Peeve.. Stop doing this!
« Reply #4 on: April 13, 2008, 10:10:21 PM »
Yeah quotes or lack of them can make a big difference.  I was getting pretty relaxed in my proper use of single quotes and messed up a few mysql queries the other day.  It's good to remind yourself about proper syntax and be vigilant about it even when it isn't 100% necessary.  Otherwise it comes back to bite you eventually.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal