Closed for holidays

30 julio 2008 at 18:14

Fecha Filed in Personal
Tags Tags: , , ,

Closed for holidays. I’ll be in Japan until 15th August. I think I deserve a rest. Up until then, have a lovely summer.

Japan

Multiple Constructors in PHP

29 julio 2008 at 22:02

Fecha Filed in Computers
Tags Tags: , , , , ,

Bob the builderAs you probably know, it’s possible to have multiple constructors in Java. They need to have the same name as the class, and they can only be distinguished by the number and type of arguments. In PHP5, you can only have one constructor. You can define it using the reserved word __construct. If the __construct function doesn’t exist, PHP5 will search for the old-style constructor function (by the name of the class). So if we cannot have multiple constructors, how could we create objects with different initial conditions? It’s not a big deal. There’s a pattern called Factory Method, which defines virtual constructors using static methods. Let’s see an example:


class Person
{
    private $name;
    private $email;

    public static function withName($name)
    {
        $person       = new Person();
        $person->name = $name;

        return $person;
    }

    public static function withEmail($email)
    {
        $person        = new Person();
        $person->email = $email;

        return $person;
    }

    public static function fullPerson($name, $email)
    {
        $person        = new Person();
        $person->name  = $name;
        $person->email = $email;

        return $person;
    }
}

We have a class called Person which contains 2 private attributes: name and email. It also has 3 static methods: withName, withEmail and fullPerson. These methods will behave like constructors.

So if we want to create a Person object just with the name value, we can do it using the following statement:


$person = Person::withName('Example');

7 tips to write intelligible code

27 julio 2008 at 13:52

Fecha Filed in Computers
Tags Tags: , ,

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 julio 2008 at 11:58

Fecha Filed in Internet, Bussiness 2.0
Tags Tags: , ,

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