With this done, the controller now only does only what it is intended to do: control the process. Public void Create(OrderCreateRequest request) Let us first extract all business logic from the controller and move it to a OrderService class: public class OrderService Controllers should only control the overall process, and not actually house every bit of logic of the process.īut today is the day we stop writing these humongous controllers! And just in case another Controller also needs to create orders, more often than not, developers will resort to copy-pasting the code. For every little change, the developer needs to change the entire controller’s code. That is simply too many jobs for a single class. Notice in the snippet of code above how the controller knows too much about “placing an order”, including but not limited to storing the Order object, sendings emails, etc. But I prefer to use special request classes. In controllers like this, I often see cases where the Order class itself is used as a request parameter. This is a usual OrderController class, its Create method shown. Send email with order details for customer Public ActionResult Create(OrderCreateRequest request) Even if you are not a C# developer, with some OOP experience you will be able to follow along easily. Let’s take a look at an example in C# (ASP.NET MVC and Entity framework). In fact, in any code that is badly written, you can always find a class that has more than one responsibility - form1.cs or index.php containing a few thousand lines of code is not something that rare to come by and all of us probably have seen or done it. I would even argue that it is a necessary and sufficient condition for good code. The first letter, S, represents Single Responsibility Principle (SRP) and its importance cannot be overstated. You probably have heard about SOLID principles: single responsibility, open-closed, liskov substitution, interface segregation and dependency inversion. Furthermore, it doesn’t concern itself with what the underlying data storage layer is: does your app rely on an SQL database, or does it store everything in text files?Īs we continue this article, you will realize how great code is a lot about separation of concern. And to achieve that, start by making sure that your models do only what they are meant to do, and don’t concern themselves with what the app built around it does. Even for complicated applications, good model implementation can result in extremely expressive code. If you have models separated from the rest of your application logic, maintenance will be much easier, regardless of how complicated your application becomes. But readers following these examples realize, only after years, how counterproductive it is to have monolithic chunks of code in their project.
#HOW TO WRITE COMPUTER CODE RECIPE SOFTWARE#
They are not meant to teach software design. Examples like “let’s say we have this ORM X for our models, templating engine Y for our views and we will have controllers to manage it all” achieve nothing other than humongous controllers.Īlthough in defense of these books, the examples are meant to demonstrate the ease at which you can get started with their framework. But they also end up providing bad advice for beginners. These examples try to show what the framework has to offer. Model Is EverythingĪlmost every book about some new MVC (MVP, MVVM, or other M**) framework is littered with examples of bad code. Writing great code is an art, but some principles can always help give your development work the direction it needs to head towards to produce robust and maintainable software. In this article, I will discuss how the Single Responsibility Principle and some techniques that revolve around it can give your code this very quality. This is something that should be considered a fundamental quality of software engineering and design. We may not need to write great code when we are trying to build a prototype, a proof of concept or a minimum viable product, but in all other cases we should always write code that is maintainable. Any code which is not maintainable and cannot adapt to changing requirements with relative ease is code just waiting to become obsolete. Proper indentation, neat variable names, 100% test coverage, and so on can only take you so far. Regardless of what we consider to be great code, it always requires one simple quality: the code must be maintainable.