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
gravatar free dating sites: Just discovered your website through Google and I consider this may be a disgrace that you aren't ranked higher due to the reality that that's a brilliant website.
October 14, 2011

Php5_zce_logo

Upcoming events

I will be speaking 06-02-2012: D-Day
I will be speaking 17-02-2012: Techademy Trainingday February
I will be speaking 23-02-2012: Zend Webinar: Git for Subversion Users

Tags

1337 2008 2010 2011 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile alfred amsterdam apache api apple article articles atk atkMetaNode audioscrobbler automation azure backwards compatibility barcelona barcodes bash bbc bbq beatstad belgium best practices bittorrent blogging blogs boards of canada book books bughuntday bundle caching cake cal evans calendar career cat cerf certificate cfp clear cms cologne common sense communities community components conference conferences contest continuous integration contribute contribution crisis css custom d-day datetime DbFinderPlugin decorator decorators deployment devdays development directoryindex docblox doctrine documentation download dpc dpc09 dpc10 dpc11 DPC2008 dreamhost drupal dv7 eclipse ed editors efficiency enterprise errors event events expertise ezcomponents facebook finland flickr fork framework frameworks freelance freeze frontend fun game games geoip germany getting real git github gnome-do google google calendar googletalk graceful degradation hack hackers hidden gem hiphop howto hp HR html http i386 ibuildings icann ide ideasofmarch idm imovie indy ingewikkeld integration international php conference internet interview ipad IPC ipc ipc08 ipc10 ipc11se iterm2 javascript jenkins jenkins-php job job openings jobeet john peel joomla joomladays kiva kubuntu launcher launchy left on the web libraries library lighttpd lime linktuesday linux live london loudblog m2ts mac magazines malware mambo marjolein mediterra meeting meme meta methodology micro-financing microframework microsoft migration movie music mysql namespace namespaces netbeans netherlands newsfire nllgg nos odmarco open source opinion ORM osx paradiso paris partnership pavilion pear pecl performance personal pfc10 pfc11 pfcongres pfcongrez pfz photo php php5.3 phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpday phpdoc phpdocumentor phpgg phpitalia phpnw phpnw08 phpnw11 phpstorm phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected prototype PSR-0 public python qa qr codes re2c recruiting refactoring review rewrite ruby on rails san francisco schedule scifi script security sensio seven things sfdaycgn sflive2011 shell scripting silex simplexml slides smfony software sogeti solar sound speakers spl ssh standard standards star trek static steer strings stylesheets subversion symfony symfony live Symfony2 symfonycamp symfonyday symfonylive symfonyUnderControlPlugin talk talks techademy technology techportal tek09 telecommuting terratec terrorism testfest testing textmate textpattern the right tool timeout tips tld todo tomas tools training twig uncon unet usability usergroup validation vhost video vim vinyl virus warp webinar weblogging webservices wiki windows winphp women wordpress work workshop world world of warcraft wpi writing wunderlist xml xpath xsd yara year youtube zc11 ZCE zemanta zend zend framework zend server zend studio zendcon Zend_Form zite
© 2004 - 2012 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