Rubin Shrestha

I specialise in web programming & support!

Decoupling code PDF  | Print |  E-mail

Everyone’s had to deal with it.  The big, nasty project, where everything links to everything, and anything you do can have seemingly random effects in apparently unrelated areas.

So, let’s look at some ways to avoid that.

But first, let’s define what we mean by “coupled.”  Two pieces of code (classes, modules, whatever) are coupled if they have to know how each other work in order to function.  Couplings can exist one-way, or two-way.  Some coupling is good – programs probably wouldn’t do much without them.

Where the sinister evil starts to sneak in is unnecessary coupling, and unnecessary intimacy.

To start with, let’s look at a piece of code:

public class SomeClass
{
    public void DoSomethingToCharacter(Character c)
    {
        string username = c.Connection.User.Name;
    }
}

Okay, so it doesn’t do much at this point.  But it’s a pretty good start as to where there might be a problem.

And what problem is that?

Well, it’s pretty apparent that this method needs to do something with the character’s name.  But to get that information, we have to go through both the connection and user properties.

This opens us up to problems.  First, it means we have a physical dependency on those classes, which can create compilation time issues.

Secondly, it means that to get the data we want, we have to know quite a bit about the class we’re using.  We have to know that it has a connection object, and that that object has a user object, and that object has a name.  I don’t want to know that much.  I just want the user name from the character!

The worst problem is that we open ourselves up to build breaks every time we use this access pattern.  If any of those classes change, we break.  And probably in lots and lots of places.

Let’s look at an alternate:

public class SomeClass
{
public void DoSomethingToCharacter(Character c)
{
string username = c.UserName;
}
}

Ah, yes.  Isn’t that pretty?  Now our code only cares about the character we class.  We don’t know how many layers of ugliness are going on underneath, and we don’t need to.  Even better, if one of them breaks, the damage is going to be limited to inside the character class itself – which means an obvious place to fix, instead of hunting down those chains of objects all over your codebase.

Oh, and for what it’s worth – those chains of objects have a name.  They’re called “train wrecks,” after all of those objects smashed into each other.  It’s not a term of endearment.

And what I’ve been describing has a name as well – it’s called the Law of Demeter.  It basically states that a given method can only use methods internal to objects it owns, on objects that are passed in, or on objects it directly creates.

Source : http://www.wrong.net/2007/12/02/decoupling-code/

 
PDF  | Print |  E-mail

Security

"Unfortunately, even with register_globals turned off - anyone can easily inject their own $GET and $POST variables into PHP scripts"

Yes, that's true, but they are still limited to only those variables that occur as $_GET/$_POST within the script. By looping, you are making your script that every *possible* variable has to be validated. This significantly raises the chance of something slipping through that you don't notice.

"Writing extra code to inspect/modify/correct sensitive variables can add a significant amount of work to any script, but it is much safer than relying on "register_globals" to cover your ass."

Yes, but that's precisely the point. The longest time is spent debugging and writing this extra code, and that's where avoiding this loop saves you time. Search your script top to bottom and look for
$_POST
$_GET
etc.
and make sure that each occurence is harmless.
$age = intval($_POST['age']);

With your loop you have to check every variable everywhere, and you can't search either. A script might have dozens of variables but only 2 or 3 occurences of $_POST, $_GET.

 
PDF  | Print |  E-mail

Single Quotes versus Double Quotes

Any time you put something in "double" quotes, you are asking PHP to check that content for a variable. So even though the following lines do not contain variables within the double quotes, PHP will still waste precious computing time scanning them anyway.
$mytext = "Dental Plan";
if ($mytext == "Dental Plan") {
echo "Lisa needs braces"; }

Those same three lines of code could be executed much faster if 'single' quotes were used in place of "double" quotes.
$mytext = 'Dental Plan';
if ($mytext == 'Dental Plan') {
echo 'Lisa needs braces'; }

Now that may not seem like much, but having PHP check for variables where it doesn't need to over the course of a larger script, can certainly impede run-time. Just to clarify my point, PHP will not read a variable if it is within 'single' quotes.
echo '$mytext, Lisa needs braces.';
// Will output: $mytext, Lisa needs braces.
echo "$mytext, Lisa needs braces.";
// Will output: Dental Plan, Lisa needs braces.

What is the the super-secret of keeping those scripts speeding along the rusty pipes of your server? Avoid double quotes at all costs. Even if you are working with a variable and think you need double quotes, it is more efficient for PHP to execute this:
echo $mytext . 'Lisa needs braces.';

As opposed to this bit of molasses-like code:
echo "$mytext Lisa needs braces.";
 
PDF  | Print |  E-mail

It All Adds Up

$variable = $variable + 1;

Is the same as:
$variable ++;

This method also works for subtraction:
$variable --;

You can also apply a similar method for concocting strings. So instead of:
$mytext = 'Done and Done.';
$mytext = "$mytext And I mean Done!"; // $mytext = 'Done and Done And I mean Done!';

Use this shorthand method of adding another string of text onto the end of the first string:
$mytext = 'Done and Done.';
$mytext .= ' And I mean Done!'; // $mytext = 'Done and Done And I mean Done!';

 
PDF  | Print |  E-mail

Avoid using Large String Concatenation

When do concatenation string, avoid uniting with large size string. It can obstructing code execution that really can display more faster.

//large string concatenation
$title = ‘title’;
$body = ‘…a very large block…’;
echo “Subject: $title\n\n$body”;

//avoid large string concatenation
$title = ‘title’;
$body = ‘…a very large block…’;
echo “Subject: $title\n\n”;
echo $body; 

 
More Articles...
<< Start < Prev 1 2 Next > End >>

Page 1 of 2
Copyrights reserved 2010, www.rubin.com.np