Search This Blog

Tuesday, February 23, 2010

Chapter 5: Robust and Healthy Code

It's easy (and sometimes necessary) to slap applications together quickly, without taking the time to make sure they'll be robust and healthy over the long term. For applications that are large, complex, or critical, it's worthwhile to use a formal software development process. That process examines, documents, and provides metrics for the development lifecycle, including design, development, change orders, maintenance, updates, and termination.


This chapter is all about writing robust and healthy code, and whenever possible you should spend the extra effort to make sure your code has these qualities. Plus, learning how to create good code gives you an edge because if you adopt good coding habits and practices, you're more likely to create high-quality code the first time around.


What are these qualities? A robust program doesn't fail easily, and can cope with a wide variety of input. If it can't proceed or reach the desired outcome for some reason, it fails gracefully, meaning it allows the user to proceed to the next logical option with a meaningful message (and certainly doesn't display PHP or SQL error messages). In addition, healthy code is written with plenty of internal documentation, so it's easy to understand what was intended when you or someone else has to go back in and do more work later, and it's written not just so it works, but so the structures make sense and are abstracted from each other to a degree that it's unlikely a small change in one spot will cause bugs in other spots. Together, these qualities make your programs better, and easier to maintain.


In this chapter you learn the basics of testing, troubleshooting, and debugging, and how to use PHP5 error handling functions. You'll also create a string handling function to validate form submissions, use regular expressions for validation, and experiment with the new try/catch functions.