Why there are no Programmers’ Dramas

All day long he had been working with a function which used “printf” commands to produce output from a database query. Simple and effective, but it meant that he was locked into one layout everytime he called that function, and if he needed the data displayed in another format, he would have to make another function altogether.

Too limiting, too limiting, he would rail in his thoughts, if only there was some way to populate an array with the db output, then just return the array at the end of the function and parse it after calling!

Simple in principle, yes, but he was working with multiple rows from a single query, which meant that the array to be returned would be nested at least two levels deep: one level for the row ID, to separate rows from each other, and then another level for the actual row data itself.

Nested arrays, he grumbled. I hate nested arrays. The less I have to deal with them, the better. He tried to visualize it, to see himself typing out the code which would loop through the row data and nest it within a loop of row ID’s, all nested within a plain old container variable. But he couldn’t do it. Somehow the vision would not come. He didn’t even need to see the syntax error message to know that this was not working.

It hit him later, in the subway station; he had been so obsessed with using foreach loops and muddling over whether to put variables in quotation marks, that he had missed the simpler solution right under his nose: There’s no need to use a loop for everything; just declare the associative indices explicitly on the second level! After a furtive glance around the empty station to make sure he was alone, he whipped out his computer and began typing furiously.

He was going to sleep well tonight. If he could only refrain from blogging…

Comments

  1. Jesper says:

    “My god, what are all those things for? When I pull down that thing, the lights over all of North London dim.” – Douglas Adams on the Nibus Macro menu, 1989

  2. Greg says:

    Boycott ‘Living Room’ – they are all a pack of Liberals! They are Muslim Lovers! They are heretics! Delink them now!

  3. sparticus says:

    No other comment could have made me hunt for the living room url so quickly (your link’s wrong by the way, they’ve moved :) )…

    Anyway, it seems this guys talking about a church, a church that (get this) loves Muslims! Shocking!

    Anyway the reason I clicked on the comment link is; what is this love code for? Is it for this blog or your thesis one, or something completely different? Is it for skins? In my new design I keep on having to add php to say things like if skin=1 do this, and if it’s skin=3 do this. If your code can somehow get rid of the mounds of code and css that I’ve had to use I wouldn’t mind having a look at it…

  4. Paulo says:

    Sparticus – The code I’m dramatizing over is just a simple db routine for my thesis, WhyBlog.org. I just wanted the db browser to get the rows and return row data as an array, so I could customize the data further after calling the function. Customization of extracted row data will figure prominently in Whyblog.org. ;)

    As for the random skins, I do it by writing up the layouts in sequentially numbered files, then just running “include index(some random number).php” in the main index.php script. I’ve been meaning to publish the code for all to see, but there’s no time to attend to that right now. Soon, though.

    As for Greg’s comment on the Living Room, I looked it over, and decided that I’m not only going to NOT delink them, I’m going to move the link to the top of my link list in giant H1 tags and big purple blinking arrows with the text “GOD LOVES MUSLIMS!!!” That should teach you inflammatory off-topic commenters to stop calling liberals “heretics” on my blog. ;)

  5. Rod says:

    Get some sleep before you start referring to you computer as “my precious.”

  6. anonymous says:

    good one…”my pweh shus”…drool drool

  7. sparticus says:

    Ah I will be using a similar code for mine, but not randomized. I once idley thought that I could make my index page entirely php with no content, just telling it to drag in bits from the database depending on the skin. However I dismissed this as outlandish, far beyond my ability and presumably fataly flawed, however your post made me think again.

    however in other news, shortly I’ll be blogging about the living room…

  8. Bam says:

    Was there any reason why a 2-dimensional array wouldn’t be a good solution? Granted, a multi-dimensional array is really just an array of arrays, so we’re back to your netsing aversion.

    However, dealing with multi-dimensional arrays is typically as straightforward as nesting loops: an outer loop to iterate over rows, and an inner loop to iterate over columns.

    You really ought to consider it unless you have specific requirements that make it impractical.

  9. Paulo says:

    I was under the impression that a nested array is multidimensional. In any case, my concern was not so much about nested loops as it was over quote-mark confusion — whether to quote keys within braces in the loops or not. Also, I didn’t want to loop through every variable in the row; I was only using half of the row data, and the rest was for ID and verification purposes. Anyway, I have a pretty decent snippet of code up and running, so I’m happy with it.

  10. mental says:

    Yep. Been there too.

    I think it’s pretty illustrative of what I think is one of the main sources of mental blocks in programming — getting hung up on what the program “does” rather than what it means.

    (Since programs don’t actually _do_ anything, of course. That’s the place of the computer running them…)

    I’ve found that the more I cultivate the habit of thinking of the problem in terms of a set of rules (as for a game) rather than a set of instructions (i.e. a script), the easier it becomes to see the “cleaner” solutions.

    At least that’s my theory on the whole thing.

  11. mental says:

    You’re correct about multidimensional arrays , for what it’s worth, though I think the term is usually applied to situations where the second-level arrays are all the same dimension.

    Situations where arrays at the same level don’t necessarily have the same dimensions are also called jagged arrays.

    I just call them all nested arrays. ^_-

  12. jiLL says:

    Hello! I checked your photos, some nice shots you’ve got there! Keep it up!

  13. Raffy says:

    jigga-wha?!