Daniel Kendell

Archive for November, 2008

You’re Doing It Wrong: Branches

by Daniel on Nov.19, 2008, under Programming, Soapbox

A fairly straightforward thing you’d think. And you’d be right. But consider the following example.

if ( isset( $a ) )
{
    if ( isset( $b ) )
    {
        if ( isset( $c ) )
        {
            echo $a, ' - ', $b, ' - ', $c;
        }
        else
        {
            die( 'need $c' );
        }
    }
    else
    {
        die( 'need $b' );
    }
}
else
{
    die( 'need $a' );
}

As you can see, the actual operation here is the echo, and it’s nested three levels deep. If it were in a class method, then it’s still three levels deep, but it’s tabbed across the screen five times already.

Imagine this was a far more complicated piece of code and and it was a good four or five “screenfuls” it starts getting a bit challenging keeping track of all the different levels of branches and where you are in them. You end up doing a lot of scrolling up and down to remind yourself.

Why not adopt a “return-early” coding style instead? You check for what would stop your code working as opposed to what it needs to work. The above example has been rewritten to return-early style instead and now the echo statement isn’t in any branches at all.

if ( !isset( $a ) )
{
    die( 'need $a' );
}

if ( !isset( $b ) )
{
    die( 'need $b' );
}

if ( !isset( $c ) )
{
    die( 'need $c' );
}

echo $a, ' - ', $b, ' - ', $c;

What you have now is three small branches or chunks of code on screen that can be modified and rearranged a lot easier than they would be if you had multiple nested branches.

I guess the moral of the story is to keep as much of your code as far left of the screen as you can.

Leave a Comment :, more...

You’re Doing It Wrong: Tabs

by Daniel on Nov.15, 2008, under Programming, Soapbox

This is likely the first in a series of You’re Doing It Wrong posts that I have rattling around my head.

Today, using Tabs to indent code. As i’m sure we’re all aware, the wonderful thing about tabs is that you can set how wide you want them to be displayed. I like a tab width of four, others like two, some like eight. And all the time that we indent our code with tabs then everyone can be catered for when it comes to their whitespace preferences.

Like most things, there is a dark side. When you start to turn to the dark side, you start use tab characters to create structured whitespace in amongst your code, that being non-tab characters, followed by tabs, followed by further non-tabs.

It’s a bit difficult for me to demonstrate this adequately on the bloggenator, so take a look at this example.

And now your work of art is ruined. Editors are trying to be too clever for our own good. Yeah it’s great if i’m the only one who’s working on the code, but what if someone else joins me on it? Or someone else takes over the project? Yeah sure you could say “their problem, not mine”, but it’s not very considerate is it?

By sticking to a rule of only using tabs at the beginning of a line, your code will have the same presentational layout whatever editor I, or anyone else want to use to view it.

When someone else eventually takes over my code, and it’ll happen, the last thing I want them to do is throw it all out. If they see bits of lines of code all over the place because they use eight spaces and you use two, it looks a mess. That gives them a bad impression of you’re coding competence, unjustly so, since if they tweaked their editor it would look good again.

Personally I use a variable-width font at work and a fixed width font at home. Don’t ask me why I do, i don’t know. The editor background on my work editor is white, at home it’s black! How whacky is THAT?!

Leave a Comment :, more...

WP Theme that doesn’t display comments???

by Daniel on Nov.12, 2008, under Blog

I was using the Blass2 theme from the WordPress theme site, and apparently it doesn’t display comments properly… what is the point in that???Now I need to find a new theme.

Leave a Comment more...

Hampshire Web Developers - Introduction

by Daniel on Nov.12, 2008, under Blog

Hey all! Just a note to say, I’m a Web Developer and I live in Hampshire. I work with Rich and Dave, who told me about the idea. So, hello.Currently I work mainly with PHP5, MySQL with a view to playing with PostgresSQL. I’ve been using a lot of XSLT lately, XML rocks. Also on my to-do list is some jabber fun, not sure when I’m going to get on my arse and do something about it though. I’m very interested code structure, how data moves around different systems and the separate layers within a system. I’m someone who sees their code as their art, and it is art. Or at least it should be. Haven’t really been up to much of note lately, as you can probably see. But I do have a bunch of LEDs and batteries on standby. My solar panel arrived the other day, still waiting on the charge controller. As I write this though the thought dawns on me that I haven’t really chosen the right season for that, never mind. But expect stuff along those lines along with the possible exception. Anyway, I’ll be off, need to perform cat-maintenance…

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!