Keith Devens .com |
Sunday, October 12, 2008 | ![]() |
| When your enemy is making a very serious mistake, don't be impolite and disturb him. – Napoleon Bonaparte (allegedly) | ||
|
| ← Why do Wiki RSS Feeds Suck? (by Jeremy Zawodny) | Wiki spam → |

Richard@Home (http://richardathome.no-ip.com) wrote:
Justin wrote:
You don't use && instead of and?
Keith (http://keithdevens.com/) wrote:
Concise code is good, but its definitely lost some readability :-(
See, to me I think it's gained readability. For the first one I thought that there was all this code for me to understand, so I never really looked at it. Now it's down to essentially two expressions for me to have to comprehend. I think that's much easier.
You don't use && instead of and?
The only language I use && in are languages that don't support and. Why, do you prefer && in all cases?
Ben (http://www.trollscript.de/blog) wrote:
"&&" and "and" have different operator precedence in PHP. This fact can bite you && be very difficult to track down. Look at this little snippet and compare the different output:
$a = true;
$b = false;
$c = $a and $b;
$d = $a && $b;
print "c: $c\n";
print "d: $d\n";
I never use the "and" operator in PHP due to this nasty circumstance. It's too easy to forget, so I stick with the one that produces the obvious result.
Regarding your concise code: It is of course a question of personal preferences. I do agree with Richard that Natalie's version is more readable and easier to comprehend. Personally, I favor consistency more than conciseness, so I always use braces after if or while statements, and use whitespace between operators too make it... more readable, duh. 
You did not only make the code more concise, you also added more functionality. I particularly like the decision to send a second time parameter into the function to serve as a reference point for the "now" time. That allows you too compute time differences with future dates as well, and makes your version more versatile to use. Thumbs up for that.
There is a small glitch in your code however: If I call it like this
time_diff(time(), time() - 5);
your code produces an error notice, and the return value is not that meaningful. Natalie's version can cope with this time, and produces "0 minutes" as the return value. I'd like to provide a fix, but my first attempt screwed even more up, and I'm supposed to be working right now instead. 
Keith (http://keithdevens.com/) wrote:
It's too easy to forget, so I stick with the one that produces the obvious result.
Oh, whenever I assign the result of a logical expression to a variable I always put it in parens. So, it's funny -- we've chosen different consistencies.
However, and is the lower precedence one, so that's usually what you want in a condition.
That allows you too compute time differences with future dates as well, and makes your version more versatile to use.
Thanks for noticing
(The arguments can also be in either order, which is nice). I originally started modifying the code for that very reason -- I wanted it to handle dates in the future for my new "countdowns" section on the right. Though after trying it, I decided I'd rather have the countdown just count in days. Also, not that it's terribly important, but this version is more efficient as well -- you don't call time() on each iteration, there are only two arrays vs 7, you don't have multiple nested array accesses in the loops, and it uses fewer temporaries.
There is a small glitch in your code
Thanks for pointing out my bug. It seems my translation of the code wasn't exact if the interval is < 60 seconds. The easiest way to handle that case was to add if($since < 60) return '< 1 minute'; as a special case. I like that better too. While the original code produced '0 minutes', this now produces "< 1 minute" in that case.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):
Generated in about 0.268s.
(Used 8 db queries)

Concise code is good, but its definitely lost some readability :-(