KBD

Keith Devens .com

Monday, October 6, 2008 Flag waving
Never ascribe to malice, that which can be explained by incompetence. – Napoleon Bonaparte
← mod_rewrite, SCRIPT_NAME, and PATH_INFOAwww.... →

Daily link icon Thursday, July 8, 2004

Stupid programmer tricks, part 001

If you ever find yourself writing something like the following:

<?php
if($foo == $bar){
    
$baz true;
}else{
    
$baz false;
}
?>

smack yourself upside the head and then change it to:

<?php
$baz 
= ($foo == $bar);
?>
← mod_rewrite, SCRIPT_NAME, and PATH_INFOAwww.... →

Comments XML gif

Sparticus (http://iamsparticus.co.uk) wrote:

I stick my hand up here and say 'I've done that'! Thanks.

∴ Sparticus | 8-Jul-2004 6:37am est | http://iamsparticus.co.uk | #4951

Elling wrote:

It depends...

I think, as long as you're not tight on resources, then code readability should be key. I often find myself writing things out instead of trying to compress it as much as I can.

Having readable code can be key when it comes to discovering bugs...

∴ Elling | 8-Jul-2004 7:44am est | #4952

Keith (http://keithdevens.com/) wrote:

The second one is more readable. It's just one line instead of 5, and your intent is immediately clear. Remember this great quote by Dijkstra:

If we wish to count lines of code, we should not regard them as lines produced but as lines spent.

Keith | 8-Jul-2004 10:35am est | http://keithdevens.com/ | #4956

Elling wrote:

Well... Dijkstra talks about algorithms when he says that. And then it makes sense. In an algorithm where time or CPU cycles is an issue, then it's clearly an advantage to spend as few CPU instructions as possible.

But in all other cases, I personally think readability should be key.

In your particular case, it could be that the two examples are of equal readability, or maybe in some cases people will find the second example more readable... I don't know.

My own policy when programming is to keep things simple; usually do only one operation per line, and keep things orderly and logically separated.

I sometimes work with code where the author has spent much time to cram as many operations as possible into one line. And often that makes the code more difficult to figure out. If there's one operation per line, or maybe two... then in my experience it usuallty adds to the readability of the code.

Just my two cents... Smiley

∴ Elling | 8-Jul-2004 11:09am est | #4958

horaciod (http://blog.codigophp.com) wrote:

another way to write this more " readable " for me

$baz = ($foo==$bar)?true:false ;

ya !!
is not perfect but cpu cycles are cheapest than programmer salary

∴ horaciod | 8-Jul-2004 6:09pm est | http://blog.codigophp.com | #4961

Scott Johnson of Feedster (http://www.feedster.com/blog/) wrote:

Hm.... I find that I'm in big disagreement here. Yes its shorter. Yes it may be more elegant but ... OY VEY.

The big question here that seems to be being ignored is what are your development staff like and will they get it? If the code will only be touched by you, do whatever you like. But unless that's either used universally so people always see it and know it, its going to be a maintenance problem.

Trust me I've had as many as 45 engineers as indirect reports (4 to 5 teams) and that one is going to be an issue.

Yes its one line but if you add this:

/*
Compare foo and bar and set appropriately

  • /

You just added 3 and I wouldn't feel comfortable with that line without it. Yeah that may be me but organizational life cycle of code is a really big deal. The person who follows in your footsteps isn't stupid -- but they aren't you either. Keep that in mind.

∴ Scott Johnson of Feedster | 8-Jul-2004 8:44pm est | http://www.feedster.com/blog/ | #4962

Aggelos wrote:

Reminds me of C. Smiley

∴ Aggelos | 9-Jul-2004 1:17am est | #4963

Keith (http://keithdevens.com/) wrote:

Yep. I use PHP syntax a lot because that gives me syntax highlighting Smiley I gotta stick in some other highlighters.

Keith | 9-Jul-2004 8:05am est | http://keithdevens.com/ | #4964

Sparticus (http://iamsparticus.co.uk) wrote:

For those who say the first is more readable: Comments. Comments add practically zero to processing speed, so the code is still faster, shorter and more elegant.

∴ Sparticus | 9-Jul-2004 10:14am est | http://iamsparticus.co.uk | #4965

Keith (http://keithdevens.com/) wrote:

I don't know why people are focusing on processing speed so much. This is such a micro-optimization that discussion of efficiency seems pretty silly.

The main plus for me is that it's much shorter and more elegant. It may not seem like this idiom is clear given the example with $foo, $bar, and $baz above. But here's a really simple example that shows that it can be easily readable:

<?php
$logged_in 
= ($user == $_REQUEST['user'] and $pass == $_REQUEST['pass']);
?>

One of the ways I think about it is that it's assigning the value (which is boolean) of that expression. It's exactly the same as if you'd said "$baz = foo($bar)" but it's with a logical expression rather than a function call.

Keith | 9-Jul-2004 10:48am est | http://keithdevens.com/ | #4966

Elling wrote:

Agree.... There are not any big problems with these two examples that you post. I just don't think that the short versions are that much to be prefered over the long versions.

I think elegance is nice. But I also think that elegance often gets in conflict with readability.

If faced with the choice between a multi-line boolean expression and a couple of nested if blocks, I usually go with the nested if blocks.

But... this probably is about taste too... So, again, I'm just saying what I personally think is the better approach.

∴ Elling | 9-Jul-2004 2:45pm est | #4971

Alan Green (http://www.cardboard.nu) wrote:

∴ Alan Green | 20-Jul-2004 5:47am est | http://www.cardboard.nu | #5042

Dave Edelhart (http://www.manateebay.com) wrote:

Sometimes its a good idea to USE cpu cycles, so that you have something to optimise later on. Also, why pass up an opportunity to write (php5 dependant) object code?

<?php

class are_they_equal
{


public function 
__construct($pVar_1$pVar_2)
{
$this->set_var_1($pVar_1);
$this->set_var_2($pVar_2);
}

/************ PROPERTIES *****************/

private $loc_var1 null;

public function 
get_var_1(){ return $this->loc_var_1;}

public function 
set_var_1($pValue){$this->loc_var_1 $pValue; }


private 
$loc_var2 null;

public function 
get_var_2(){ return $this->loc_var_2;}

public function 
set_var_2($pValue){$this->loc_var_2 $pValue; }

/**************** METHODS *******************/

const ARE_EQUAL1;
const 
ARENT_EQUAL2;

public function 
are_equal()
{
if (
$this->get_var_1() == $this->get_var_2()
{
  return 
self::ARE_EQUAL;
}
elseif (!(
$this->get_var_1() == $this->get_var_2()))
{
  return 
self::ARENT_EQUAL;
}
else
{
  return 
'A GIANT HOLE IN SPACE TIME HAS RENDERED
 BOOLEAN LOGIC INVALID. SELL YOUR TECH STOCKS NOW.'
;
}
}

public static function 
test_equality($pVar_1$pVar_2)

{
$tester = new are_they_equal($pVar_1$pVar_2);

$out $tester->are_equal();
unset(
$tester);
return 
$out;
}
}
?>
∴ Dave Edelhart | 14-Aug-2006 6:13pm est | http://www.manateebay.com | #9597

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 6 posts

Recent comments XML

new⇒Girls, please don't get breast implants

Makes me sick to hear normal sized​women whining about being small. B​cup i...

Rachel: Oct 5, 7:42pm

obout inc - ASP.NET controls

I like there components. I've got​it to work locally on my pc.​However I'm ...

Jeff: Oct 2, 4:43pm

Dumb substring behavior in C# (and Java)

Yes, the Substring function is not​helpful when you hit the length​problem,...

Mike Irving: Oct 2, 7:56am

YouTube - Burning Down The House: What Caused Our Economic Crisis?

> Please save another copy of the​"Burning Down The House" video as​the ori...

Keith: Sep 30, 11:05am

Johnny Walker Blue Label

I bought a 2 finger glass for 60​bucks about 5 minutes before my​wedding. I...

Ty: Sep 30, 9:52am

More on the bailout

I figured they wanted about a buck​or so from every man, woman and​child on...

Peggy McGilligan: Sep 30, 12:20am

Generated in about 0.121s.

(Used 8 db queries)

mobile phone