The aim is to write code efficiently, but without compromising easy debugging, maintenance, and especially adaptability for “possible” changes in future.

When I first started programming with Fortran (77), which had been used intensively for half a century in scientific and engineering applications, I learned that all variables had types: integer, real, double precision, complex, etc. If programming started with numeric computation, it required precision, and to do that we need to reduce unknowns.

If we are allocating a room to store data, we need to know the variations so that we can allocate room with proper size. We can not complain to the hotel after booking a room for 2 and arriving with 3 more friends!

I believe it (the chaos) started with variant types and/or pointers! Let me explain..

When I started learning Visual Basic (3.0) and that you do not have to define your variables and data types in certain modes, I was shocked! “Basically” variables of “variant” type were created/destroyed automatically on-the-fly, which may sound allowing so-called programmers to write quick and dirty pieces of code.

One of the best features of Visual Basic IDE was the auto-complete feature where it understood your variables as you write code and depending on the type/class it used to list the properties and functions after hitting the magic character “.” (the dot). The second feature was debugging: I could “step into” code line by line and watch variables.

I think majority of the bugs can be eliminated while you are writing the code. Dynamic languages come with advantages and disadvantages. It is good to have better adaptability but you need to reduce the unexpected run-time errors (exceptions). A programmer needs to expect the unexpected. The more exceptions you handle in your code the better it will run.

For example, PHP magic methods: __get(), __set() and __call()

If we do not explicitly define the properties of our class and simply use a hidden array to manage data dynamically; if we do not explicitly specify the functions (methods) of our class, then we can not expect much help from our favourite IDE (unless you hate IDEs and use notepad or vi, even worse).

When I found Eclipse PDT, I have gone back in time to 90s when I used Visual Basic! It can understand your data structures as you write code. An important note is that you need to help yourself and your team by writing PHPDoc comments for your variables, class variables, functions so that Eclipse can help you in return. For example:

class pc {
    
    /**
     * Serial number of a PC
     * @var string
     */
    public $serial_number;

    /**
     * Clock speed as MHz
     * @var int
     */
    public $speed;

}//end class

class programmer {

    /**
     * Name of a programmer such as 'Murat'
     * @var string
     */
    public $name;

    /**
     * PC of a programmer
     * @var pc
     */
    protected $pc;

    /**
     * Set PC of a programmer
     * @param pc $pc Note that PHP enforces the type of the input at run-time
     * @return void
     */
    public function setPC (pc $pc) {
        //todo
    }

    /**
     * Get PC of a programmer
     * @return pc
     */
    public function getPC () {
        //todo
    }

}//end class

//sample usage
$a_pc = new pc();
$a_pc->serial_number = '1976';
$a_pc->speed = '100';

$bill_gates = new programmer();//he was!
$bill_gates->name = 'Bill';
$bill_gates->setPC($a_pc);//on fire

Considering hundreds of classes in PHP frameworks and libraries these days, “->” (arrow) is a life saver in Eclipse.

Eclipse is one of the best donations of IBM to the IT community!

There are even more dangerous features in PHP, like Microsoft made millions of enthusiasts “programmers” BASICally, and presented office macros to hackers in silver plate, Zend created millions of web programmers and gave them eval and call_user_func_array together with SQL injections.

For example:


class my_app {

    static function main (){
        //read some settings from a file; a XML or even a YAML file these days
        $butler = find_my_butler();
        $task = read_your_task();
        $any_arguments = array();//no arguments please
        //just do something
        call_user_func_array ( array ($butler, $task), $any_arguments);
    }

}//end class

That is what I call: programming in the dark!

Simple solution is to define interfaces. Interfaces set the rules of communication between classes and systems. So, if we receive an input object which implements the methods in our interface, we will happily call the methods and know what it will likely do.


interface butler {
    public function call_my_driver ();
}

class chef implements butler {
    //maybe

    public function call_my_driver (){
        //do it
    }
}

class my_app {

    static function main (butler $a_butler){
        $a_butler->call_my_driver();
    }

}//end class

$a_chef = new chef();
my_app::main ($a_chef);

This is perfectly acceptable, and we are aware of what’s going on during design-time and run-time.

There are millions of PHP applications out there waiting to be fixed; and many “poor” company directors don’t even know what they are paying for when they are hiring/employing an “expert programmer”!

Happy coding!