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 cd players touch screen: people with different personalities are just one reason not every mp3 player is suitable for any one person. ยท home theatre audio. www.jmautomotive.com. iriver introduces fast usb 2.0 dongle type built in flash memory mp3 player in 5 different sophisticate colors(black, white, pink, blue and brown) to choose from. it comes with the new windows 8 for fast application loading.
February 28, 2013
gravatar parts of a microscope worksheet for kids: (note to google, analyse that!) - i've got my keywords locked and loaded... in short, get in there fast and furiously, (with both feet on the ground, nothing rash and crazy to be going on) and seize the earliest time to avoid the "hardening" of the plasticity of the brain and nervous system that you do have on your side. how to safely manage asbestos in textured ceiling
March 9, 2013
gravatar Power Washing A Deck: so you can see that a shower head filter is an obvious best buy. many people choose to buy coffee online for the simple convenience that it provides. pitcher filters also come in smaller sizes for individual use as sports water containers. if you show a little care for your dishwasher, you can enjoy the longevity of the product. the heat that is generated is dispersed in a rapid and even manner.
May 17, 2013

Php5_zce_logo

Tags

1337 2008 2010 2011 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile alfred amsterdam amsterdamphp apache api apple article articles atk atkMetaNode audioscrobbler autoloading 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 cilex clear cms cologne common sense communities community components composer conference conferences contest continuous integration contribute contribution crisis css curl custom d-day data migration datetime DbFinderPlugin decorator decorators deployment deps devdays development directoryindex directoryiterator docblox doctrine doctrine2 documentation download dpc dpc09 dpc10 dpc11 DPC2008 dreamhost drupal dv7 eclipse ed editors efficiency enterprise errors event events expertise ezcomponents facebook filter-branch filteriterator finland flickr fork framework frameworks free ticket freelance freeze frontend fun game games geoip germany getting real git github globiterator 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 inclusivity indy ingewikkeld integration international php conference internet interview ipad IPC ipc ipc08 ipc10 ipc11se iterators iterm2 javascript jenkins jenkins-php job job openings jobeet john peel joomla joomladays kiva kubuntu launcher launchy left on the web libcurl libraries library lighttpd lime linktuesday linux live london loudblog m2ts mac magazines malware mambo manchester marjolein mediterra meeting meme meta methodology micro-financing microframework microsoft migration movie music mysql namespace namespaces netbeans netherlands newsfire nllgg northeastphp nos odmarco open source opinion ORM osx paradiso paris partnership pavilion pear pecl performance personal pfc10 pfc11 pfcongres pfcongrez pfz pfz.nl photo php PHP php5.3 phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpday phpdoc phpdocumentor phpgg phpitalia phpnw phpnw08 phpnw11 phpnw12 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 sexism 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 Symfony2 symfonycamp symfonyday symfonylive symfonyUnderControlPlugin talk talks tech 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 - 2013 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