on code.

software, code, art, etc.

Dada code…

without comments

Code style among programmers touches a sensitive subject. It may be hard to understand for an outsider, but code style can make the difference between grasping the function of a piece of code within seconds, or having to plough trough each line individually, correcting or changing markup as you go. Even while typing, a certain way of “doing” things may influence the logic you’re writing down.

As an example, I was once told to “put down the curlies first, then write the code.”. This doesn’t make much sense to a non-programmer, and may not even to a certain group of programmers who never “had to put down the curlies”. In certain programming languages, you put parts of your programme between accolades. Putting down both accolades first helps you not to forget one in the end, a common error. In my case, this goes further, in the sense that I whenever I put something in-between something, I first type the quotes, dashes, percentages or parenthesis first, move my cursor back a bit, and then start typing.

It has become my way of handling a my brush, and really, I can’t stand watching someone doing otherwise. Along those lines, I’ve picked up certain ideas about how code should look like. However, like a musician, playing multiple instruments influences the way you play each instrument in the end. In my case, this has been, for a long time, C++, PHP and Javascript. Each of these languages have *lots* of curlies. With curlies, I compose my sonnettes.

Recently I’ve been learning python. Not having my beloved curlies has been weird, for a long time. Python just looks “plain wrong” to my eyes. Wait. it looks “plain wrong” to my PHP, C++ and Javascript eyes. Just as $dollar signs look plain wrong when going from javascript and C to PHP. Python is just diffent. More dada, maybe, even..

if not fruit:
 fruit = [apple for apple in basket if apple is 'jonagold']

In PHP, this would look more like this:

if( empty($fruit) ){
    $fruit = array()
    foreach( $basket as $apple ){
    if( $apple == 'jonagold' ){
        $fruit[] = $apple;
    }
}

Note that the python code is about two lines, the PHP code takes 6 lines. This is because I have been following my self enforced dogmas: *Always use curlies*. *Never put things on one line*. *Declare Variables*.

Since I cannot know what $fruit is ( be it an array, object or “NULL” ), I must check or force it to become an “array”, or PHP may yell at me. In this case, I’m initalizing $fruit with a fresh, empty array. In python, there’s no curlies. No parenthesis, and $fruit is just a name I call for something that’s apparently “not there”, so I create something and call it $fruit using the sentence “fruit is an apple for each apple in the basket that’s a jonagold”. The colon feels natural: I’m preparing a statement. I pause. Maybe the ‘dot’ as line delimiter whould have suited python well.

After this experience in python, I’ve started looking at PHP in a different way. No more I think of those who omit the curlies whenever possible as “sinners”, No more I curse at people who cram stuff on one line. I’ve started to use “empty” instead of strict checking all the time ( I’ve come to the concusion that if you need strict checking *all* of the time, you might not know entirely what you’re doing, or code against a hostile API ).

So what does this mean? I think it meas that things like this:

foreach( $basket as $apple ) if( $apple == 'golden' ) $fruit[] = $apple;

 

May start to appear in code from now on. While a sonnet can be a thing
of beaty, It’s time for some futurism. or dadaism. da da da da…

Written by Matthijs

February 15th, 2010 at 10:32 pm

Posted in ramblings,Uncategorized

Tagged with ,