“Keep it simple, stupid”
Always write code that you’ll be able to understand in six months time when you’ve moved on to bigger and better things. If you have to be clever, leave yourself clues. It’s less likely to break this week, and when it breaks in 6 months time, you’ll be able to pick up the pieces again. The gotcha here is that you need to be pretty experienced to write simple code. Don’t sweat it too
much, just make sure that when you come back to fix it later, you left enough comments to understand what the hell you were thinking at the time!
Reduce, reuse, recycle
The more you re-use a piece of code, the less overall code you have to worry about. But be careful! If something in your code library doesn’t quite fit, resist the urge to add modifications and options to make it more general-purpose. All too often the price of extra flexibility is unnecessary complexity. It’s okay to have two similar things.
As Voltaire put it, “le mieux est l’ennemi du bien”: perfect is the enemy of good.
Keep your core PHP and your HTML separate
As you program, you’ll quickly find out that you’ve got at least two different kinds of code going on: code that puts things on the screen, and … everything else. Put everything that isn’t displaying code in a separate file (or files).
Template systems do this for you, and MVC-style frameworks & micro-frameworks go even further, but even if you’re not using them, remember to keep “what it does” separate from “how it looks”.
It’ll be easier to change how your site looks, and much easier to reuse code you wrote. Finding database code half-way through an HTML page is a sure sign that you’re doing it wrong.
Swallow your pride
Don’t be afraid to use other people’s code. Some tasks sound simple on the surface, but turn out to require years of experience and special knowledge (HTML filtering, for example). Find an open-source project that has tackled that problem and use their code. The less code you write, the less you have to maintain and document, and the easier your code is to understand. Just make
sure you keep an eye on the projects you use and make sure you stay up to date.
Write code that’s clear enough to serve as documentation
Documentation systems have gotten really, really clever in recent years:
you can write special comments to automate testing, or to generate gorgeous (and useful) developer documents, but whether or not you use those tools, add comments sparingly to define stages of processing or any code that’s complex enough to need a second glance. Most especially, whenever you do something weird and froopy, write a comment to say why. You’ll thank yourself later.
Ignore 90% of the database functions in PHP
There’re hundreds of the bloody things! PDO is safe, flexible, and fast. It probably talks to the database you use, and you only
need to learn it once, not once per database. PDO’s support for prepared statements will make your code tidier, and you get better security as a perk.
Read more…
Learn to code object-oriented PHP
Some people find it hard to wrap their heads around object-oriented programming, but others find it’s actually easier. Learning to code OO can make your code a lot neater and more comprehensible, and it’s definitely worth learning it sooner rather than later. You won’t find many modern PHP projects that don’t make use of OO somehow.
Trust your source control
Don’t leave huge chunks of code commented out. Your old code is all in your source repository, so you can remove it. When you come back to your code later, you won’t be left wondering if you commented it out because you were debugging,
or because it was broken, or useless.