7 tips to write intelligible code

27 July 2008 1 comment

PHPWriting intelligible code helps to increase the productivity of a developing team, even if you are an independent worker. Why is it so important to write intelligible code? How can it improve the productivity of my team? A messed up code could delay your partners understanding, or it could create a barrier for new developers. Even trying to understand your own code could be a real challenge as well. Here are 7 tips of how to make more intelligible PHP code (coding style tips):

  1. 75-85 characters per line: Each line must contains approximately 75-85 characters. If a line exceeds more than 85 characters, consider splitting the line into multiple lines. It will be helpful for code readability. You can see a great post on this topic in Paul Jones’ blog. He talks about why this limitation is not really arbitrary, and he puts some brilliant examples such as this one:

    Bad

    list($foo, $bar, $baz) = array(Zim::getVal('foo'), Dib::getVal('bar'), Gir::getVal('baz', Gir::DOOM));
    

    Good

    $foo = Zim::getVal('foo');
    $bar = Dib::getVal('bar');
    $baz = Gir::getVal('baz', Gir::DOOM);
    
  2. Assignment statements: Assignment statements ought be aligned for better readability. Let’s see an example:

    Bad

    
    $example = 'string_value';
    $anotherExample = 42;
    $exampleInst = new ExampleClass();
    

    Good

    
    $example        = 'string_value';
    $anotherExample = 42;
    $exampleInst    = new ExampleClass();
    
  3. 4-spaces block indentation: Use an indent of 4 spaces with no tabs. It helps to avoid problems with diffs, patches and SVN history. It helps the readability as well. Usually tabs are 8 spaces length, which means a 5th-block indent starts 40 spaces from the left border (nearly to 50% of a code line).
    
    {block1}
        {block2}
            {block3}
                ...
    
    
  4. Control & conditional structures: These statements must have one space between the control keyword (if, else, for, while, …) and the opening parenthesis, to distinguish them from function calls (which obviously don’t have a space). It’s nice to always use curly braces because they help to decreases the likelihood of logic errors.

    
    if ($display === 1) {
        while ($obj->next()) {
            echo $obj->toString();
        }
    }
    
    
  5. Class and function/methods declarations: Class and function/methods declarations have their opening brace on a new line.

    
    class Foo
    {
        private $_obj;
    
        public function __construct($obj)
        {
            $this->_obj = $obj;
        }
    }
    
  6. Comma-space between items of a list: When we list some items, it’s nice to separate them clearly with a comma followed by a space.

    BAD

    
    $example = array(Foo::get('key',2),$value,true);
    

    GOOD

    
    $example = array(Foo::get('key', 2), $value, true);
    
  7. Use constants properly: If we have a method which returns some status codes, it’s nice to use literal constants. Having descriptive constants helps to make better intelligible code. For example:

    BAD

    
    class Foo
    {
        public static function getStatus()
        {
            $res = 0;
    
            if (!self::isValid()) {
                $res = 1;
            }
            ...
    
            if ($res === 0 && self::isRepeated()) {
                $res = 2;
            }
    
            return $res;
        }
    }
    

    GOOD

    
    class Foo
    {
        const STATUS_SUCCESS  = 0;
        const STATUS_FAILURE  = 1;
        const STATUS_REPEATED = 2;
    
        public static function getStatus()
        {
            $res = self::STATUS_SUCCESS;
    
            if (!self::isValid()) {
                $res = self::STATUS_FAILURE;
            }
    
            ...
    
            if ($res === self::STATUS_SUCCESS && self::isRepeated()) {
                $res = self::STATUS_REPEATED;
            }
    
            return $res;
        }
    }
    

These tips are just some coding style tips. If we really want to write intelligible code, we would keep in mind many more things like to use software patterns, to make use of well established software practises, …

Has Google acquired Digg?

23 July 2008 3 comments

Has Google acquired Digg for 200 millions dollars? Michael Arrington has written a post on TechCrunch saying that both companies have reached an agreement for around 200 millions dollars. The popular social bookmarking site would become part of Google News property.

Digg

Plurk, your life on the line

20 June 2008 4 comments

Plurk is a new microblogging based application. It lets to publish from texts (140 characters) to multimedia files such as pictures and videos. The most revolutionary feature that Plurk brings to us is the original timeline. When you take up using it, it can be a bit confusing, but you can get used quickly. I like how the system let the user knows new updates as well. I think Plurk could be a nice alternative to Twitter, but I have some questions:

  • Why don’t they make a search engine for users? When you start using Plurk, it’s complicated to find friends
  • Why don’t they let upload ours own files directly onto the system?

You can follow me on http://plurk.com/user/alfonsojimenez

HDR time-lapse

19 June 2008 2 comments

This is a High Dynamic Range time-lapse from Goblins State Park in Utah (USA). It is very nice :)


Simplemente tú

19 June 2008 4 comments

Te voy a escribir lo más bonito que he escrito en mi vida, porque simplemente eres lo más bonito que me ha pasado. Desde aquella noche en que me besaste, no ha habido ningún día en el que no haya pensado en ti. Eres mi pasado, mi presente y mi futuro, te llevo en mi mente y corazón a todas partes.

Pienso en ti cuando estoy triste y cuando estoy feliz, cuando me siento arropado y cuando me siento solo, cuando río y cuando lloro, cuando gano y cuando pierdo, tú siempre estás ahí. Eres la estrella que me guía, mi razón de ser, el motivo por el cual merece la pena vivir.

Cuando no estoy contigo me siento perdido, necesito tu presencia. Echo de menos los pequeños detalles que me das cada día; el latido de tu corazón, los abrazos de media noche, el aliento de tu boca cuando me besas cada mañana, el brillo de tus ojos verdes, cuando me dices I love you baby, tu dorada melena al caer el sol, tu bella sonrisa, y es que echo de menos hasta el aire que respiras. Aunque tal vez no puedas enteder todas estas palabras, no te preocupes cariño, que yo te las explico con el lenguaje de mis besos.

Alexandra Storey and Alfonso Jimenez

PHE08 y Cámara Abierta 2.0: El lugar del Internauta

6 June 2008 2 comments

Intruder

Amarok: Project Neon

6 May 2008 0 comments

Project Neon Amarok

Ladies and gentlemen, the doors are opened on the Neon project. Neon is no more than a new service by Amarok, which offers nightly builds to the users who want to try the lastest available version. It should be noted that is not a stable version, but it’s intended to be used by everyone who wants to help find bugs or to join development for Amarok.

If you are using Kubuntu Hardy Heron and you are interested on installing it, just add this line to /etc/apt/source.list and install the amarok-nightly package:

deb http://ppa.launchpad.net/project-neon/ubuntu hardy main

More information | Official Amarok site

Non-free SF on free systems

4 May 2008 22 comments

GNU
People are ignorant, but we already know that. As usual, I’ve read something really stupid on Twitter this morning. It said: I hope Apple will develop for everyone [every OS]. I think it would be one of the worst things to ever happen. Why do people want to install non-free software on free systems? If we want a truly free system, we cannot accept non-free software in our system. If people keep installing, running or developing non-free components, GNU/Linux will be turned into a fuzzy combination of free and non-free software. And we also could find free software which depends on non-free packages. The freedom movement would have failed if this happens.

People wants to install non-free software on free systems because they have not idea. They have not been educated using the free software principles. They don’t understand why the software should be free. They are confused between the free software and the open source movement. The universities doesn’t teach anything about the free software principles, even many teachers don’t know anything about free software. People wants to install non-free software on free systems because they don’t know what the free software is.

The insistence of running Adobe Photoshop on GNU/Linux is a good example of this. I’m not going to talk about the Photoshop/The Gimp challenge, although I manage well myself using The Gimp. Each user should know that Adobe Photoshop has a horrible license which it doesn’t allow the freedom to run, modify and redistribute it. The Gimp (GNU Image Manipulation Program) is available under the terms of the GNU General Public License (GPL), so we have the freedom to run, copy, change, study, distribute, improve the software and release our improvements. That’s how the community gets benefits. The Gimp, like free software, contributes to human knowledge, whilst Adobe Photoshop, like non-free software does not.

I read a message in a mailing list that could not have shocked me more. It said: Google also sponsored some work by Codeweavers to improve [Wine] support for Photoshop (’cause so many people want it) What a bad news for the free software! That’s not what people want, that’s not what GNU/Linux was made for. Why don’t Google support The Gimp instead of a Wine support for Adobe Photoshop?

The match

23 April 2008 2 comments

FC Barcelona are gonna play the most important match of the year tonight. The rivals, Manchester United, will return to the stadium where one of the most glorious night of history was lived They beat Bayern Munchen in the most dramatic European Cup final victory of all-time nine years ago. The city of Barcelona will have the chance to host one of the most attractive matchs of the old continent. Barça will try to hold United to stop their winning streak.

It should be noted aswell that Lionel Messi and Cristiano Ronaldo (probably the two best players in the whole world) will be face to face tonight in Barcelona. Manchester United will try to get a good result to get through to the next round, whilst FC Barcelona will want to arrive at Old Trafford with an advantage. If the Catalans lose tonight, they will say good bye to the season. There is a lot at stake.


Ubuntu on lettuces

6 April 2008 4 comments

I’ve seen on Entre tuxes y pepinos a curious lettuce with the Ubuntu Logo! The company is called El corral de los niños (The children’s farmyard) and it has been seen in Badajoz. And from what I have read, I would like to advise that it can contain bugs :)

Lettuce

Lettuce

Alfonso Jiménez 2004 - 2009 | Creative Commons Attribution | Powered by Lightpress2