Archive

Archive for November, 2008

November 26th, 2008 3 comments

So I noticed this the other day.

$results = getResultsFromSomething();

$tempA = $results['A'];
$tempB = $results['B'];

$RealA = $tempA;
$RealB = $tempB;

...

Where the remainder of the function never used $tempA, or $tempB again. What was the point of writing $tempA and $tempB in the first place?

Here’s a tip to would be programmers. Flow chart out your code no matter how simple it is, it’ll save you time in the long run. Also allows other people to see your process flow.

I flow chart out process on paper or a white board… until I’m happy with it I do not start programming. I start once I have a blueprint. With this blue print, I can write/design unit tests for each step. By looking at this process flow chart, you should notice what helper functions you may need, what sections could be fine tuned. You may glean many things from just looking at your flow chart.

Remember, the process flow does not need to contain code, just the ideas.

Categories: General Programming Tags:

Give useful names to your variables/functions/methods/classes/parameters

November 24th, 2008 No comments

$count, $i, $j, $k are kind of standard names for variables.  Even $x, $y, $z for very simple mathematical problems.

I’ve seen some one do this

$ee = $this->doSomethingReturnArray();
foreach ($ee as $ff)
{
    $kk = dosomethingelse($ff);
    $temp[] = $kk
}

What significance does $ee, $ff, $kk, or even $temp have, without reading the whole function?  This is unmaintainable.

function name like:

getFeedObjectID()
{
    return (integer) $this->FeedObject['id'];
}

setPublisherID($integer)
// or
setPublisherID(integer $ID) // are good.

Stating the type helps phpDocs/javaDocs compliant editors, such as eclipse auto-generate documentation for you.  More about that in a later post. — hint for eclipse PDT, above the function name type “/**” and then enter to start a phpDocs block.

Categories: General Programming Tags:

Programmer Rap Battle Via Chat

November 24th, 2008 No comments

This is super geek: via digg to bash

gah… please forgive me for posting this.

Categories: random Tags:

html doctypes

November 22nd, 2008 No comments

Something I learned from a frontend guru recently… always! always! always! have the doctype at the top of you web pages.

I was trying to debug a page that someone else wrote.  The problem was that an “onclick” html event would not trigger. I was sure that the javascript syntax was correct.  The guru’s first idea was that the doctype was set wrong. Sure enough, he was right, the original coder slid the doctype somewhere randomly in the code, way below the first line of the html code.

I’m not exactly looking forward to cleaning up all that crappy code.

Categories: HTML Tags:

Poor logic

November 21st, 2008 No comments

Golden tidbit I found while doing some maintenance:

$count = count($some_array);

if($count)
{
    if($count > 1)
    {
        $val = 'PluralText';
    }
    elseif ($count == 1)
    {
        $val = 'SingularText';
    }
    elseif ($language == 'fr')
    {
        $val = 'PluralText';
    }
    else
    {
        $val = 'SingularText';
    }
}
else
{
   $val = 'None found';
}

could have been just written as

if ( $count = count( $some_array ) )
{
    $val = ( $count > 1 ) ? 'PluralText' : 'SingularText';
}
else
{
    $val = 'None found';
}

The reason in this example, the first section language code would never be reached.

The program would never reach the “$language” if condition. Allowing us to remove that extra code.

Simplifying the code to the point where it’s easily readable. Some may say that ternary statements are cryptic to read. I agree for the most poart. But when setting a simple value… say setting up initial values.

$value =
            (empty($this->getRequestParameter('some_random_parameter'))
       ?
            null
       :
            $this->getRequestParameter('some_random_parameter');

is better then writing

if (!empty($this->getRequestParameter('some_random_parameter'))
{
    $value = $this->getRequestParameter('some_random_parameter');
}
else
{
    $value = null;
}
Categories: PHP Tags:

Why didn’t the programmer run their program?

November 21st, 2008 No comments

The error is simply this

mysql_quety(...);

If the programmer originally check their code by just running the script, the php parser would have immediately stated that it does not know the function “mysql_quety()”.  I’m surprised that this passed the programmers initial inspection.

This leads to the question in the title…

Categories: PHP Tags: ,

Our first tidbit was found by Sean!

November 21st, 2008 No comments

in our code…

<?php

$var = doQuery($something);

however, “doQuery” was never defined.  The original programmer never even ran the program after writing it.

Moral of the story, test your code at least by running it.

Categories: PHP Tags: ,