Wednesday, October 22, 2008

More about Singleton and static

Below is from 'http://my.opera.com/zomg/blog/2007/09/17/singleton-pattern-vs-static-classes'

Singleton also gives a reference/pointer to the class instance. You can then pass this reference as a parameter for some other function for example. If you were using static classes, the other function would have to use the class statically too and if you ever needed to change the behavior, you would need to rewrite parts of that function too instead of just having the first one pass a different class as the parameter.

There's also an another side to the reference thing. If you use the reference in your class and decide that you won't need a singleton anymore, but a normal class, you will just need to change the behavior of the class itself and perhaps replace the line which gets an instance of it to constructing a new one or such. If you were using a static class, you would have to rewrite a lot of the code to use the new reference to the class instead of the static class name. A singleton class should also work almost out of the box as a normal class too. If you had a static class and wanted to convert it to a normal one, you would have to rewrite many parts of it.


You can't extend static classes, but singletons you can. Except it doesn't work very well in PHP due to some issues with static method inheritance, but it's not the only language in the world and it can be done but slightly hackily.



So if we think about it with all this, why would you use static classes at all?
They have their uses too - For example for library code they can be nice, such as the static System.Math class in C#.

Tuesday, October 21, 2008

scattered thought

Haven't updated my blog for a while. The company restructured PHP team, again. Now i'm in the team in charge of the project of the company's future core system. I found I entered a crazy world. I experienced this case several times: this this feature, must be done by the end of today! The team had to work night to 11 pm or even 12 pm. This is a project with 3 releases per week!

Experienced developers can imagine that this project must be messy, and actually, yes it is. Now i know why the project's initiator resigned two weeks ago. However, this is life.

Still reading the book refactoring, and after that, i plan to read professional refactoring to patterns.

Tuesday, October 7, 2008

php refactoring

I decide to spend more time in refactoring. I plan to read two books thoroughly: one is "Refactoring: improving the design of existing code", and the other one is "Refactoring to patterns".

I believe refactoring can be applied to PHP applications more than others. Due to PHP language's drastic evolution from a simple script which can be embedded in HTML, to a full OOP features contained programming language, we can apply refactoring technique to tons of legacy PHP applications.

A pity is there are few refactoring topics regarding to PHP. One of them is a chapter in book "PHP In Action". However, based on my opinion, that chapter only touches the super surface of refactoring. There are serveral articles or tutorials as well, i think. But none of them dig more in this area.

Saturday, October 4, 2008

singleton, static, defensive programming

below quoted from PHP 5 CMS Framkwork Development.
"The singleton appears repeatedly, especially for the handler objects described later. In theory, handlers could be implemented as class methods, but in PHP5 a class is not itself a first class object and it is more effective to use singleton objects. Other objects are naturally singletons, such as the session object, since PHP5 handles only a single request at a time.

They(signleton classes) arise naturally in designing systems where a range of related services is needed. Sometimes, this is done by creating a class that consists only of static methods. But that is both inefficient, and inflexible. Dealing with an instance of a class is generally more efficient than dealing with static methods, and in many cases a singleton object will know quite a lot of potentially complex information. Since a class of this kind is a manager or handler, there should never be a need for more than a single instance of the class, hence the name singleton. "

Friday when i was looking into the codes of our system, i found piece of codes like below:

public function f($keyword, $country)
{
if ($keyword === 'a') {
$keyarray = $config[$country]['a'];
} elseif ($keyword == 'b') {
$keyarray = $config[$country]['b'];
}

foreach($keyarray as $key) {
......
}
}

What is the problem of the above code? First, $keyarray is not initialised! if $keyword is neither 'a' nor 'b', the program will still try to run the next codes and throw a notice: Undefined variable and a warning 'Warning: Invalid argument supplied for foreach()'.

In the above case, at lease we should put one more 'else{$keyarray = array()}' at the end to ensure $keyarray is initialised. It is better to check with isset to make sure $config is set up correctly. However, since many php programmers don't do this in practice.

My friend who was working in GE told me that none of PHP programmers are professionals, or all PHP programmers are amateur because PHP is designed for amateur to develop simple database driven website quickly. However, since PHP plays increasingly important role in web app development, enterprises tends to employ PHP developers for their web applications. As usual, they define the criteria like 3-5 years experiences, etc, etc. They never thought that PHP programmers with 3,5 years experiences means they don't have any formal, scientific software development methodology. Probably they know more detail of PHP language. They know more functions in PHP manual. But they don't have OOP in mind. They don't have design patterns in mind. They don't have refactoring in mind.

Friday, October 3, 2008

Experienced programmer != Good programmer

The title formula especially applies to PHP grogrammer. Why? Because PHP's feature determines that an experienced PHP programmer is very likely to be a programmer with very bad programming style & habit. This is fine. But the terrible thing is, he doesn't realise that and is still thinking he is good, experienced. The more terrible thing is, the company heads also trust him, believe he is good because he is very experienced. That makes things hard for those truly good programmers.