The other day my boss was reviewing the code written by me. He had the feedback, keep the code DRY. It was great feedback and I thought I would share with you what DRY means.DRY stands for Do Not Repeat Yourself. To understand it better let us consider the following code snippet: I am fetching a specific movie on the basis of the id. As you can see the code is inside the Details method. Next in the other method Edit is in the controller and I am doing the same fetching of a specific movie on the basis of the id as shown below: As you can clearly see that I am repeating the code in the same class hence the code is not dry in the preceding scenario. The code should not be repeated and should be kept in a repository class. We could have a function FindById() in the MovieRepository class and both of the methods will call FindById(). A common function to find the movie by id can be created as follows: And then both methods of Controllers can use them as follows: In the layered architecture approach DRY code is very essential for robust software development. I hope now you have a better understanding of DRY code and you will follow this approach. Happy Coding.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: