Thursday, 10 September 2015

Rules and laws

Rules are good....sometimes. I'm a great believer in the flexibility of doing things...when it comes to layout there's a lot of time wasted on certain things like where curly braces should be and whether or not they should even be...

Let's take this little C++ example:

if ( a > b )
{
    a++;
}
b = b - a;

and now this example:

if ( a > b )

    a++;
b = b - a;

These are the same...until they are not. here's the same with code commented out.
if ( a > b )
{
//    a++;
}
b = b - a;

and now this example:

if ( a > b )

//    a++;
b = b - a;


These now do totally different things. I came across code a while back that was exactly like this and it was hard to tell what should have happened after the IF statement. I therefore always write the code the first way using the braces. A more senior (in title only, I have 10 years more experience) colleague of mine then went through my code and stripped them out because he didn't like them.

Some rules are good like curlys always. If you need speed then omit them as rules are made to be broken. Comments are free.

This brings me to the laws. I have 3 laws. They are straight forward and they are definitely not original in anyway and collectively I call them Brian's Laws...a bit like Asimov's laws of robotics...not really, I'm no Asimov.

Law 1. If it ain't broke, don't fix it.
Fairly self explanitory. Leave it alone unless it genuinely is broken. However....

Law 2. Simple is good.
There's often loads of ways to do stuff and the simplest often means easier to maintain and less likely to need fixing in future. Now, simple is not always possible but simpler can usually be achieved.

Law 3. Just because you can, doesn't mean you should
This needs explaining a little. Imagine some code that is really complicated and, with a bit of effort, can be simplified (Law 2) but it isn't broken (Law 1). Law 3 helps here. Just because you can simplify the code, it does not mean you should do it.  It doesn't have to be Law 1 and 2 in conflict. e.g. Just because you can have another coffee (after having 3) doesn't mean you should

These laws are a great guide to not just programming.

There's a secret law 4 I often keep secret as it's not quite as absolute as the other 3 and I work with a bunch of folks who fall into law 4 and it really holds true for a lot of them.

Law 4. Do not let non-software grads code
This is tricky. There are a few good non-s/w grads out there who actually are great at coding. However, I've met more than not who fall into law 4. A lot of folks have a post-grad in S/W development but I would say this - you cannot learn in a year what normally takes 4. And Java...really... I think learned (or dabbled) around 15 different languages through college and what that taught me was diversity.

Keep law 4 to yourself but always keep it in mind.

No comments:

Post a Comment