Skip to content


The Naked Else – How I Hate Thee

I cannot express my frustration at seeing code like this:

if (isset($modifier)) {
    // Do some things.

    // And some more things.

    // Still more
    // things
    // to be done here.

    // There's a whole lot of things here!

    // Last bunch of things
}
else {
    // Do totally different stuff
}

People. Comment your elses! I shouldn’t have to scroll up to read the if again just to figure out what a branch of logic does. This is a maintenance code nightmare, and simply adding a simple “// If validation has failed” will save all kinds of time and frustration.

Posted in Coding.

Got Laid Off Today

I always figured that when I got laid off next, I’d write a scathing missive about how the company that did it was full of blundering morons, savagely incompetent noncompoops, and how I hoped they’d fail miserably without me.

Okay, rather, I figured I’d have the fortitude to resist writing such a post, but I at least figured I’d want to.

But see, I got laid off earlier today and frankly, I’m not upset at all.

I’m not upset with the company – I wish them the very best. They have some neat technology and if it pans out (and it looks like it will), then they’re going to clobber the marketplace. I’m not upset with anyone left behind – these things are usually as hard on them as they are on those that got laid off, and everyone still working there is good people that I just don’t wish this on. And I’m not upset with the management either – they were faced with a touch decision. They’d cut every non-staff expense they could (we watched and noticed and were grateful), but this economy is brutal and people are loathe to commit to purchasing decisions, which affects their bottom line.

And it’s not like I’m going to be in dire straights or anything. Since I was laid off, I can get EI for Quite Some Time, or even better, Ontario offers a Self-Employment Grant that you can get if you’re otherwise eligible for EI. Since I wanted more time to work on Bill On Site, then this may actually work out to my benefit.

Ultimately, I’m choosing not to think of this as a setback, but rather as a hell of an opportunity.

Posted in Bill On Site, Life, News.

Super-useful Komodo Plugins

Komodo Edit is Awesome and has Plugins

Komodo Edit is awesome and you should all use it.

Seriously. I don’t know how much more I need to go on about this point, but the productivity benefits of a good IDE are astounding, and if you’re still using Notepad or gedit or the like like, then you are Doing It Wrong.

If I had to recommend a good IDE, I’d recommend Komodo Edit. I have before. Hell, I’m still their go-to quote, a fact that I find puzzling, yet rewarding.

Anyway. The guys at Activestate switched to using the Mozilla codebase a while ago, which means that anyone who can write a Firefox extension can (in theory) write a Komodo extension. And so they have! And I have found three that I have fallen absolutely in love with.

They are:

TODO Helper

The TODO Helper plugin is great if you leave all kinds of little “TODO:”s scattered through your code as mental bookmarks. This plug-in can quickly search your entire project for all TODOs listed and display them in the bottom panel.

It’s a little frustrating when the framework you’re using leaves TODOs in their releases, but it’s not hard to take those suckers out anyway.

kJSLint

A long while ago, I blogged about how to integrate jslint into Komodo Edit, but I guess that’s not really strictly necessary these days. Since there’s a plugin that integrates jslint into a menu command.

Sure, you lose out somewhat on the ability to control exactly what version of jslint you use, but the version included is rather up-to-date, and saves you the trouble of having to install an interpreter just to run it. A definite win.

(Also, the “Crockford says no.” message it spits out when you fail validation makes me smile every time.)

Source Tree

Though not as slick as Komodo IDE’s code browser, at least the Source Tree plugin is free and does what it says on the tin. Having the entirety of the file’s functions and classes available and easy to navigate is very handy. Shame this isn’t in the free version of Edit to start, but they’ve got to have some features to differentiate the two enough to encourage people to buy the “pro” edition.

And in Conclusion

Komodo Edit. It’s good. Komodo Edit with Plugins. Even better.

Now get coding.

Posted in Coding. Tagged with , , , , , .

Clean Code: Nested conditionals

Ugly code is everywhere. Inside that copy of Microsoft Office 2007, behind that latest Linux kernel, and in the dozens and dozens of web 2.0 MicroISV tools you use day-in and day out. Lurking.

But there are those of us who fight! Who rage against the dying of the light! Those who are more than happy to share our solutions to cleaning up ugly code, in the hopes that up-and-coming programmers will notice and apply said solutions. This… is one such solution.

Take, for example, nested conditions. They look like:

if (thing) {
    [CHUNK A]
    if (thing) {
        [CHUNK B]
        if (thing) {
            [CHUNK C]
        }
    }
}

It’s not THAT ugly, but try extending that out into five or more chunks, or if you’ve already indented a couple of times, you will notice just how ugly this looks. Since each chunk is only executed based on the result of a boolean test (the if statements), and each chunk is directly dependant on the previous chunk (and oughn’t be run if the previous chunk fails), we can take advantage of short-circuit operators and rewrite this section of code to be much cleaner.

Instead, rewrite each chunk into its own function, have that function return a boolean, and you can rewrite the original code like:

$success = do_chunk_a();
$success = $success && do_chunk_b();
$success = $success && do_chunk_c();

This is a huge win in terms of readability, and therefore in terms of maintainability. Especially so if you name your new functions descriptively, like “send_invoice()” or “update_client_list()”. It’s far easier to look at the descriptive name of the functions instead of having to read through the entirety of each chunk, just to find out what the code is supposed to do.

Posted in Coding.

The Micropreneur Academy

As some of you may know, I began working on a side-project back in December – a business I want to launch this year. Thankfully, to a large degree I know what I don’t know, and I’ve been reading as voraciously as possible, trying to fill the gaps. I’ve read books by Guy Kawasaki, I’ve subscribed to blogs for entrepreneurs, and participated in discussion forums for software business owners.

But just today, I decided to sign up for The Micropreneur Academy, by Six Figure Software. Run by a fellow named Rob, who has experience creating, launching, and running not one but many super-small businesses, it looks to be a very interesting resource. Two main things attracted me:

  • It’s not free. Actually, it’s rather pricey. The benefit here is that it keeps out the riff-raff. The only people here are the people who really want to learn how to launch a super-small business and succeed, not just some shmoe who wants to dream of doing constantly without ever achieving. Signing up to something that costs serious dollars at least signifies some intent.
  • The forum. I waffled on signing up for a bit, wondering if the information imparted would really be worth the money, when I realized that it didn’t matter. The course could be a total wash, teaching me nothing that I didn’t already know or couldn’t already find on the internet for free. But it would provide a guided discussion of all the varied aspects of running a super-small business, and all those people on the forums with intent and businesses, who are currently putting it on the line will be there, taking part in that conversation. That, in and of itself, is worth it.

Today was the first day the Academy was open, and the forum is already lively. There’s the seemingly-mandatory introduction thread, there’s a few threads about very specific questions that are returning some interesting answers, and there’s an entire sub-section dedicated to people announcing goals each week and how well they followed through on the previous week’s goals, a brilliant little feature that I immediately participated in. It’s good to have people keeping you honest.

And honestly, I’m really looking forward to the coming weeks and the lessons learned therein.

Posted in Bill On Site.

New in PHP 5.3

PHP is approaching another important release, version 5.3. The release candidate is already available, if you’re interested in trying things out early. I thought I’d take a moment however, and highlight four really interesting new features we can expect in this new release.

1) Namespaces

Adding namespaces solves, in a stroke, a great many conflicts that developers tend to run into with PHP. For example, if I write a database layer class called db and YOU write a database layer called db, and I try to include another library that you’ve written that references your database layer, I’m boned. Since they’re both declared globally, this attempt at code re-use fails.

However, if I write a database layer and I decide to put it in the “danhulton” namespace, and you put yours in the “anotherlibrary” namespace, we’re fine. I refer to “danhulton\db” and you refer to “anotherlibrary\db” and all is right in the world.

Sadly, the namespace delimiter they’ve chosen is the backslash, which to my eyes just looks ODD, and also can run you into issues if you ever have to put your namespace in a string (with backslash being the escape character). Still, this is a net win for writing shareable PHP code.

2) Anonymous Functions

I got into the habit of using anonymous functions liberally in javascript, and have come to sorely miss them in PHP. Sure, you can emulate them with create_function(), but the syntax is awkward and the resource cost prohibitive. As of PHP 5.3, we’ll be able do do something a little like this:

$escaped = array_map(function($value) {
    return "'" . $value . "'";
}, array('sql', 'parameters', 'that', 'need', 'escaping'));

I’m very much looking forward to the cleaner code this will allow me to produce.

3) Ternary (?:) Operator

The ternary operator already exists in PHP, and I use it frequently. It allows you to do something like this:

$colour = ($balance > 0) ? 'green' : 'red';

Or, if you need to ensure a value is set before using it, you can write:

$entries = isset($entries) ? $entries : array();

In PHP 5.3, you’ll be able to simply write:

$entries = $entries ?: array();

Which means you can also write:

foreach ($entries ?: array() as $entry) {
    // some stuff
}

This change is excellent because, again, it leads to cleaner code.

4) Native MySQL library driver

This should be a huge win for many applications which value efficiency. (Although, isn’t that all of them?) The MySQL native driver is a full replacement for the currently-used driver written my MySQL AB. It is more efficient in terms of memory usage (storing all rows only once instead of twice), and should be just as processor-friendly, if not more. Further, there are a host of performance statistics calls that should ensure that performance debugging is much easier, and they include a function I wish had been there from the beginning: mysqli_fetch_all(). No more having to write a big while() loop with mysql_fetch_assoc(), this baby will take care of you in one line.

Now, since I’ve switched to the Kohana framework, my database functions are a lot friendlier already, so the latter function won’t specifically change how I code. But the decreased memory costs, especially for queries that return many rows, will be fantastic.

Conclusion

PHP 5.3 is going to be an excellent addition to the language, one I can’t wait for. I expect a fair amount of grumbling as we get adjusted to all the new features, and I’m rather curious to see what Kohana and other frameworks decide to do with regard to support (ideally for Kohana, IMHO, is keep KO 2.3 compatable with PHP 5.2.9, but require PHP 5.3 for KO 2.4 and 3.0). However, in the long run, we’ll have the ability to write cleaner, clearer, and less code, and that will be a fantastic advantage.

Posted in Coding. Tagged with , , , , , .

PAX 2009

PAX Registration 2009 Confirmation

Posted in Life.

Goals, 2009

New Year’s Resolutions are a waste of time. They’re broken consistantly, they’re frequently un-achievable, they’re vaguely defined… they just flat-out suck. So rather than set a list of easily-broken, poorly-defined resolutions, this year I’m going to set some proper Goals – things to achieve during/before the end of 2009.

Goals have certain qualifications. They must be specific, measurable, realistic, and time-bound. So goals like “lose some weight” are right out, because it’s not specific. Goals like “become mayor of the earth” are, again, right out – not realistic.

So given that, here’s my goals for 2009:

  1. Lose 30 lbs. I don’t want a perfect body, but I do want to get rid of this spare tire I’m lugging around. Getting down to 195 would do most of that.
  2. Excersize 3x weekly, for at least 30 minutes. I’m going to keep a journal and everything.
  3. Launch BillOnSite. There’ll be more on this later, but I think I have a real winner (or at least some real potential) here.
  4. Pay off my PC Financial Mastercard. My strategy here is that every non-rent paycheque is used to reduce it a further $500 from last time. So if it’s at $2000 and I spend $40 in the intervening time, I have to put $540 on it to “catch up”. I don’t know how well that will work, but even if it doesn’t, I should still be able to pay the whole thing off this year.

I am going to make one resolution though, since I have a sort of tradition of including one un-keepable resolution with each year’s list. This year’s will be…

“Punch out Bill Gates in a proper boxing match.”

I’ve got nothing against the guy (actually, I rather admire him), but the chances of me being anywhere NEAR a boxing ring at the same time as him are rather slim, let alone there being a pugilistic agreement between the two of us.

Good luck with the new year, everyone!

Posted in Life. Tagged with , , , , , , .

How to fight a woman

I have fought a few women in my time, and while I can claim quite a few victories, I can also report quite a few losses. Most frustratingly however, is the fact that for the great many of these fights – even the ones where I won – I never quite knew what was going on.

Clearly, I’m not talking about physical fights here; I’m not a violent man, nor do I participate in martial combat that would pit me against the fairer sex. Rather, I’m speaking about your average, run-of-the-mill domestic conflict: the argument.

I doubt I’m alone in this – to a great degree my relationships have been successful (except ultimately at the end, where clearly they were not). But when things went south, and we commenced arguing, things got very confusing.

But then I read an article by Michael Crichton on how to properly conduct a domestic dispute. Or, at least that’s what it would be called, were it targetted to a school or other educational facility.

The really interesting thing about this article actually, is that it was written for Playboy Magazine, and as such, it was targetted at Playboy readers. The article talks a lot about “winning” a fight with a woman, but then goes to great lengths to define “winning” as something other than traditionally known.

Traditionally, the winner of a fight leaves his opponent bruised and bloody, unable to fight back. A boxing match ends with one man standing, the other unable. But Crichton is crafty, and ultimately defines winning in terms that are beneficial to both parties – a win by Crichton’s standard is a win for the relationship, not simply for the man. And yet his terminology, his tone – they both call out to the competative nature in a man. It’s beautiful – a guy following these instructions will lead an argument to a proper, mutually beneficial resolution, and yet sleep well at night, knowing he accomplished his goal, and “won” the argument by his terms.

Regardless of the fantastic misdirection, I have to recommend this article to all the men I know (and some of the women as well, even if just to understand how the other side views yours). It explains a lot of the mysteries behind a lot of women’s actions, and many tactics you can use to successfully resolve situations that in the past may have lead to disaster.

Posted in Life.

Sharing cookies between subdomains in Kohana PHP

So i’m working on a little side-project again, and something cropped up that kept me occupied for about an hour. I have a subdomain for logging in, and from there, users are redirected to personalized subdomains. By default, cookies aren’t shared between subdomains, but there are various ways around that.

At a basic level, the domain for the cookie is typically set to “www.yourdomain.com”. So if you have users redirected to “premium.yourdomain.com”, they lose their cookies because their domain no longer matches. If you set the domain for your cookies to “.yourdomain.com” however, cookies set in one subdomain will match and carry over to other subdomains.

You can set this via php.ini, in your .htaccess files, through an ini_set() call, or even through a session_set_cookie_params() call. However, if you’re using Kohana (as I am), what you actually need to do is to copy system/config/cookie.php to application/config/cookie.php and modify the domain parameter to match the following:

$config['domain'] = substr($_SERVER['SERVER_NAME'],
                           strpos($_SERVER['SERVER_NAME'], '.'),
                           100);

This way, no matter what server name you’re using (development or live), the right cookie domain is set.

One final thing to keep in mind when you’ve implemented this little solution – don’t forget to clear your cookies! I spent a further twenty minutes thinking I’d missed something code-wise because Firefox still had a better-qualified cookie left over from before and was relying on that instead of my sparklin’ new “.yourdomain.com” cookie.

Posted in Coding. Tagged with , , , , , .