My article for PHP Advent 2009, the first in this year’s offerings, has been published: http://phpadvent.org/2009/comprehensible-code-by-paul-jones. Summary: “Reading code is hard work. Here are some reasons why, along with some tips on how to make it easier for other developers to understand your code.” If you would like to comment on it, please do so here. Thanks!
Categories
Archives
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- July 2007
- June 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
Nice article! A lot of good points. I’m always cringing at code I’ve written 6 months-6 days ago. :P
Hi, nice article. I’m from brasil and would like to know if I can translate it in portugues and post in my blog?
tks.
Very nice article. I really like the “writing code your code as a teaching exercise” take on this. I am very conscious of writing maintainable code, but I’ve never thought of it quite that way before.
Some really good points and well made. Following a defined set of coding standards/guidelines and favouring readability over complexity is important – for yourself, and others!
Simple and well thought out code is a joy to come across. Let’s have more of it, please :-)
One item 1, this is always been an issue. I’ve modified the way I do things to please other people, while not driving me insane. Of course, the next thing would be the tabs vs spaces.
If you are wondering, I use a modified GNU style of coding, spaces and line up all my equals with in each scope.
nice explanation of why it’s good to write comprehensible code. Hope it will help convince people of the benefits.
As you say in your last statement, I know I totally forget the logic that got me to write some code a year or so ago. Good writing style + appropriate comments helps me understand my own code, let alone others.
Shame comments aren’t supported on phpadvent.org though..
“In short: when writing code, remember the poor bastard who will have to read your program. That poor bastard may be you, six months from now.”
Hit the nail on the head there, shall be bookmarking, great article!
Good article. Every developer must read these kind of writings. Educating developers in this direction is obligatory. Thank you.
I’ve one observation. That “poor bastard”‘s life is easier, in some cases, if there’s a “ton” of inline documentation/comments in the code. You can’t design software for the worst programmer of your team. The code (and you) has to be clever (but not tricky).
Fantastic start to PHP Advant 2009! This very thing is something I’m always encouraging myself and co-workers to improve upon.
This whole concept is really an iterative exercise. I am speaking solely about code that one writes and has to maintain over time. I have found what works for me is to never be opposed to writing code over from scratch. Not entirely from scratch mind you but be willing to make big changes repeatedly over time. Comprehensible code is directly related to having a design that cannot be simplified any further. It’s the complexity of the solution that makes code incomprehensible more than anything else. The iterative part comes in solely for the purpose of taking your solution and figuring out a simpler way to accomplish the same thing whether it be the whole design or just some key functions. There is a saying about complexity that goes like this – “the more complex something is the more labels we need to create to manage it”. This is so true in programming. Personally speaking, having worked on large systems where large volumes of data need to be processed it is a mistake to code for performance. Instead code for simplicity and when the design is as simple as you can make it then go back and tweak for performance.
Paul,
Your article certainly will be a reference for good programming. I worked in a company that did a lot of incomprehensible things likes using coded names for table’s columns (like a table “emblem” for “e-mail marketing [the app], black list e-mails”).
This sort of thing is a horrible way to communicate, specially for new programmers.
We have luck because, in PHP world, we have so many good tools to inspire us with our code (PHPUnit, Solar, ZF, symfony are my preffered!).
While not entirely related to this article, I will be waiting for your word about design patterns and communication in code.
@Rodrigo I you can translate the article without problems. Read the Creative Commons license: http://creativecommons.org/licenses/by-sa/3.0/us/ and give the credits in your post.
I am a huge fan of writing clean, readable code. It’s amazing the mileage you get from simply applying some “extract method” refactoring to some code:
if (!is_null($this->pattern) && !preg_match($this->pattern, $path)) {
return false;
}
can be refactored to:
if (!$this->patternMatchesPath($path)) {
return false;
}
and taken one step further:
if ($this->patternDoesNotMatch($path)) {
return false;
}
When you read that last version it sounds like the proper English sentence “If this pattern does not match path” with no Yoda-like “If not you will” weirdness. Stuff like that is even easier when you use an IDE like Zend Studio that can automatically apply extract method to highlighted code. I wrote a series of blog articles about creating simple code by applying a few rules (more like guidelines)
Hi Paul,
a well-written and well-argued article. It would be even better though if you added some concrete examples. Yes, I know some of this is subjective, but it is pretty hard to learn good programming practice just be reading prose (as opposed to reading code).