Posts

Showing posts from February, 2015

Refactoring: rename class/method/variable

This refactoring is such a simple idea and you'll find yourself continually doing this as you work and build extra functionality into your web applications. There is also continual need to do this whole debugging and/or refactoring existing code. Take for example the below class: class person {   public $w;   function clchrlypr() {     // calculate hourly pay rate of employee     ...     return $var;   } } Now let's look at the below example: class employee {   private $wage;   public function calculateHourlyPayRate() {     // do some magic   } } Which was easier to read and follow?  I would hope the second snippet was clearer, however the first is an all to common sight in some projects and seeing this code makes you really have to think hard, and quite frankly wastes time  if we had written the second block of code in. The first place things would have been so much easier for our colleague.

Legacy Code Change Algorithm

When working with legacy code, it is very easy to get carried away and make a big bang refactoring (actually, 'code cleanup and pray' is a better term since we do not have any tests). In this case, we can try to eliminate the static cling by making the static method non static, and extracting the non-static method into an interface. Make the clients (all the call sites) use the interface. This would mean that we deal with potentially a few changes to the client code to a few hundreds in some worst case scenarios, all at one time. The result could be 'Object Reference not set to an instance of an object'. We want to be able to make this change in a phased manner in small manageable increments. Approach Pick one call site. Change in one call site. Bring it under the safety net of unit tests. Make sure the change works. Meanwhile, the other call sites continue using the original unchanged code and continue to work fine. Go back to step 1 till all the call sites ar

Increase conversion rates - the easy way

When your business is online, conversion rates are hot on the list of KPI's to watch month on  month, year on year.  But what are conversion rates, and how do we increase them? In terms of an ecommerce site, be this physical goods or e-goods, for example ebooks or services, the conversion rate on your site is essentially the percentage of your visitors who you manage to convert into paying customers.  People who visit your site and decide, you know what, I've just gotta have this!  And if you can make them impulse buy - even better. Here are some quick tips to increase your conversion rates: 1.  Explain the benefits - 3 C's Clear Concise Consistent Summarise the benefits and sell your product or services as clearly and concisely as possible.  Let your customers know what they are paying their hard earned cash for, and let them know quick.  Why waffle on for pages and pages?  When you meet someone in person, they make their mind up about you in the first 30 se

Refactoring: Push down method

Previously we mentioned the pull up method we can use during refactoring our code. The push down method is the exact opposite. Let's start with some code: abstract class animal {   function bark(){     // bark here   } } class dog extends animal{} class cat extends animal{} $dog = new dog(); $dog->bark(); Maybe one day our cat had a bark about it, or maybe we've coded an if statement to do something different like return false if it's a cat.  abstract class animal {} class dog extends animal {   function bark(){     // bark here   } } class cat extends animal {} $dog = new dog(); $dog->bark();

PHP Refactoring: Pull Up Method

As projects grow organically it is important to continually be refactoring your code base.  One simple refactoring technique which is often overlooked is called "pull up method". This is exactly what it says on the tin - pulling a method up in the hierarchy of its chain so it can easily be reused. Let's take an example: class vehicle { } class car extends vehicle {   function accelerate() {     // code here   } } class motorbike extends vehicle {   function accelerate () {     // code here   } } We can clearly see the duplication above of the 2 accelerate methods. What if e pull these up into the vehicle parent class?  Lets see how: class vehicle{   function accelerate (){     // code here    } class car extends vehicle {} class motorbike extends vehicle {}