Program Logic & Refactoring

First off, let me say that refactoring is really nothing more than rewriting code to improve the logic. My personal recommendation is to write it correctly in the first place. That said, I have worked on many projects when I didn't write the code in the first place, but it needs help. Since management tends to frown on rewriting code, instead we refactor it, because that is clearly a better alternative.

OK, so we are going to structure some code into some meaningful arrangement that allows the code to be understood and maintained. Where do we start? What's the best organization? Should we use design patterns?

Organizing

How would you go about writing a recipe? First, you need an idea of what you want to make. Then you need a list of things that will be used to make it. This can include additional tools necessary in the production. Generally you would then break the process up into meaningful steps or stages. Say, for example, we are making a pumpkin pie.

The Idea

We have our idea: Pumpkin Pie.

The List

We'll need:

That's a good start.

To simplify this, we'll just buy the parts we can, such as a pie crust and filling.

Steps

We need to prepare the crust.

We need to prepare the filling.

We need to combine them.

We need to bake it.

We need to eat it. (OK, this isn't really a step, I just want some pie.)

Is This Cooking or Programming?

We are talking about programming so let's convert this pseudo-recipe into a psuedo-program.


Fatal error: Uncaught Error: Class 'format' not found in /home/abentley/www/pages/pgm101/logic.html:87 Stack trace: #0 /home/abentley/_framework/library/framework.php(126): include() #1 /home/abentley/_framework/library/framework.php(60): FRAMEWORK::include_file() #2 /home/abentley/_framework/library/framework.php(1466): FRAMEWORK::respond() #3 /home/abentley/public_html/index.php(4): include_once('/home/abentley/...') #4 {main} thrown in /home/abentley/www/pages/pgm101/logic.html on line 87