Page 115 - DCAP405_SOFTWARE_ENGINEERING
P. 115
Software Engineering
Notes development phases. You can also use the idea of PHP Design patterns which can speed up the
development process by providing tested, proven development paradigms.
Don’t Repeat Yourself (DRY)
The concept here is that anything you use in your code should have a single, unambiguous
representation. This means if you are storing a number, only store it in a single place (don’t have
PI stored in three different places). Not only is multiple storage wasteful, but it multiplies your
effort if you want to change, especially if you have to go hunting through several files to find
where the numbers are defined. Multiple representations are also a great way to generate bugs
if you forget to change some of them.
!
Caution This also applies to code; don’t repeat chunks of code that do the same thing – have
a single version and put it in a function.
Test as you Write
As much as possible, test your code as you write it. These tests will often be quick and easy ones,
such as checking that the value of pi you’re using is really what it should be, but if you perform
these little checks while you’re working on (and thinking about) that piece of code, you’ll save
yourself a lot more effort having to come back later and fix bugs. You’ll find that you can
perform a lot of simple tests very quickly as you go along; once you’re in the habit, you really
don’t spend a lot of time doing it. But the time you save yourself later on can be considerable. As
well as informal testing, using a test harness and writing automated tests will help guarantee
code you have already written keeps doing what you expect and helps prevent bugs you have
already fixed from reappearing.
Reduce Dependencies as much as Possible
A dependency is a connection between two chunks of code, for example, using the same variable,
and the more of these your code has, the more things you have to keep track of. When writing
and debugging a chunk of code, the last thing you want is arbitrary other chunks being able to
interact with it, because it’ll very quickly become unmanageable. Much better would be for each
chunk of code to be highly de-coupled from the rest of the code, with only very specific connections
to the rest of the program. This keeps things much simpler and easy to manage. In practice, this
means things like compartmentalising your code into chunks (which you were doing anyway,
right?), avoiding global variables (with the possible exception of explicitly fixed constants such
as PI) and the like. If you’re feeling keen, writing object oriented code is excellent for this, as this
has encapsulation built into the process.
Validate your Data
At some point, someone will feed garbage into your carefully crafted code. In fact, part of your
testing should be feed garbage input into your code to check that it recognises it! If your code is
validating the data it is given then it should be able to deal intelligently with this, even if
“intelligently” means “crash but tell the user what has gone wrong and why”. Assertions are an
ideal way to make the program stop as soon as something is wrong. They work by ‘asserting’
that something is true and if it isn’t then the program stops.
108 LOVELY PROFESSIONAL UNIVERSITY