Archive

Archive for the ‘General Programming’ Category

Check list

April 29th, 2010 No comments

[are/did] you:
1.) backed up the file
2.) backed up the RIGHT file
3.) editing the right file
4.) editing the right file on the right server/environment
5.) are you viewing cached data, or live?
6.) does the input/output validate?
7.) did you remove all exit/die statements?

If someone else looked at it, how many WTF per minute will there be?

Categories: General Programming Tags:

Reusing a edit/create form

March 5th, 2010 No comments

As a web developer, you have to deal with web forms. Why would you make a new form, and an edit form, which is basically the same.

How to handle this would be to use this design pattern:

1.) load the get item to load, if it exists. Lets call it $dataObject
2.) if $dataObject is empty/does not exist, start a new dataObject. Lets call it $dataObject
3.) dataObject is now instantiated.
4.) Do what needs to be done — for example “title” of the page, being “editing XXX”, or “new dataObject”. Whatever frontend reflections need to happen, based on if it’s new object or editing an old object.
5.) populate the form, accordingly. If the object is blank, it’ll just be populated with the default values.

6.) onSubmit()… check to see if an ID value was submitted, and the appropriate security checks, and deal accordingly. If there was no ID value, start a new dataObject, to be populated with the data submitted.

7.) $dataObject->save();

The above described is how symfony admin generator handles form reuse. While the admin generator is useful for most cases, sometimes you need to just do “more” customizations, than using the generator.

For example, you need to handle a collection of both NEW and Prior saved objects… to do batch edits, it’s easier to do it manually, instead of using the symfony-admin-generator.

When Loops Attack

August 17th, 2009 1 comment

Let’s say you have an array of values, and you want to loop through those values. You might do something like this:


$values = array(...);

foreach ($values as $value) { ... }

Simple, right? So simple, in fact, that I can’t figure out why I keep seeing this instead:


$tmp = array(...);

foreach ($tmp as $t) {

$values[$t] = 1;

}

foreach (array_keys($values) as $value) { ... }

This takes the values of one array and uses them as the keys for a second array, but then pulls out the array keys and operates on them alone, disregarding the value! It’s not an isolated incident, either, as it’s come up in numerous spots in the code. If anyone can think of why it would be done this way, please let me know because I’ve got nothing.

Categories: General Programming, PHP Tags:

redundant code redundant

August 12th, 2009 1 comment
function ifFileExists($file)
{
if(!file_exists($file))
{
return 0;
}
else
{
return 1;
}
}

function isWritable($file)
{
if(!is_writable($file))
{
return 0;
}
else
{
return 1;
}
}
Categories: General Programming, PHP Tags:

Excellent post on programmer mistakes

February 8th, 2009 No comments

Great posting of stupid things programmers do… The * stupidest things I’ve done in my programming job

In all honesty, I think most programmers should make these kinds of mistakes on their own, and then learn from them.

One that does not understand the wrong, cannot become better.

To extend the post, specifically for PHP:

1. ORM

One should use Doctrine, or propel.  I personally favour Doctrine.

2. EAV

Doesn’t need to be PHP specific.

3. Database Access

PDO with Doctrine, can’t really go wrong…

4. IDE

Use an PHP IDE, there are lots out there now.

Eclipse PDT, NetBeans PHP, PHPEditor, Aptana… etc…

I currently favour NetBeans.  I have tried Eclipse PDT, Aptana, NetBeans… and I’m currently using NetBeans, and loving it.

5. Transactions

Just use them.  This reminds me to clean up, the data injector to use  transactions.  I know I can… I don’t know why I didn’t.

6. Prepared Statements

Prepared statements in PDO, or subsequently Doctrine, is trivial.

PDO example:

<?php
// the following statements has question marks inside the query...
// meaning, that those will be replaced with values later.  Think of the command as a combination of sprintf, and mysql_real_escape_string, together... but smarter.

$query_string = 'select * from tableA where id = ? and column_a = ?';

$this->prepared_statement = $this->DBH->prepare($query_string);

$this->prepared_statement->execute(array(123, "some random value, but will auto quote everythign for me even if it has \" and ' inside the string"));
$results  = $this->prepared_statement->fetch();  // or
$results2 = $this->prepared_statement->fetchAll();

Doctrine is an ORM, so if you’re using Doctrine, basically everything is prepared for you.

7. Business Logic

LOL, sorry… this one makes me laugh, as code at work has this type of “design” (or lack of) in legacy code.  This legacy code is less than a year old.  Thank you Botched Code!

Categories: General Programming Tags:

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: