The modern web has largely settled on Model-View-Controller (MVC) as the paradigm of choice. Though it may ultimately be described by different names, these components are the core of what makes most object-oriented web applications work. And most people know that the model is the most important element of the application. But crafting good models can be extremely challenging. Many developers end up putting code all over the place, with the logic that belongs in the model scattered throughout the view and the controller layers.
What ultimately makes a good model? Where does each aspect of the model belong? And what is to be made of the other layers of the application? Let’s explore together.
I have a long-standing client who has a complex piece of software that I developed for them some time ago. From time to time they approach me and ask me to make improvements to the software, which I am almost always happy to perform. And like any good client they want an understanding of the cost before they move forward, largely to ensure that the cost-benefit analysis makes sense. And so, they ask for an estimate.
I’ve written on estimates in the past. I don’t like them, largely because they can tend to box you into a particular position that may or may not be correct.
Like many mentors, instilling best practices is an important part of what I do. And I encourage my mentees to follow best practices, including testing. For example, I encourage them that code isn’t done until it’s been properly tested, and that the higher the test coverage, the better the outcome overall.
Yet even as an advocate for testing best practices, I’ve steadily learned that “well-tested” or even “completely tested” doesn’t always mean “100% test coverage.” And while this might seem a paradox, it’s not.
Three engineers were standing around chatting at a children’s birthday party. The host approached them and asked, “can you help me hang this piñata somewhere in the house for the children to hit?” The engineers agreed, and set off to figure out the best approach to the problem.
The most junior engineer was young and unsure of himself. He looked around for obvious items that could hang the piñata, and after rejecting several as unsuitable, he proposed that the engineers simply place the piñata on a table, so the children could break it open and access the candy inside. The host didn’t like this suggestion very much; while it met the goal, it failed to meet the scope.
Occasionally folks will ask me, “what does it take for me to earn more money as a freelancer?” They want to know if I have a magic bullet, or if I’ve learned anything through my travels in the business world. The truth is that there’s only one thing you can do to increase your revenues as a freelancer, and it’s this:
Provide more business value for the client.
When returning large amounts of data, it can be common to turn to a built-in PHP data structure: the array. But in object-oriented code, arrays make poor object-to-object data transfer mechanisms. Object-oriented programming is and should be focused on objects. That means that each array should be converted into an object, and collections too.
The reason is simple: when one object makes a request to another object, it should generally get back an object unless the response is scalar. For example, asking for a data set from the database ought to generate an object, not an associative array of data.