Less code vs Good Code

by vid luther on November 2, 2007

Alex Netkachov has an interesting post on his blog, giving tips on writing less code. It’s an interesting post, because this is something is very subjective.

First, you want to find out why you want to write less code, less code for the sake of writing less code is dumb, if you really don’t want to write code, then that’s a completely different thing, hire me, or the countless other guys who like writing code :) .

What are the benefits of writing less code? Does it make the code run faster? Does it help you save disk space? I’m not really sure.

Tip #2 says use ternary operators.. this is a pet peeve of mine, I really can’t find a good reason why these operators still exist, or are used in high level languages like PHP, but I personally think the use of ternary operators in 99% of the situations I’ve seen, is just the programmer thinking he’s being clever. Eventually ternary operators make code less readable, but again that is subjective, not authoritative. I find lots of things unreadable, but they’ve made the best seller lists, so.. what do I know?.

Tip #3, I really don’t see a benefit of using for vs while, again this is subjective, I think it boils down to what one puts more priority on, one line of code, or more readable code.

He does make the comment about writing better documentation in your code, but I really don’t see why the two have to exclusive..

I think S0enke put it best in the comments.. “Writing less code is not about writing less lines. It’s about avoiding copy-and-paste programming and no formatting-paradim but more a question of programming skills.”

If your code is executing faster because you have fewer lines of code, than you did in version 1, you have a bigger problem than you realize.

{ 1 trackback }

developercast.com » Alex Netkachov’s Blog: 6 PHP coding tips to write less code
November 5, 2007 at 8:48 am

{ 11 comments… read them below or add one }

1 Harald November 3, 2007 at 4:10 am

in my opinion code should be self-explaining. you have the API documentation to explain the interfaces and function calls, but the rest of the code should really be self-explaining.

i still think the ternary operators is often very useful – not to make the software have less code but to make it more elegant.

to make it short:

#1 logs ugly and is hard to write. some editors might get into trouble as well, when they look for {} for code-folding.

#4 is the best thing you can do to make your code unreadable and hard to understand.
ah – was the post about obfuscating or writing less code?

[Reply]

2 MonkeyT November 3, 2007 at 5:52 am

The real winner in his list is point 6. That cracked me up!

[Reply]

3 Jim November 3, 2007 at 6:32 am

It is a very subjective topic. For me less code generally means a more elegant solution with less scope for errors, and less to read and understand. This allows me to move on to writing other code. It’s nothing to do with speed or disk space (is that even a problem these days?), it’s to do with making the most out of my time. Not that I’m advocating writing optimisation for the sake of it.

I can’t agree with you on the ternary operators, and I’m glad that the majority of languages have them. I find them very readable, and a natural extension to a programming language.

[Reply]

4 Mgccl November 3, 2007 at 11:50 am

Less code for Javascript might be reasonable because it have be sent over on the internet constantly. But less code for PHP? where does that come from? I believe understanding the code is most important part for most language…

[Reply]

5 Me November 3, 2007 at 3:09 pm

Oh people, the ternary operator can of course help to really increase code readability.
But you should avoid cascading them.

[Reply]

6 Philip November 3, 2007 at 6:18 pm

I find it interesting that ternary operators are hardly ever referenced in comments at PHP.net. Indeed chapter 16 “Control Structures” of the manual hardly mentions them either.

Anyhow, I don’t use them, although they do look attractive :P IMHO, PHP is a relatively easy language to gets to grips with I think that the wide spread use of ternary operators would only increase the barrier to entry.

[Reply]

7 metapundit November 5, 2007 at 10:20 am

>Less code for Javascript might be reasonable because it have be sent over on the internet constantly. But less code for PHP? where does that come from?

Obviously no Paul Graham fans here:

>It seems to me that succinctness is what programming languages are for. Computers would be just as happy to be told what to do directly in machine language. I think that the main reason we take the trouble to develop high-level languages is to get leverage, so that we can say (and more importantly, think) in 10 lines of a high-level language what would require 1000 lines of machine language. In other words, the main point of high-level languages is to make source code smaller.

>If smaller source code is the purpose of high-level languages, and the power of something is how well it achieves its purpose, then the measure of the power of a programming language is how small it makes your programs.

I focus on succinctness a great deal in my programming – particularly when refactoring. Admittedly – this is mostly at the algorithm level rather than the syntactical statement level. The reason is that compact expressions of code are more easily modified, debugged, and understood. Look at the if/else vs strategy pattern near the bottom of the essay at http://metapundit.net/sections/blog/code_smells_and_design_principles for an example – and a link to Graham’s “Succinctness is Power” essay.

Syntactical completeness is still a virtue, though, especially when it more accurately describes your intentions. The ternary operator expresses exactly: I want to do one of two things based on a value. When I see it in code I understand much quicker what is going on than if the same code took 8 lines of keywords and brackets to say exactly the same thing. I don’t agree with all of Alex’s points – but writing less code (ie getting more done with less work) ought to be a goal of any professional programmer…

[Reply]

8 Vidyut Luther November 5, 2007 at 10:27 am

Meta,
I agree with you on the part of writing succint algorithms, and that’s the point I was trying to make. You shouldn’t focus on what’s the shortest line or statement to do something, but what is the most efficient way of addressing a problem. With high level languages, 4 lines of a condition structure, vs one line of ternary isn’t going to make the code run faster, the same condition needs to be met, the same function needs to run, or same value needs to be set.. but possibly eliminating the condition statement, would be a better use of the brain, than scouring the manual and finding a shorter way to write the code.

[Reply]

9 metapundit November 5, 2007 at 11:57 am

Vidyut -
I think we’re talking at cross purposes – I’m not concerned with succinctness for the benefit of the machine – I’m concerned with succinctness for the benefit of ME! It’s true that aren’tt any efficiency (from the point of execution speed) benefits to writing a ternary operator versus writing an if/else statement. And I’ll also concede that if people don’t know the syntax the ternary can be confusing to read. But – it is much easier (I argue from my subjective vantage point) for me to understand equivalent code written with a ternary operator than with and if/else statement. Partly that’s because of compactness – On the first line of code I read I can ascertain that we are choosing one of two actions based on a single statement. When I read the first line of an if/else structure I don’t know how many elseifs there might be, I don’t know if we’re operating on a single condition. I have to read more lines of code to understand the intention of the program…

To me that makes code written using the ternary operator more maintainable – partly because it has a more specific meaning and partly because it is more compact. It doesn’t have anything to do with execution efficiency (CPU’s are cheap) – it has to do with programmer efficiency (coders are expensive).

To get off the ternary operator – take another example: I’ve seen people argue over whether or not there are infinitesimal performance differences between using single quoted strings (non – interpolated) and double quoted strings (interpolated). My answer? Who cares which one is .005% faster? Would you rather read code like:

$foo = 'Hello, my name is ' . $fname . ' ' . $lname . "\nOccupation: " . $occupation;

or:

$foo = "Hello, my name is $fname $lname \nOccupation: $occupation";

Again – it’s subjective but the second seems clearly easier to read and write to me. It doesn’t have 5 concatenation operators (odds of forgetting a dot the first time I write this line of code: 50%, odds of forgetting a dot if I go back and add a variable: 75%), it doesn’t have to concatenate to get the space between the names. It’s shorter – not just in characters but in program elements (same number of variables, 5 less operators, 4 less quotes… It’s a better use of my brain to write shorter more expressive source code that is easier to read, write, and understand. And back to the efficiency thing – the easier code is to read and write the more I will get done. That’s worth looking up a bit of syntax in a manual once in a while…

[Reply]

10 null December 11, 2007 at 10:30 am

String concatenation produces less opcodes than having an interpolated string. Therefore it scales better. It also uses a little less memory, and does less memory reallocations at the interpreter level.

[Reply]

11 Ramses Paiva March 27, 2009 at 5:12 am

I know this post is kind of old, but I’d like to add:

echo (1 > 0) ? 'One rules.' : 'When will zero rule?';

Or:

if (1 > 0) {
echo 'One rules.';
} else {
echo 'When will zero rule?';
}

Another situation:

return (1 > 0) ? 'ONE' : 'ZERO';

Or:

if (1 > 0) {
return 'ONE';
} else {
return 'ZERO';
}

The thing is that there’s no coding standard guidelines where they say not to use ternary operators or when to use, being much more something of personal matter than anything else. If you rather write your code with ternary operators, fantastic, if you rather use if/else conditions, great!
Nobody will kill for doing that. Ternary operators are very handy for dealing with very simple situations like explicited above, but they can become a headache for solving more complex problems, like when you have more than one condition to test.
Imagine the situation:

echo (1 > 0)
? (2 > 1)
? (3 > 2)
? (4 > 3)
? 'Four is bigger than 3.'
: 'World is getting crazy!'
: "I don't know math anymore!"
: 'I think I should go back to school.'
: 'Why the hell I am programming???';

The number of question marks will be loaded straight to your brain and the confusion is made! The criteria for choosing which one to use is up to you, but make sure that anyone can read your code without getting their brains blowed up.
There will be sometimes where the if/else will be much more elegant (like in the case above) and in these cases it will be much more readable.
But I don’t see any problem on using echo (1 > 0) ? 'ONE' : 'ZERO'; either.

Regarding single or double quotes, the thing is that when you’re executing a single string with 0.005% of increased performance, it doesn’t make any sense using or another.
But when you’re using a complex application, with several files, objects and thousands of concurrent connections to this application, then it will really affect the performance of your application.

That’s my humble opinion. :-)

Ramses

[Reply]

Leave a Comment