KBD

Keith Devens .com

Saturday, March 20, 2010 Flag waving
Sometimes a man with too broad a perspective reveals himself as having no real perspective at all. A man who... – Steven Den Beste

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 →
March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



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

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.044s.

(Used 7 db queries)