code efficiency vs code readability - which one is more important?

stndn

Golden Member
Mar 10, 2001
1,886
0
0
When coding (in any language), do you put more points in code efficiency or code readability / understandability?

This question came up as I am working on a piece of code (in PHP) involving foreac() and if() statements.

Attached are two really simplified codes.
First code is what I had at the beginning, where I have if() statement enclosing foreach()
The reason for this is to reduce the amount of if() comparisons with each loop.

Second code is the revised code, where the if() statement is inside foreach() loop.
This code is much easier to read and with less code repetition. But the downside is the if() grows up as the loop grows.

Which do you prefer?
Which is the better approach in your opinion?

I know for small amount of loops, second approach may be favorable.
But think about 100 loops that got executed each time a visitor refresh a page. It goes up quite fast with that.


-stndn.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Like most aspects of software development, "it depends".

A lot of the time readability helps with maintenance and has no impact on performance, so readability is much more important. Real world code can persist for years and you might look at sections of it only once every six months or less.

Even for areas where performance matters, in compiled code (pre or JIT) readability penalties are often optimized away by the compiler for you, so again it's better to be readable.

In those tiny parts of your code where performance matters and you think your code might lose efficiency if you make it too readable, if time permits you can try it both ways. If not you can go with efficiency but take the time to add decent comments to explain the difficult parts better.
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Originally posted by: DaveSimmons
Like most aspects of software development, "it depends".

A lot of the time readability helps with maintenance and has no impact on performance, so readability is much more important. Real world code can persist for years and you might look at sections of it only once every six months or less.

Even for areas where performance matters, in compiled code (pre or JIT) readability penalties are often optimized away by the compiler for you, so again it's better to be readable.

In those tiny parts of your code where performance matters and you think your code might lose efficiency if you make it too readable, if time permits you can try it both ways. If not you can go with efficiency but take the time to add decent comments to explain the difficult parts better.

He speaks the truth :)

And make sure your code is not sloppy!
 

RaiderJ

Diamond Member
Apr 29, 2001
7,582
1
76
It's almost impossible to code and have any idea of where the speed bottlenecks will be. This doesn't include major algorithmic mistakes.

Code for readability, ease of bug fixing.

Run tests, find out where bottlenecks are.

Fix bottlenecks.

Repeat until satisfied.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: RaiderJ
It's almost impossible to code and have any idea of where the speed bottlenecks will be. This doesn't include major algorithmic mistakes.

Code for readability, ease of bug fixing.

Run tests, find out where bottlenecks are.

Fix bottlenecks.

Repeat until the boss is satisfied.

Added bolded area for accuracy. A good S/W person always thinks that a little extra tinkering will make things even better.

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: DaveSimmons
Like most aspects of software development, "it depends".

A lot of the time readability helps with maintenance and has no impact on performance, so readability is much more important. Real world code can persist for years and you might look at sections of it only once every six months or less.

Even for areas where performance matters, in compiled code (pre or JIT) readability penalties are often optimized away by the compiler for you, so again it's better to be readable.

In those tiny parts of your code where performance matters and you think your code might lose efficiency if you make it too readable, if time permits you can try it both ways. If not you can go with efficiency but take the time to add decent comments to explain the difficult parts better.
I agree, along with what RaiderJ said: it's very difficult to actually know what 'optimizations' will make a non-trivial difference without actual runtime testing. In this case, I highly doubt that running a simple if like that 100 (or even 1000) times will be noticeable, and if it is, there's something very wrong with php. But, if you do have a problem with the page loading too slowly, test it out both ways and if the optimized version is better, just comment it well.

In this particular instance, I don't think that having the if/else outside the for loop is really the readability problem. Any programmer with half a brain can read that code. The problem is that you have 3-5 lines of code that are completely duplicated (depending on how you count the braces) without any indication that they need to be the same. I can see a programmer coming along, fixing a bug in the if block but not the else (ok, that sorta is the same thing...).
 

jman19

Lifer
Nov 3, 2000
11,225
664
126
It definitely depends on many factors... size of data input/overall run time, what the application is designed for, etc. It is essential for code to be readable in the workforce, that is for sure.
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Thank you all for the input. Really appreciate it.

DaveSimmons: Yes, the answer depends greatly on who get asked the question. Since the code I'm working on is PHP (and thus not compiled code), I don't think the interpreter will do any fancy optimization for me. So, from your comment, readability it is.

LoKe: The problem with documentation is that, programmers are the worst writers (okay, maybe not all - but most). Plus, when deadline hits, the first thing on the list is to get the project out and scramble to document later on. (perhaps during bug fest?)

Snapster: That's what we're trying to avoid: sloppy codes. But sloppy can mean at least two things: sloppy code development, or sloppy code documentation.. But I'm thinking you're referring to the first one.

RaiderJ: We don't have the luxury nor the time to run tests to find bottlenecks. Heck, we'd be happy to just get the project out as soon as we can, as we have tons other lining up in our todo list -(

EagleKeeper: Haven't you heard? the boss is never satisfied ,)
But I agree, *good* software developers always have things to add to make things go better

kamper: Actually, the example that was posted was mostly showing code repetition like you noticed. In the original code, it's a little code repetition with different operations/calculations performed depending on the if/else condition. What we're trying to avoid with the first code is the repeated comparions with each loops.

jman19: The readability is what hinted us to the second approach, sacrificing a little on the performance. We tried putting more comments on the first code, but the nature (length) of the contents inside each if/else statements just got out of hand quickly and we ended up with a few too many comments.


At the moment, we are still doing the development and we're not sure how many traffic we will be expecting to hit the page (of course, we're hoping for a lot of traffic).

As for testing which code runs faster - we would like to. but as with most real world projects:
1. we don't have the time and resources for code optimization / comparison / benchmark
2. according to the manager, "this project is long overdue"
3. "i don't care how it works. just give me the result"
4. boss (during afternoon meeting): "this thing is too slow/ugly/etc/. can you make this better by tomorrow morning?"

sigh... such is the life of software developers... -(

anyways, thank you all for the comments -)


-stndn.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Really, 95% of the time it's not even a close call. Readability is more important than efficiency. However, I would argue that there isn't an inverse relationship between the two. Most of the best aids to readability don't get compiled into the binary, i.e. comments and appropriate indenting, useful variable and function names, etc.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Rat
I code for speed. Comment for readability. If I feel like it.
Wait until you have to maintain it.

That will cause a quick attitude change


 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
i code for readability mostly

although my version of readability is different than others'

some people have a hard time reading dynamic variables and ternary operators
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I code for speed. Comment for readability. If I feel like it.

And all of those people that must look at your code later on, including yourself, will or already do hate you for it.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,002
126
My programs are seldom performance bound so I try to code for readibility.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
In PHP what happens if you define the variables outside of the for loop and then say print. Doesn't it replace the values at runtime still and not when you assign the variable? I thought it did... has been a while though.

Therefore wouldn't this be better?

 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
torpid: In your code above, PHP will spit out error message saying $vlKey and $vlValue is not defined.
Which is correct since the variables $vlKey and $vlValue are not created until we enter the foreach() loop.

Maybe you were thinking about variable variable?
Where the name of variable will be replaced by the value of a variable, creating a new variable?
(ummm... the sentence above really sounds silly)

$vlKey = "one";

print "Animal $$vlKey is $vlValue\n";

will be parsed into:

print "Animal $one is $vlValue\n";

which will be parsed to the appropriate contents of $one and $vlValue