| Start |
|---|
| Fat Model, Skinny Controller “Fat Model, Skinny Controller” refers to how the M and C parts of MVC ideally work together. Namely, any non-response-related logic should go in the model, ideally in a nice, testable method. Meanwhile, the “skinny” controller is simply a nice interface between the view and model. In practice, this can require a range of different types of refactoring, but it all comes down to one idea: by moving any logic that isn’t about the response to the model (instead of the controller), not only have you promoted reuse where possible but you’ve also made it possible to test your code outside of the context of a request. Let’s look at a simple example. Say you have code like this: You can change it to this: Then, you can move the logic to your post model, where it might look like this: |
| Domain Objects (No More Fat Models) "Fat Model, Skinny Controller" is a very good first step, but it doesn't scale well once your codebase starts to grow. Let's think on the Single Responsibility (opens new window) of models. What is the single responsibility of models? Is it to hold business logic? Is it to hold non-response-related logic? No. Its responsibility is to handle the persistence layer and its abstraction. Business logic, as well as any non-response-related logic and non-persistence-related logic, should go in domain objects. Domain objects are classes designed to have only one responsibility in the domain of the problem. Let your classes "Scream Their Architecture (opens new window)" for the problems they solve. In practice, you should strive towards skinny models, skinny views and skinny controllers. The architecture of your solution shouldn't be influenced by the framework you're choosing. For example Let's say you're a marketplace which charges a fixed 15% commission to your customers via Stripe. If you charge a fixed 15% commission, that means that your commission changes depending on the order's amount because Stripe charges 2.9% + 30¢. The amount you charge as commission should be: Don't write this logic in the model: As soon as you integrate with a new payment method, you won't be able to scale this functionality inside this model. Also, as soon as you start to integrate more business logic, your Prefer domain objects, with the calculation of the commission completely abstracted from the responsibility of persisting orders: Using domain objects has the following architectural advantages:
I personally like to put domain objects in You may also prefer to create domain objects more action-oriented, following the Command/Query pattern. In such case, putting these objects in |
| Convention Over Configuration In Rails, you find yourself looking at controllers, views, and models for your database. To reduce the need for heavy configuration, Rails implements rules to ease up working with the application. You may define your own rules but for the beginning (and for later on) it's a good idea to stick to conventions that Rails offers. These conventions will speed up development, keep your code concise and readable, and allow you an easy navigation inside your application. Conventions also lower the barriers to entry for beginners. There are so many conventions in Rails that a beginner doesn’t even need to know about, but can just benefit from in ignorance. It’s possible to create great applications without knowing why everything is the way it is. For Example If you have a database table called For Example To create an app you simply run It includes:
|
| Beware of default_scope ActiveRecord includes The above code will serve posts which are already published when you perform any query on the model. That scope, while innocuous-looking, has multiple hidden side-effect that you may not want. |
| Don't Repeat Yourself (DRY) To help to maintain clean code, Rails follows the principle of DRY. It involves whenever possible, re-using as much code as possible rather than duplicating similar code in multiple places (for example, using partials). This reduces errors, keeps your code clean and enforces the principle of writing code once and then reusing it. It is also easier and more efficient to update code in one place than to update multiple parts of the same code. Thus making your code more modular and robust. Also Fat Model, Skinny Controller is DRY, because you write the code in your model and in the controller only do the call, like: This also helps lead to an API driven structure where internal methods are hidden and changes are achieved through passing parameters in an API fashion. |
| You Ain’t Gonna Need it (YAGNI) If you can say “YAGNI” (You ain’t gonna need it) about a feature, you better not implement it. There can be a lot of development time saved through focussing onto simplicity. Implementing such features anyway can lead to problems: ProblemsOverengineeringIf a product is more complicated than it has to be, it is over engineered. Usually these “not yet used” features will never be used in the intended way they were written and have to be refactored if they ever get used. Premature optimisations, especially performance optimisations, often lead to design decisions which will be proved wrong in the future. Code BloatCode Bloat means unnecessary complicated code. This can occur for example by abstraction, redundancy or incorrect application of design patterns. The code base becomes difficult to understand, confusing and expensive to maintain. Feature CreepFeature Creep refers to adding new features that go beyond the core functionality of the product and lead to an unnecessarily high complexity of the product. Long development timeThe time which could be used to develop necessary features is spent to develop unnecessary features. The product takes longer to deliver. SolutionsKISS - Keep it simple, stupidAccording to KISS, most systems work the best if they are designed simple. Simplicity should be a primary design goal to reduce complexity. It can be achieved by following the “Single Responsibility Principle” for example. YAGNI – You Ain’t Gonna Need itLess is more. Think about every feature, is it really needed? If you can think of any way that it’s YAGNI, leave it away. It’s better to develop it when it’s needed. Continuous RefactoringThe product is being improved steadily. With refactoring, we can make sure that the product is being done according to best practice and does not degenerate to a patch work. |
Read Article
https://develop.horje.com/learn/1434/reference