| Love your class methods [message #2106] |
Wed, 16 July 2008 10:17  |
mvriel Messages: 20 Registered: July 2008 Location: Hoorn, the Netherlands |
Shiny and New |
|
|
Functions are the developer's best friends ever since the invention of procedural programming. Then Object Oriented Programming arrived and took those functions with it and called them methods.
Sometimes I am looking at code created by fellow developers and while I am pleasantly surprised about the increased usage of Object Oriented Programming, I get disappointed when I see people not giving enough love to the class method. It is as if we have forgotten what methods mean to us and our applications.
You can give love to your methods by asking yourself the following questions,
Does the name say anything when read out of context?
People use the name ‘foo’ for their methods, they really do! Do you know what it does by looking at it? Try to give your functions understandable yet concise names explaining what they do.
Examples: getUserId, useUndefinedAction, runTests
Do I use a coding standard?
When working on a project it is always good to use a coding standard. Your methods benefit greatly from this. Try to use a tool like PHP_Codesniffer to check your code and become aware of what you type. Your methods are easier to be recognized and your code works more intuitive, which will save effort and time!
Do I (or should I) excessively comment these methods?
Probably you can split up your method into smaller ones when you feel the need to use comments like 'get this' or 'do that' followed by multiple lines of code inside a method body. Just extract the lines and place it in a private or protected method. It will make your code more readable, better maintainable and you might discover that a method can be reused in the same class!
Did I use this code again?
Sometimes you start hacking on a class and get that Déjà vu feeling when writing your code. [U]Use this moment![U] Check your class to see if you have written the same code again (or with a slight variety) and if so, turn that code into a generic method. Chances are that you might use it again later in your class!
An example: before
Let me show an example class where we have left out the actual code to help visualize.
class ExampleSingleton
{
private static $_singleton = NULL;
private function foo()
{
// Do some stuff
{…}
// Get some stuff
{…}
// Do other stuff
{…}
}
private function __construct()
{
// Initialize this instance
$this->foo();
}
public static function getInstance()
{
{…}
}
}
An example: after
Now, what would be much easier to read is the following refactoring of the class.
class ExampleSingleton
{
private static $_singleton = NULL;
private function doSomeStuff()
{
{…}
}
private function getSomeStuff()
{
{…}
}
private function doOtherStuff()
{
{…}
}
private function initialize()
{
$this->doSomeStuff();
$this->getSomeStuff();
$this->doOtherStuff();
}
private function __construct()
{
$this->initialize();
}
public static function getInstance()
{
{…}
}
}
In the end
When you ask yourselves these questions you will see that your code becomes alive, more beautiful, much more readable and maintainable.
As can be seen in the example, a new method even replaces some comments since every method contains a story to be told, instead of lines and lines of raw code.
Give love to your code, love your methods!
[Updated on: Sun, 03 August 2008 14:54] by Moderator
|
|
|
| Re: Love your class methods [message #2114 is a reply to message #2106 ] |
Thu, 17 July 2008 10:55  |
coche Messages: 17 Registered: July 2008 Location: El Salvador |
Shiny and New |
|
|
|
Nice article. Thanks for sharing
|
|
|