Programming Tips #3 “Divide, Indent, Comment and Name”

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

managermaths.jpg

Continuing my programming tips series we have the first guest post from LukasS.

Divide (not conquer), Indent, Comment and Name

We all started programming at some point in our life, some of us on our own, some under guidance of teachers, parents, but even now there are few tips, that should be known by everyone. Here is the four (unordered list):

Divide - when you write any code, you can easily get overwhelmed by the amount of information or functions that can be contained in a single line, come to think of it, you can write whole software in one line, but that’s bad idea! (definitely), so use your „Enter” as much as possible, your code becomes stressed out on more lines, but it’s getting easier to read this way and to upgrade most importantly.

Probably most useful is to divide with new line any braces, these will allow you to clearly see when specific function/control statement starts and where it ends. Use new line to divide clusters of code, thanks to this you can create groups of code, each with specific task, which is easier to read.

Indent - if I get a hold of someone else’s code, the second thing I do (after dividing it), I use my TAB key as much as is need to structure the code even more. With combination of dividing and indenting you can easily move to third part which is:

Comment - professor on my academy, once said, that when we will work on bigger projects, an equivalent of A4 size of paper of code, will be filled with up to 50% of the area with comments. At the beginning of programming journey (and some even later) people are too lazy to comment the code and when it comes to upgrade it, after few months of not seeing it (or sometimes even earlier) many spend hours figuring „what the hell is that all about”. My friend is using comments to display step by step what actions specified file does — he wrote a simple parser that fetches all one-line-comments and displays it in a clear list. This can be helpful when you have one large file with many actions / functions.

One final TIP, but I think most important, even if you don’t like to divide your code, indent or comment it – name your variables / functions accordingly to what they contain or do, You can use capital letters or underscore to create more readable names. When you look at something like this: $tempcustomnonassociativearray it’s hard to see what that means comparing to: $temp_custom_non_associative_array.

Don’t forget to organize, organize and organize.

-

If you have any programming tips that you would like to share with my readers please send them in via my contact form. I will be picking a ‘best tip’ which will be rewarded with a license for Zend Studio Professional.

There Are 9 Responses So Far. »

  1. Rather than manually indenting lines, I like to use Artistic Stlye (http://astyle.sourceforge.net/) to do it automatically.

  2. Thanks for posting this :), never thought It would be published.

  3. Nick,

    Great post! Thanks. Many developers I’ve had to follow needed to have read this. (and I’m sure they felt the same way about me) :)

    =C=

  4. Get a decent editor. Set code formatting preference to the style you like. Press code reformatting hot key. Done. …except for the part where you argue forever with your team about whether braces go on the same line or the next line. I vote for the same line — otherwise the code is basically double-spaced and takes twice as much vertical space.

    Also argue forever with your team about how much commenting is too much, and how much commenting is too little. Oh, code folding in a decent editor helps with this to some extent to because you can hide someone else’s verbosity, if you just need to see the code. See for example http://themindstorms.blogspot.com/2006/11/my-eclipse-code-folding-plugin.html

  5. [...] Programming Tips - Divide, Indent, Comment, and Name [...]

  6. [...] Programming Tips #3 “Divide, Indent, Comment and Name”: “We all started programming at some point in our life, some of us on our own, some under guidance of teachers, parents, but even now there are few tips, that should be known by everyone.” [...]

  7. My friend is using comments to display step by step what actions specified file does

    I’m a fan of using code documentation tools such as phpDocumentor. I’d rather see well designed, loosely coupled, and document classes than line-by-line procedural documentation. Thinking of your code base as an API that should be documented, rather than a series of steps to be documented, is the approach I prefer. I might even go so far as to say that procedural comments are an indication of a potentially bad design.

  8. ashley johnson…

    ashley johnson…

  9. Moon Boy Hentai…

    Moon Boy Hentai…

Post a Response