Coding - Thinking before Coding
(Page 2 of 10 )
You have a brain--use it before you ever set finger tips to keyboard. By taking the time to make a few decisions and thinking out the design of a script, you can save yourself hours, days, even weeks of time when your "great" idea turns out being not so great. One thing every programmer should have nearby is the simple paper and pencil. Writing code is the easy part; if you do proper planning then your just writing syntax.
Procedure or Object Oriented
One of the decisions you should make before you start coding is what methodology your code will follow. Will you code it procedurally with a library of functions, object oriented with classes or a combination of the two? PHP is a language that doesn't dictate one way or the other--which in my opinion is one of its greatest strengths. Each has its strengths and weaknesses, so pick the method that will work best for your script.
Basic Logic Flow
Have you ever heard of flowcharting or pseudocode? They are simple non-language specific methods of working through the scripts logic. With your pencil and paper you write/draw out your logic and do simple bench testing to make sure it works. Here is where you should be spending the majority of your time. You can write out your logic from the step by step if/else level up to the script level calling the classes or functions. Take your time--the more problems you find here the fewer problems/bugs there will be to fix later, and a piece of paper is easier to crumple up and redo then rewriting 1000 lines of code.
Breaking it up
Now that you have some of your logic figured out, start looking for things/patterns that are done repeatedly or just seem to go together. Maybe these things can be moved into small self contained functions or classes. Breaking up your code into reusable pieces saves you time in typing and debugging. Typing because you don't have to type the same thing over and over again, debugging because once it is bug free for the first call it will be bug free for the last call.
Next: Commenting >>
More Programming Basics Articles
More By lig