Simplicity Rules the Day

February 27th, 2007 at 9:16 pm by Mark
Tags: , , , , , ,

     Before I start this little rant, I just wanna say, this is not to ridicule Chris — he’s certainly worth his salt.  This was just an example — and a rather basic one, at that, given that this was more of an oversight than a faux pas — of something that’s been getting on my nerves for years.

     Every once in a while, someone’ll do something, semi-cool with a piece of software, but their execution makes it appear that they’ve forgotten why a given function works the way it does.  It happens a lot these days, and I’m glad I’m old enough to remember “why” things are like they are.

     A case in point — and Chris is a brilliant guy, mind you, and again, I’m not bashing him — was the beginnings of a WordPress plugin (blog software, for the initiated) to show extra formatting buttons in the Text Editor.

function st_addAdvanced($buttons) {
    unset($buttons[22]);
    array_push($buttons, ‘wp_adv’);
    return $buttons;
}

     It’s a pretty elegant little function.  It takes an array called $buttons, and changes the last value to ‘wp_adv’ and returns it.  Unfortunately, it makes things look ugly because of the way the $buttons array is used elsewhere in the code.
     He says, “Ahh, there are some formatting issues to take care of.”

     I’ve always had this bizarre K.I.S.S. approach to programming anyway.  I mean, hey, why bother calling two other functions, unset() and array_push() when I can do what I need with a single, local variable?

function st_addAdvanced($buttons) {
    $x=$buttons[21];
    $buttons[21]=’wp_adv’;
    $buttons[22]=$x;
    return $buttons;
}

     Sure, I could’ve used two, and said, “$a=$buttons; $x=a[21]; $a[21]=’wp_adv’; $a[22]=$x; return $buttons;” to save keystrokes…. but… I’d be wasting as much memory as I gained CPU by foregoing the functions.

     But that’s basically my argument about most Developers these days.  It’s a pretty serious can of worms for me to open, because I know I’ll have developers coming from all over to tell me I’m full of it…  Even though I was writing Assembler before their parents ever met, and have some pretty cool — working, useful, debugged — software under my belt…. Joke ‘em if they can’t take a f… *shh*

     Seriously, it’s no wonder our CPU and Memory requirements are so ridiculously high these days.  There’s an API or a DLL or a Library for freaking everything!  Layers upon layers, upon more layers, with repositories and snippets and widgets and scripts galore!

     Our Universities teach this method, often telling people, “Do it this way!” without telling people “Why it should be done this way.”  The “simpler” things get, the more abstract they become.
     They’re not churning out programmers and problem solvers.  They’re churning out memorize-and-regurgitate linkers who can’t write code without the assistance of a Visual Integrated Development Environment.

     Hey … Wasn’t the whole point of all this Link Library, Visual garbage to make software development easier?  Faster?  More bug free?

     I used to sit down with vi, edit or Notepad and it’d take me a couple of days to write a program.

     I can use all these neato-keen, new-fangled hooks and VIDEs, and it’ll still take me a couple of days to write a program…
     But it might take weeks to debug.

     Most developers do the same thing… And spend a lot of time setting up their VIDE.  Or getting a bit of code they copied off the internet to work.  Or…

     But, hey … I’m probably full of it, right?

     As employers, educators and policy makers, we need to get back to basics.  Yes, teach how.  But teach why, as well.


8 Responses to “Simplicity Rules the Day”

  1. Chris Says:

    Ouch.

    As a soon-to-be 39 year old part-time coder who’s never used PHP in his life, you’ve ensured I’ll never share any fledgling PHP attempts again.

    Your function works great, thanks for correcting me. You should bundle it up and release it as a plugin — lots of folks seem interested.

  2. Couple WP Plugins » Solo Technology Says:

    [...] by tonight and fixed it up quite nicely, and then very politely and with many nice compliments made me feel about 3 feet tall. Anywho, between those two links you can find the pieces you need to make a little editor plugin. [...]

  3. Monty Says:

    I am bothered by this static $buttons[22] thing. I would want to make that dynamic. $buttons[count($buttons) + 1] Yeah, that uses a function, but it will always work correctly. Or pass “22″ into the function so that is a real function, i.e. reusable. And your code has that added potential bomb called $x. $x is so ubiquitous that everybody uses it as a throw away. And then, next thing you know, your code dies because some asshat used $x in his function you called and you were using it in your for loop… Sure, the variable in the function is not the same as the $x in the main code per se, but, it’s a risk.

    This is using PHP core functions, which I would tend to trust. You’r egetting grumpy in your old age…

  4. Mark Says:

    Why bother making it dynamic? It’s as simple as it can get now. Core functions are still functions, and still cost memory for more variables and CPU cycles to process.

    Can’t pass anything to it — it is what it is.

    $x is a local variable and “global $x;” is undefined — there’s no chance of collision.

    *flip!* And you’re getting contrary in your old age, ya bloody Swede! *snicker*

    You’re just pissed cuz I singled out precious Universities, who fund ridiculous projects like proving that bees can fly. ;-)

  5. Monty Says:

    Is this 1980? Is CPU time and memory a precious thing? It’s a Microsoft world where all code gets bigger and slower as our computers get faster and more powerful. Each new version of Windows brings your latest computer’s blazing new speed back down to the standard slowness. As coders, we don’t worry about such things. Not any more. Load your entire library of functions every time and then use one of them. Add huge amounts of graphic crapola so that even if your code is fast, the graphics card is still running behind the bus screaming “Wait, I haven’t redrawn the screen!”

    Why is that a function anyway? Is there a need to call it more than once? That’s why I say change it as I do. Make it a function to add any button. Just looks like a pointless way to hide a piece of code somewhere else and make the program more complicated than it needs to be.

    Trust the engineers. They hand you a black box and tell you, “stick X in here and Y will come out the other side.” That’s all we need to know. The parameters of X and Y. What goes on inside the box is not for us to know. Trust the box Luke.

  6. Mark Says:

    The point is to do things the simplest way, without modifying core code. This function is an abstraction in quite a complicated piece of code. You’ve seen it, and worked with it, so quit being pedantic. ;-)

    Is there a need for Conserving CPU and Memory —- YES! It’s a “spoiled brat” attempt at software development to say, “It’s here, so I’m gonna use it all!” A LAZY development methodology…

    This is ESPECIALLY critical for shared hosting scenarios.

    I can think of a couple of CMS’s — which I won’t bash specifically, as most people already know that silly dance and its freeware couterpart — which REQUIRE each customer to use MINIMUM 32M of memory per session. You get 100 simultaneous visitors (which isn’t hard!) and all of a sudden, you’re out of memory, and the whole server bogs. Doesn’t that suck?

    And don’t EVEN get me started about optimizing SQL queries where some of these programs do literablly hundreds of the most intense and complex table joins with custom variables per pageview, thereby filling up an entire 32 Meg MySQL cache and forcing every other program on a shared server to re-query the database… and bogging it all down again…

    Just imagine the ENERGY we would save, globally, if developers weren’t maxing out every CPU and amount of memory they possibly could…

  7. Monty Says:

    That array_push() function probably is actually the same as your code if you saw it, globalized.

    “Just imagine the ENERGY we would save, globally, if developers weren’t maxing out every CPU and amount of memory they possibly could…”

    Maybe this is causing global warming!

  8. Mark Says:

    Nah, it was local … The original was only changing element 22 to ‘wp_adv’ … but it was actually element 21 that needed to be changed, anyway, to make it look right.

    Here’s the array…

    $mce_buttons = apply_filters(‘mce_buttons’, array(‘bold’, ‘italic’, ‘strikethrough’, ‘separator’, ‘bullist’, ‘numlist’, ‘outdent’, ‘indent’, ‘separator’, ‘justifyleft’, ‘justifycenter’, ‘justifyright’, ‘separator’, ‘link’, ‘unlink’, ‘image’, ‘wp_more’, ‘separator’, ‘spellchecker’, ‘separator’, ‘wp_help’, ‘wp_adv_start’, ‘wp_adv’, ‘separator’, ‘formatselect’, ‘underline’, ‘justifyfull’, ‘forecolor’, ‘separator’, ‘pastetext’, ‘pasteword’, ‘separator’, ‘removeformat’, ‘cleanup’, ‘separator’, ‘charmap’, ‘separator’, ‘undo’, ‘redo’, ‘wp_adv_end’));

    Warped, yes. But… ;-)

    As for Global Warming — for damn sure it is. Increased energy production equals increased production of environmental ozone, carbon pollution and any number of waste gases. Decreased energy production always equals less… And if the demand isn’t there… Hey.

    I had an environmental, political and economic angle. So there. ;-)