OU blog

Personal Blogs

New blog post

Visible to anyone in the world

Rant time! (Wait. That's every time... Well, this time I'm actually typing it into my blog.) This is all about computer programming so if you don't know anything about computer programming you are officially off the hook! (Enjoy your day!)

Coding style guidelines are usually a matter of opinion. But here's one of my opinions, and in my opinion, it's not an opinion, it's a fact. smile

Apart from normal start-of-line indent, trying to align lines of code horizontally is always wrong.

Here are three examples. For each one I'll give WRONG then RIGHT.

WRONG:

$this->something = $something;
$this->x = $x;

Right:

$this->something = $something;
$this->x = $x;

WRONG:

$thing = array(
'value' => 1,
'anothervalue' => 2,
'more' => 3);

Right:

$thing = array(
'value' => 1,
'anothervalue' => 2,
'more' => 3);

WRONG:

$something->function1()->function2($variable, $anothervariable,
$yetanothervariable) {

Right:

$something->function1()->function2($variable, $anothervariable,
$yetanothervariable) {

etc.

The wrong examples are wrong because computer code is not carved in stone, nor even printed on paper. (When you're grave-carving or desktop-publishing, feel free to make things line up. When you're computer-programming, stop it! Now!) Unlike gravestones and newsprint, computer code changes.

Let's say I add a variable called $slightlylonger to the list of assignments. With the right code, this involves me adding one code line. With the wrong code, I have to add one code line, but now I have to modify 2 other code lines as well to make the stupid thing consistent.

Similarly if I change the name of one of the functions I would now have to go around changing all the following lines as well, where it wrapped to multiple lines. (The function example is also bad because horizontal space is usually at a premium, which is why you're having to wrap lines in the first place; when you wrap the line, you want to get a useful amount of increased space for adding more parameters in future. That doesn't happen if the wrap point is artificially shifted to way up near the end.)

Having to modify extra lines matters for two reasons: first, making the changes is annoying, and second, the more lines you touch then the more chance of a conflict in your version control system.

In addition, as I'm sure the newspaper designer would tell you, both these approaches are bad even from a layout and visual design perspective. They result in 'columns' that start every which way at random points. It's messy.

So, much better not to make it line up in the first place. If you disagree... well there's a word in bold in this post... let's leave it there smile

Rant over!

And no this wasn't directed at any particular developer, or even the Moodle project. Just, grr.

I'll try to write a blog post about something more specific to do with our Moodle developments next time. But this is better than nothing, right? smile

 

Permalink Add your comment
Share post

Comments

neil

New comment

I share your pain, but what can you do? At least it's not VB!?

n

VB is good

Neil: What do you mean is wrong with VB? It doesn't rely on arbitrary non-everyday symbols, hence is very quick to code in. Considerably quicker than C-style languages. Try it.

New comment

You argue that you have to do it yourself. Why so? Have the IDE do it for you! That's what it's here for. Also: you may use column edit mode to ease the effort. That's why we have it smile

New comment

@Tom

That's not the point - making the changes is easy, but it causes larger patches that are more likely to cause conflicts with other changes. In a list of 10 items, there are potentially going to be 10 modified lines rather than 1.

Just like how it's bad practice to make whitespace changes in a patch (which by the way makes IDE auto-format options useless unless the entire team has agreed on precisely which one to use, something which is kind of tricky for an open-source project), because that ends up changing lines that you didn't 'really' change, it's bad practice to write code in a style that actively requires you to change lines you didn't 'really' change.

As for whoever liked VB... well everyone's entitled to an opinion, but, er, no. smile I did a serious project in VB once, years back, because it had to integrate with Word. It was like trying to make a serious building project out of play-dough (assuming said play-dough was a mixture of toxid acids that burn skin on contact). [Yes the project was successfully completed in the end, but damn.] Significantly worse than PHP, which I currently work in, and that's saying something. smile

Luckily, nobody has to use VB any more because Microsoft invented C#, right?

New comment

tool. The wrong way is alot easier to read when you have alot of lines. You can pretty much stare at it and know exactly what's going on. The right way is a bitch to read. 14 years of programming and I prefer the "wrong" way.