KBD

Keith Devens .com

Saturday, October 11, 2008 Flag waving
Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will... – Eric S. Raymond

Archive: July 23, 2004

← July 22, 2004July 25, 2004 →

Daily link icon Friday, July 23, 2004

Mozilla adds document.all support

Mozilla Adds Undetectable document.all Support:

Mozilla builds starting tomorrow will now support an undetectable version of document.all. This will help with sites that blindly use document.all in DHTML scripts.

Excellent, and clever. Also see IE Emu.

Stupid programmer tricks, part 002 (Elimination of else blocks, or highlighting the "main line")

When writing a function it's important to highlight the "main line" of the function if you can. Many times you'll have a "guard" at the beginning of the function to test for appropriate values, or to check for the existence of a file or enough disk space, or whatever. Here's an example from some of my own code I just modified. The original code was something like:

<?php
function & loadSettings($settings_path){
    global 
$KISML;
    
$temp = array();
    if(!
file_exists($settings_path)){
        
header('Content-type: text/plain');
        die(
"Settings file not found!");
    }else{
        
$f fopen($settings_path'rb') or die("Couldn't open settings file");
        
$temp = &$KISML->load($f);
        
fclose($f);
    }
    return 
$temp;
}
?>

By removing the else block, you make it clearer that the beginning if clause is simply a "guard" and not part of the logic of your code. You therefore highlight the "main line".

<?php
function & loadSettings($settings_path){
    global 
$KISML;
    if(!
file_exists($settings_path)){
        
header('Content-type: text/plain');
        die(
"Settings file not found!");
    }
    
$f fopen($settings_path'rb') or die("Couldn't open settings file");
    
$temp = &$kisml->load($f);
    
fclose($f);
    return 
$temp;
}
?>

It's important to point out that there were things in the original function that didn't have to be there, such as the initial $temp = array() line and the returning of $temp outside of the else block. It's embarrassing that those things were there in the first place, but it makes my point stronger. Those superfluities were naturally eliminated when the else block was eliminated and the "main line" was highlighted.

Another example of eliminating else blocks and highlighting the main line is something like this (this isn't real code, I'm making it up on the spot, though it happens to use the function above):

<?php
function getSettings(){
    if(
foo){
        
$settings loadSettings();
        
#do other stuff
    
}else{
        
$settings = array();
    }
    return 
$settings;
}
?>

It's better to highlight the main line and get rid of the else block:

<?php
function getSettings(){
    if(!
foo) return array();
    
$settings loadSettings();
    
#do other stuff
    
return $settings;
}
?>

Here's a final example from real code I'm working on now. This is the code that prints the timing data on the lower-right-hand corner of this page. getmicrotime() is the same as microtime(true) in PHP 5.

<?php
function timer($finish false){
    static 
$time;
    if(
$finish)
        echo 
'<p style="font-size: smaller">This page took about ',round(getmicrotime()-$time,4),' seconds to generate.</p>';
    else
        
$time getmicrotime();
}
?>

The utility of this example is questionable. It's very short, and (therefore) is one of the few cases where using this "trick" actually winds up making the code longer (just one line longer), but I think I'm going to stick with this updated version of the code because it's clearer:

<?php
function timer($finish false){
    static 
$time;
    if(
$finish){
        echo 
'<p style="font-size: smaller">This page took about ',round(getmicrotime()-$time,4),' seconds to generate.</p>';
        return;
    }
    
$time getmicrotime();
}
?>

(P.S. I really gotta get another language's syntax highlighter programmed in here so I can stop using PHP for all my code examples Smiley)

Update (July 26): Here's one more example from real code I'm going through right now. Before:

<?php
function setSize($field_name$size){
    if(isset(
$this->field_settings[$field_name])){
        
$this->field_settings[$field_name]['size'] = $size;
        return 
true;
    }else{
        return 
false;
    }
}
?>

After:

<?php
function setSize($field_name$size){
    if(!isset(
$this->field_settings[$field_name])) return false;
    
$this->field_settings[$field_name]['size'] = $size;
    return 
true;
}
?>

You save 3 lines and the condition at the top is more clearly a "guard".

And, here's one more. Before:

<?php
function disabled($bool NULL){
    if(isset(
$this->field_settings[$field_name])){
        if(
is_null($bool)){
            return 
$this->field_settings[$field_name]['disabled'];
        }else{
            
$this->field_settings[$field_name]['disabled'] = $bool;
            return 
true;
        }
    }
    return 
false;
}
?>

After:

<?php
function disabled($bool NULL){
    if(!isset(
$this->field_settings[$field_name])) return false;
    if(
is_null($bool)) return $this->field_settings[$field_name]['disabled'];
    
$this->field_settings[$field_name]['disabled'] = $bool;
    return 
true;
}
?>
← July 22, 2004July 25, 2004 →
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 7 posts

Recent comments XML

new⇒I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.051s.

(Used 7 db queries)

mobile phone