For seven years, I wrote computer programs in Visual Basic Script (VBScript), a Microsoft programming language used in building WebPages from information in a database. As part of a paradigm shift where I work to open-source software, in the last six months I have had to learn and develop software in Hypertext Preprocessor script (PHP).
For the most part, the shift has gone smoothly. My thinking has evolved from a procedural paradigm to a more object-oriented one, and everything I could do in VBScript I can do in PHP and much more; however, months into this shift, I was still wrestling conceptually with the different way the two programming languages deal with arrays.
An array is a “data structure consisting of a group of elements that are accessed by indexing” according to Wikipedia. A phone book is an array, with names serving as the indexes to phone numbers. A spreadsheet table is an array, with column headers and row numbers acting as indexes to data cells.
Spreadsheet |
In VBScript, I always thought of arrays in terms of lists, tables, or cube of data, referencing information within the array using coordinates:
Visualizing VBScript Arrays |
If I want to know what the value for the sixth row third column, I simply referenced x,y coordinates as array(3,6). Looping through the arrays involves incrementing the coordinates. I can get all the values for the third column with {array(3,0), array(3,1), array(3,2)… array(3,n)}. The point is that the third column is always going to be the same column.
PHP uses a very different paradigm for storing arrays. An array in PHP is always a collection of key/value pairs, a row. A multi-dimensional array in PHP is created by putting an array within an array, sets within sets.
Visualizing PHP Arrays |
Looping through the arrays involves recursive iteration. I loop through the first array, and if I come across an item that is an array, I loop through that too. If I want to locate an item in the array, I key directly into with something like array(Laws of Thermodynamics(Zeroeth Law)) or array(3(0)). With 0 representing the first item in each array, Laws of Thermodynamics(2) refers to its second law and Laws of Motion(2) refers to its third.
The advantage of the VBScript method is that column two always represents the same kind of item. The advantage of the PHP method is that it saves memory by not storing empty elements, unless specifically told to. It could be argued that databases store things the VBScript way in tables, and it also be argued that a highly-normalized database more closely resembles the PHP way.
For programmers who started out working with PHP, this is no problem; they know how to accomplish everything I could accomplish in VBScript. I, on the other hand, had to unlearn everything I have taken for granted over the last seven years and learn a whole new strategy for getting data out of arrays.
Comments
One response to “Thinking in Different Programming Languages”