There are two mods (that I’m aware of) for Minecraft which let you build computers inside the game. ComputerCraft allows you to have Lua-programmable computers and robots, with basic wired and wireless networking capabilities. RedPower 2 on the other hand has a 6502-like CPU based computers, which are programmable either using 6502 Assembly or FORTH. What if you used Minecraft …
Exceptions and abstraction
So you already know how to handle your errors properly. Even if you’re already using exceptions, there are some nuances to the use of exceptions that are important to know and understand in order to write code that is easier to reuse and more decoupled. Let’s talk about exceptions and how they relate to your classes and abstraction.
The “do X or die()” pattern must die
What’s the most common pattern for error handling you see in beginner’s PHP code? – That’s right, do_X() or die(‘do_X failed);. That’s nice and all, as at least you have some sort of error handling, but I think this way of handling errors must go. There is no place for it in modern PHP code – it’s the worst way …
Should a failed function return a value or throw an exception?
You have created a nice, well written function, but you realize you forgot something: The failure case. What should a function do when it fails? There are two schools for this – the “throw an exception” school and the “return an error value” school. But which of these is the correct approach?
A simple way to make your code better: Stop adding more parameters
You need to add some new functionality to your function or class. Let’s say you need to remove all objects stored, but optionally also call a method on them. It’s pretty simple, isn’t it? Let’s just add a parameter to removeAllObjects! If you make it true, the additional method is called, otherwise not. Except it’s not really such a good …
Is commenting your code useless?
James Carr has written a well argumented post about comments. To sum it up shortly, he says comments are the lowest form of communication, and that commenting freshly written code is not a good idea. I tend to disagree, and here’s why.
Improving code with peer reviews
Peer reviewing is the practice of looking at code written by others to find errors or ways to improve the code. Sometimes also called desktop reviewing, this approach can be useful for various reasons: If you have a coworker who is more experienced than you, you can learn from him/her It’s often helpful to have another set of eyes take …
Decoupling models from the database: Data Access Object pattern in PHP
Data Access Object pattern: It’s a pattern which abstracts the details of the storage mechanism – be it a relational database, OO database, an XML file or whatever. The advantage of this is that you can easily implement different methods to persist objects without having to rewrite parts of your code.
Food for thought: utilizing models in MVC
“What is a model” and “Is Zend_Db_Table a model” seem to be asked once in a while on #zftalk. Frameworks say they have a full model available, thus they are MVC frameworks ORM libraries say they generate models. It seems the ActiveRecord pattern has become somewhat synonymous with model. Pádraic Brady wrote an excellent post on how models are misunderstood, …
Base classes in OOP programming languages
David Otton posted a short but thought-provoking post about stdClass, which many think is the “base class” all PHP classes automatically inherit from. I have to admit that I had this misconception as well. On the other hand, “true” OOP languages such as C# and Java both have a base class which all other classes inherit, even if you don’t …