Shamelessly promoting PHP, Myself, and my services
2 Nov
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.
11 Responses for "Less code vs Good Code"
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?
The real winner in his list is point 6. That cracked me up!
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.
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…
Oh people, the ternary operator can of course help to really increase code readability.
But you should avoid cascading them.
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
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.
[...] out Vidyut Luther’s response to Alex’s comments too. [...]
>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:
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…
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.
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…
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.
Leave a reply