Tip: use clearly readable variable names, and constants!

Yes, you know what code you write and you also know how it works. You can find your way around it. But what if you haven't touched a piece of code for months or even years. Or what if someone else needs to work with your code. Prevent a hell: use clearly readable variable names and constants.

As an example, let's take a look at an if-statement that I encountered in FUD forum. Note that I have very little experience with FUD forum, and even less with their code:

if ($obj->avatar_loc && $a & 8388608 && $b & 8192 && $o1 & 28 && !($c & 2))

Now, that first object attribute I can figure out, that is not a problem. But now, let's have a look at what else is happening here. Actually, what the hell is happening here? $a, $b, $c and $o1 are absolutely not clear to me, and neither is the use of 8388608, 8192, 28 and 2.

Now, variables should never be long because that will clutter the code, but they should be clearly readable and understable, think: $user_id (or $userId depending on your preference), $file_to_update, $my_own_variable.

Same for "magic" values. I usually have an abstract class named Constants or something similar in which I "save" these magic values, so that the code becomes more clear. As an example, in my code I would use Constants: :D EBUG_ENABLED or Constants::IS_ADMIN.

That makes the code so much more clear and understandable. Because if you haven't worked on code for ages and you need to change or add something, or someone else needs to work on your code, you want that to be equally nice as writing the code in the first place. And everybody knows how annoying maintainance work is as opposed to developing a brand new project with lots of fresh code and fresh problems to solve.

And especially when you release your code as Open Source software, it's even more important. Because you know people will be looking at it and might want to change it. It's not just important that it works properly, but also that people will be able to easily find their way around your code. Without that, there will not be that much developers willing to join your community.

I had to learn this lesson a few years back now, and I am so happy about this. Because every once in a while I need to look at very old code I wrote, and usually it does not make things easy that I used the alphabet for my variable naming. 


Add comment

Comments

gravatar Ivo Jansch: Looking at the values and the operators, I assume that these are bitwise flags. So 8192 is probably the decimal notation of a few flags. Basically in ATK we do something similar, but use constants. We say 'AF_HIDE|AF_READONLY' which is basically 2 bitwise flags or'ed together in a single number.

But I agree with you 100% that this line of code is absolutely unreadable and very unmaintainable. If this were my application, I would reverse any commit that looked like this. :)
December 4, 2007
gravatar Stefan: Ivo, you are right, they are bitwise flags. Searching their code a bit more supports that. But it should be done as you do it, using constants either in a class or globally defined.

Now for the next bad part, it seems they are using the bitwise flags for several different configuration options in one variable. Quite annoying to say the least :(
December 4, 2007
gravatar open source cms: FUD forum have got awful code. Don't ever use it.
December 5, 2007
gravatar Anatoli: This is my way i'm writing.
You can share yours :)


December 6, 2007
gravatar Anatoli: formating is gone away :)
December 6, 2007
gravatar Stefan: Line breaks are back ;)
December 6, 2007

Php5_zce_logo

not tested in IE


Upcoming events

I will be speaking 08-10-2010: Symfony Day Cologne 2010
I will be speaking 09-10-2010: Symfony workshop

Tags

1337 2008 2010 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile amsterdam apache apple article articles atk atkMetaNode audioscrobbler azure backwards compatibility barcelona bbc bbq beatstad belgium best practices bittorrent boards of canada book books bughuntday caching cake cal evans career cat cerf certificate cfp clear cms cologne common sense communities community conference conferences continuous integration contribute crisis css custom datetime DbFinderPlugin decorator decorators deployment devdays development directoryindex documentation download dpc dpc09 dpc10 DPC2008 dreamhost dv7 eclipse ed efficiency enterprise errors event events expertise ezcomponents facebook flickr framework frameworks freelance freeze frontend fun games germany getting real google googletalk graceful degradation hack hackers hidden gem hiphop howto hp html http ibuildings icann ide idm imovie indy ingewikkeld internet IPC ipc ipc08 javascript job jobeet john peel joomla kubuntu left on the web lighttpd lime linux live london loudblog m2ts mac malware mambo marjolein mediterra meeting meme meta methodology microsoft movie music mysql namespace namespaces netbeans netherlands nllgg odmarco open source opinion ORM osx paradiso pavilion pear performance personal pfc10 pfcongres pfcongrez photo php phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpgg phpitalia phpnw phpnw08 phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected public qa recruiting refactoring review rewrite ruby on rails schedule scifi script security seven things sfdaycgn simplexml slides smfony software sogeti solar sound standard standards star trek static steer strings subversion symfony Symfony2 symfonycamp symfonyday symfonyUnderControlPlugin talk talks technology techportal tek09 telecommuting terratec terrorism testfest testing textpattern tips tld tomas training twig uncon unet usability usergroup validation vhost video vinyl virus warp weblogging wiki windows winphp women work workshop world world of warcraft wpi writing xml xpath xsd yara year youtube ZCE zemanta zend zend framework zend server zend studio Zend_Form
© 2004 - 2010 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0.1