Programming with too many unknowns in a dynamic language

..contributing to IT in London since 2002..

Programming with too many unknowns in a dynamic language

The aim is to write code efficiently, but without compromising easy debugging, maintenance, and especially adaptability to “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:

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:

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.

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!


Comments: 1

  1. Lionel says:

    I agree with you. The maintainability and simplicity of code is not valued enough. There is sometimes a choice to make when writing code to optimize for performance or simplicity. Do I want this routine to be super efficient, or do I want the next programmer who touches it to be able to understand easily what it does? Both add value to the product.

Add your comment