Do not learn patterns, learn concepts

Good afternoon (or evening or morning, depending on when this post comes out).



I want to speak about the elite part of programming and convey, in general, an obvious idea to beginners in the back-end (and not only) development, along the way using the attempt to start writing in Habré.



so



Any programmer with at least a little good taste, who began to program of his own free will, after understanding the basics of the language and writing the first projects, will be asked not “how to do something”, but “how to do something right” and “what are the standards beauty. "



These, of course, true questions will sooner or later lead him to the obscure names “Abstract Factory”, “Singleton”, “Mediator” and the no less understandable abbreviations SOLID, GRASP.



Please note that I did not cite the example of OOP, MVC, ORM - these are concepts with a very specific meaning and scope, with a minimum level of abstraction.



OOP says: "dude, I found a clear and understandable way to present programs."

MVC says: "people, it would be better if you split the code, and the separation rules are under the cut."

ORM says: "pss, man, I just found a way to reconcile two different ideologies - OOP and DB."

Everything is clear here.



I am talking about things that involve a maximum level of abstraction. Example:

"Mediator":
people, if you have a lot of different objects, make one central, connect everything else to it.
The programmer will just look strange in his direction, if he is sociable, he will say “thank you, cap!”, And for the eyes he will call nothing more than “captain evidence”.



The same goes for the other patterns:

"Factory":
people, did you know that you can construct objects using another class?
"Data mapper":
why don't you use an extra layer of abstraction to save data?
"Observer":
guy, but let's get some interfaces?
"Strategy":
Do you know what polymorphism is?
And especially SOLID:

S (single responsibility):
one module should perform only one task, do not forget about it!
I (interface segregation):
use different interfaces for different operations!
Well and so on.



I lead to the fact that the main area of ​​application of patterns is not at all in accustoming to good code. Patterns are needed to discuss a program already written by someone, to formalize good code. So that you can say (“what is this class for?” - “ah, this is part of the intermediary”). But not in order to check his writings with the uml-patterns of the convulsive attack “aah, what pattern to apply in this case?”.



A programmer who is fluent in some kind of concept (for example, from the above) will write code himself using best practices, even if he has not heard a word about them. And those same best practices are needed to formalize the relation of his team leader to his code - and the quintessential here is, of course, GRASP.



What I would like to say to novice programmers:



  1. Learn concepts, not patterns. Concepts are really important.
  2. if you suddenly come to your senses that you don’t know the patterns, do not panic. This will only matter to the recruiter, but not to your code and good style.


What I would like to say to the guru of Habr:



  1. I might be wrong. Really.
  2. I understand the idea is commonplace. But, nevertheless, for some reason I often come across a similar approach for beginners.
  3. Based on paragraph 2, a similar article may already have been written before me. I dont know.


And finally: the article, of course, is subjective, I do not urge anyone (to fight the factories) to anything. This is just my opinion :)



Moreover, I'm just a junior, but I just see the contradictions and talk about them.



Have a nice day!



All Articles