Posted on Leave a comment

Debug code better, faster and with fewer headaches

Every time you change code, there’s a very good chance that you will have introduced bugs to your existing code. IT’s important to take a methodical approach to squishing these bugs, or you may waste a lot of time hunting down the cause of specific problems. Here are 7 tips to debugging your code faster.

debugging php javascript c++ java

  1. Don’t pretend that this isn’t your fault (or your teams fault). You wrote the code and you made a mistake. Don’t beat yourself up over it, but don’t blame the computer for your mistake. By that same token, dont’ beat yourself up over a bug. Bugs will always happen, just solve it and move on.
  2. Treat every error as an opportunity to learn about yourself. If you think of bugs as a puzzle or mystery to be solved, you won’t find yourself burning out as fast and this will encourage a more patient and relaxed approach to solving errors.
  3. Write tests. Testing is not only great at preventing bugs but also helps a lot in debugging. Testing forces your design to be more or less modular, which makes isolating and replicating problems simpler. The tighter control this provides over the environment will cause fewer surprises. Moreover, once you get a failing test case, you can be reasonably sure that you’ve caught the real reason of the code’s behaviour..
  4. Use a debugger! Don’t avoid stepping through your code with a debugger just because it’s time consuming. Checking every line of your logic will often help you to find better ways to do things and highlight the real cause of errors. I personally really like the built in debuggers in both Netbeans and PHPStorm
  5. Talk about someone about your problem, even if it’s just a rubber duck (I personally use my android figurines from Android TO). Forcing yourself to express the problem you are working on in words really does miracles.
  6. Give yourself a time limit. If for example after 45 minutes you feel you are going nowhere, just switch to other tasks for some time. When you get back to your bug, you’ll hopefully be able to see other possible solutions that you wouldn’t have have considered before.
  7. If you have narrowed it down to two systems that collaborate (for example a PHP back-end and a JavaScript front-end), inspect the messages between the two systems via traffic monitor or log files, and determine which system is behaving correctly and which one is not. If there are more than two systems in the scenario, you can perform pairwise checks and work your way “down” the application stack.

As you can see, debugging code isn’t always an exact science,but by applying the right mindset and techniques, you can speed up the process significantly. Fast debugging isn’t always

Related reading:
Why Programs Fail
The Pragmatic Programmer
Software Entrophy

Posted on Leave a comment

Factory Design Pattern In PHP

The factory design pattern solves a common programming problem. When you have a single point of origin for many classes with the same base class, you have need of the factory design pattern. A factory is a class that instantiates one or more of a number of classes that share the same base type, choosing the type of the instantiated object type at runtime without exposing the instantiation logic. You won’t often see this pattern appearing in basic projects, but as you move into enterprise development, it is very common.

In the following example, an object of type ShapeFactory is instantiated by the application code. Then when the getShape() method is called, the ShapeFactory object instantiates a new object which based on the logic in this function chooses which child class of the Shape class will be chosen to hold the new object. This allows a little more freedom in which types we use at runtime.

Factory Design Pattern UML Diagram
Basic Factory Design Pattern UML Diagram

Factory Design Pattern Example In PHP

The factory design pattern is very simple. The simplest example I like to use to describe it using PHP is a basic game with different kinds of enemy ships. In the following example we will start with an enemy class, and a few child classes that extend it like so:

Now if we are playing a game and wanted to call any of the enemy ship types from a single point of origin, we would spawn them using a factory class that looks something like this:

The code that calls the factory looks like this:

With output looking like this:

Posted on Leave a comment

Lean and Mean PHP Email Class

Sending beautiful emails and attaching files with PHP can be a real hassle. I wrote this class as part of one of my side projects but I have recently used it as a teaching tool.


Posted on Leave a comment

Simple & Light PHP MySql Db Class

In my world, a PHP db class in essence should be a black box that handles the concerns of direct interaction with the MySql database and provides an abstract but functional alone basis for my model classes. The Db class must handle connecting to the database, and have inheritable static methods for executing both statements and queries. Also it must be tested before it is put into production. This code is tested and ready for production.

I originally wrote this class in a side-project of mine, and I’ve been using it recently as a teaching tool.