Answer the question
In order to leave comments, you need to log in
DRY at any cost?
I'll start from afar - with a general question. Is it always necessary to strictly adhere to the basic rules and principles of programming? Will the disregard of such rules be considered low skill? Can this play a key role in the evaluation of you by the employer?
Why did I ask this question. In Youtube, I am taking a course of a web developer popular in the English-speaking environment. In the course of the course, there is the creation of a form for adding and editing products on the site. And this is the principle the author of the course adheres to: one form is created with all possible fields to fill in. Depending on who and for what purpose the form is used (the user sends his product to the admin for the admin to add it later / the user edits his product / the admin adds the product / the admin edits any of the products), parameters are passed to it. Based on these parameters, the form changes - it hides fields that should not be available to the user / the action address changes / the fields are automatically filled if an existing product is being edited, etc. And like, this is Don' t Repeat Yourself. Instead of several forms, only one is created. But! As for me (maybe it's just my lack of professionalism), the form becomes sooooo complicated, with a bunch of checks and sending various additional parameters in order to understand how and where to send this form. And the form itself becomes almost unreadable due to the large amount of logic added to it.
And so, it became interesting to me, is it really the way most serious programmers write? Do I need to accustom myself to this approach - DRY at any cost? Because I would most likely create multiple forms. Not one for every occasion. And for example, one for editing a product and one for adding a new one. And only then I checked with logic who is using it - the admin or the user. Yes, the forms are repeated. But the code would be much easier to read and less confusing.
Answer the question
In order to leave comments, you need to log in
Is it always necessary to strictly adhere to the basic rules and principles of programming?
It's not DRY anymore.
This is already an anti-pattern of God Object actually turned out.
The essence of the DRY pattern is to make it so that changes are required only in one place in the code
and it is not a pain when you have to go through and look for the same pieces of code and replace them.
In the case you described, a person is trying to solve another problem - so that all forms are similar to each other
or something similar and do not have to change 10 forms. And I agree, his solution is good only for him, others
will have a hard time supporting such code.
To train yourself to follow *any principle* at any cost is stupidity.
Programming principles are just the accumulated experience of good decisions. But there are no solutions that are good in everything. Especially in programming, where conditions change dynamically, and it is simply impossible to predict them all in advance.
Maybe a single form for all cases will be very convenient when you need to change the design of the entire site. Or maybe adapting logic that is not suitable for it to this monster will only gobble up a lot of time, and changing the design will be accompanied by a transition to a framework that will make this universal form simply meaningless. Now, while you are composing it, you cannot know it.
Therefore, one should not pray for principles, but look back at them when deciding how best to do here and now. And accept it not because you learned the principle, but because it helped you understand that it would be better that way. Or vice versa.
> at any cost
Only the Sith make everything absolute.
And any principle should be applied when it is appropriate.
Everyone has already said about the fact that not always available resources correspond to the inner desire to "make everything perfect".
But DRY also sometimes contradicts KISS (the code can become more complicated) and YAGNI (the code will become brittle)
And you can go even further and generally put all the forms on the site into one. And rightly so, why <form>
open the tag several times? Everywhere and always you should be guided by only one rule - common sense. Everything else is a special case of this rule.
Do I need to accustom myself to this approach - DRY at any cost?
Blindly and mindlessly following anything is bad.
Specifically, in your example (exclusively from the way the problem is described), you can follow the DRY principle and, at the same time, adhere to KISS and not make a god object, you can chain inheritance from simple forms to complex ones.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question