How to write SOLID C++

  Рет қаралды 36,901

platis.solutions

platis.solutions

Күн бұрын

We all know, or should know, about SOLID. The question is, do we write C++ according to the SOLID principles?
This is a quick tutorial on how to apply the SOLID principles using the C++ programming language. You can find it in a written form at: platis.solutions/blog/2020/06...
Contents:
02:56 - Single responsibility principle
08:01 - Open/closed principle
10:35 - Liskov substitution principle
16:08 - Interface segregation principle
20:31 - Dependency inversion principle
Please like, subscribe, comment and share the video to promote good programming practices.
Want to buy me a coffee so I can keep making free videos? Then please do so here: buymeacoff.ee/platisd

Пікірлер: 84
@eurbanautotech
@eurbanautotech Ай бұрын
Thanks for the awesome video. I've been programming C++ for 25 years but non professionally. I used to think all the abstract programming design theories were just "corporate bloat". I had the mentality of "shut up and code!". But as you can imagine, projects became increasingly complex as project size grew. This forced many projects to go unfinished. I was CONSTANTLY having to rewrite code. Not to mention near full-rewrites... Stuff was near impossible to maintain or extend... the list goes on. Finally, I've been making a concerted effort to write maintainable code so I don't have to keep rewriting everything from scratch all the time! I definitely agree with your beginning of the video. I tried looking through some SOLID videos before but they were based on other languages. The implementations were quite different and definitely made the examples harder to relate to. Thanks again! I've subscribed and will definitely be checking out your other videos.
@antonfernando8409
@antonfernando8409 2 жыл бұрын
Awesome presentation. To summarize SOLID principle: 1. keep it simple, own only one responsibility. 2. without having to touch base class, extensions are done in child class, meaning base class should be generic/abstract enough. 3. Liskov substitution principle: child behaviors are honored, while adding new behaviors. 4. Interfaces should be minimum and sufficient, nothing more, nothing less, otherwise confusing and code bloating, also interfaces should be simple enough, and should do one job. 5. decoupling, nothing is tightly coupled, high business logics are not tightly coupled, concrete rules are not tightly coupled, basically in all things consider how to reduce tight coupling. Bottom line: easy to understand code , easy to use and change, easy to extend. Thanks.
@platisd
@platisd 2 жыл бұрын
Sounds about right! 👍
@antonypace8907
@antonypace8907 3 жыл бұрын
One of the best videos I have ever seen on OO embedded design. Thank you.
@101Crock
@101Crock 2 жыл бұрын
Honestly, I watched another video that used python as an example, and I wasn't really getting it. This really helped me truly understand this set of principles.
@platisd
@platisd 2 жыл бұрын
I'm very happy to hear this! ⭐👍👍
@AlexDrastico380
@AlexDrastico380 3 жыл бұрын
Good explanation of SOLID. Just want to point out something about the LSP. The only way this principle is not violated is when both the pre- and post- conditions of the interface are not violated. This principle is basically derived from the old concept of "contract programming", which implies the definition of the specifications through interfaces, both with signatures AND documentation. So both pre and post conditions for the interface should be stated by specifying the range of valid values accepted and returned by the methods in their documentation. This does not prevent bugs by code, but it's up to a good programmer making sure they read the specifications and stick to it. Enforcing this by code essentially introduces another dependency, so I don't like that solution either.
@svensk8squad
@svensk8squad 3 жыл бұрын
Thank you for the Video. I enjoyed your examples and explanations. They really helped me understand the principles better. I hope they become more natural to me with more practice.
@rexsoltera
@rexsoltera 2 жыл бұрын
A concise and practical guide. Thank you so much!
@reksie7816
@reksie7816 3 жыл бұрын
I use C and still learnt quite a lot from this. I absolutely suck at OOP and designing OOP so C is what I prefer to use in my free time.
@eotcoldhymns2930
@eotcoldhymns2930 7 ай бұрын
I watched your other video on SFINAE. I personally like the way you present. Please keep it.
@platisd
@platisd 7 ай бұрын
Thanks for the kind words of encouragement! 🧡
@BeWrecker
@BeWrecker 2 жыл бұрын
One of the best video available on solid principal, great effort man :)
@CodeStation
@CodeStation 3 жыл бұрын
Great content. Thanks for sharing. A small bug that I noticed in the example code. In about 26:07, the struct AwsCloud should derive from Cloud.
@platisd
@platisd 3 жыл бұрын
Ooops! Good catch. I fixed it on the written versions of this tutorial.
@CodeStation
@CodeStation 3 жыл бұрын
@@platisd It's not a big deal. What matters is the explanation and getting your point across. Which you did! Congratulations!
@ninadgade
@ninadgade 3 жыл бұрын
Very well explained. One of the best videos on this topic.
@mohamedhussien4013
@mohamedhussien4013 5 ай бұрын
Thank u so much for the great explanation. Real-world examples are awesome.
@odedkadshai
@odedkadshai 3 жыл бұрын
thank you, the aws file upload example is perfect
@raselhasan2433
@raselhasan2433 Жыл бұрын
Loved your content for using cpp, thanks.
@chandankumarmishra336
@chandankumarmishra336 3 жыл бұрын
you really did a wonderful work here...thanks a ton :) ...very well explained here...best in the market as of now ...cheers..
@bigbitesaint
@bigbitesaint 3 жыл бұрын
Thank you. This is exactly what I need. Subscribed =)
@siedamout3904
@siedamout3904 2 жыл бұрын
Awesome explanation. Thanks!
@vinay_sac
@vinay_sac 2 жыл бұрын
This is very very clear explanation of SOLID principles.
@AJSquirrel53
@AJSquirrel53 3 жыл бұрын
This video was awesome! Subscribed!
@felipelopessss
@felipelopessss 2 жыл бұрын
Excelent video, thank you!
@dhananjayah.s9796
@dhananjayah.s9796 2 жыл бұрын
Awesome explanation with code walk through
@estebanm.8668
@estebanm.8668 3 жыл бұрын
Fantastic explanation !
@proxy8918
@proxy8918 2 жыл бұрын
Amazing video, thank you so much.
@errodememoria
@errodememoria 2 жыл бұрын
Good video bro. I already knew about solid, but it was in java. This video really came in handy for me.
@platisd
@platisd 2 жыл бұрын
Thanks, that was exactly the reason this video was made 😊
@kleanlins
@kleanlins 3 жыл бұрын
what a great video!
@arizona_ranger_connoisseur
@arizona_ranger_connoisseur 3 жыл бұрын
Φίλε μου, πραγματικά φανταστικός. Έχω παρακολουθήσει τουλάχιστον 2-3 courses και μπορώ ειλικρινά να πω, κανένα δεν με βοήθησε να καταλάβω τα solid principles. Αν και σε συντομία, τα εξήγησες πάρα πολύ απλά και παραστατικά και, βεβαίως, τα παραδείγματα στην cpp βοήθησαν και αυτά.
@platisd
@platisd 3 жыл бұрын
Ευχαριστώ πολύ Ηλία!
@platisd
@platisd 3 жыл бұрын
Αν τυχόν δεν είσαι ήδη μέλος, υπάρχει και το grcpp στο meetup και στο Facebook όπου διοργανωνονται εκδηλώσεις τακτικά πάνω σε θέματα σχετικά με C++ στα ελληνικά: facebook.com/grcpp www.meetup.com/grcpp-athens/
@arizona_ranger_connoisseur
@arizona_ranger_connoisseur 3 жыл бұрын
@@platisd Όχι δεν το είχα ακουστά, οπότε θα γραφτώ. Σε ευχαριστώ πολύ Δημήτρη.
@berksteraydo9517
@berksteraydo9517 3 жыл бұрын
Really good video, and good solutions to the problems showcased. I just want to ask How would you go about the project file structure? I don't like to put many structs into one file, because I wouldn't have an appropriate name for the file. Splitting them into their separate files would clutter the project directory. Frankly my declarations and usage looks strange.
@platisd
@platisd 3 жыл бұрын
Thanks for your kind comments Berkster. Now, as to the project structure, if it's big enough and unless there already is some convention I like to split the code in three top layers/directories: 1. Commodity layer: Components that do not add any value to the product but "have to be there". It's components that you should not add features to but if you make changes to them, it should be for bug fixing and decreasing their maintenance cost. 2. Differentiation layer: Components that differentiate the product from others, code for different variants or customers etc. You make changes to those layers so to differentiate more from the competition and create "revenue". 3. Experimentation layer: Components that *may* bring value in the future. You need to be able to create them fast and verify the business case. You create quick prototypes in this layer and if the business makes sense you move those to the differentiation layer. Higher level layers may depend on lower level ones but not vice versa. This architecture I described is based on the "3LPM". You can read bit more here: janbosch.com/blog/index.php/2017/01/28/9-out-of-10-in-rd-work-on-commodity/ If you need, I can provide some more reading material on it. Anyway, for each layer, I usually group different components or classes that belong to the same functional area. Each folder often contains an `include` and a `src` directory. Last but not least, there's a test directory in the root of the project too.
@berksteraydo9517
@berksteraydo9517 3 жыл бұрын
@@platisd Thank you, kind sir, your reply is much appreciated. Keep at it, and you shall abound. This channel will get bigger soon. : )
@chandankumarmishra336
@chandankumarmishra336 3 жыл бұрын
your blogs are worth reading...well done...
@platisd
@platisd 3 жыл бұрын
Thank you! :)
@TheMorrigan
@TheMorrigan Жыл бұрын
Perfect !
@404lab9
@404lab9 2 жыл бұрын
Thank you, But I found it very hard to read at your blog because of gray font color on white backgrounf. Could you please change it to black for better reading.
@platisd
@platisd 2 жыл бұрын
I'll see what I can do 👍
@mehmetaltinsoy525
@mehmetaltinsoy525 2 жыл бұрын
Thanks
@fadyhany6818
@fadyhany6818 2 жыл бұрын
In 9:29 I didn't understand how did you initialize" mModle" in the class "DistanceSensor" without declared it ?
@platisd
@platisd 2 жыл бұрын
Just imagine/assume there's a member variable of type SensorModel. The goal of this presentation isn't to demonstrate fully working or high performant code but illustrate a point related to the SOLID principles.
@raimonestanol8234
@raimonestanol8234 3 жыл бұрын
Never saw it so well explained, still to make sure, the D means the methods shouldn't depend on the details no? They should be universal across different children and parents no? Great work!
@platisd
@platisd 3 жыл бұрын
I think you have understood it correctly, yes. One aspect of the dependency inversion is that an interface shouldn't leak or impose an implementation. It shouldn't be semantically coupled to a specific child. This happens very often by the way because, when creating interfaces, we sometimes can't help being biased. Bias in favor of "the current" implementation details makes us create our interfaces dependent on them. We will eventually discover this when we want to reuse our code on a different product and realize that our interface wasn't abstract but rather specific to certain details. 😅 I'm preparing a new video where this is shown.
@MrGaurabpaul
@MrGaurabpaul 3 жыл бұрын
@@platisd do you do online classes on c++ over zoom. if so can you let me know
@platisd
@platisd 3 жыл бұрын
@@MrGaurabpaul I used to in my previous job. Best way to get notified about this is to subscribe to my channel! :)
@NaP608
@NaP608 3 жыл бұрын
Ty
@chandankumarmishra336
@chandankumarmishra336 3 жыл бұрын
loved it totally....Do you have the complete source code for these ?
@platisd
@platisd 3 жыл бұрын
There isn't a "working" project with these examples, but you can find all related code in this blog post: platis.solutions/blog/2020/06/22/how-to-write-solid-cpp/
@FrostGamingHype
@FrostGamingHype 3 жыл бұрын
some one distrub him with 9999 cups coffees so he makes a great video imagine getting these
@iamagenius2646
@iamagenius2646 Жыл бұрын
Really good fundamental principles for projects, but they represent something ideal. In real case scenarios these will be bent and sometimes violated. So for anyone who is new, follow them as a guideline and in real cases you'll need to pivot a lot and do lots of violations of these principles :))
@greatbullet7372
@greatbullet7372 3 жыл бұрын
DIP can be overused though. For sockets it makes no sense for instance and also for code that is highly dependant on operating specific routines that differ from each other that much that you need to implement them anyway as intended. For generic software architecture design its benefitial though, thumbrule the lower the level you operate on an os in your software the less sense it makes.
@platisd
@platisd 3 жыл бұрын
Sure, if you have a unit and all it's doing is using an external library or making calls to an operating system then it makes less sense to try and make it reusable. That being said, I'd argue that the particular part of the code should remain as limited as possible and for it to be made available via a generic interface to higher level modules of the system.
@cristianinujazznight3044
@cristianinujazznight3044 3 жыл бұрын
Thank you. Could you explain Design patterns in C++ or Rust?
@platisd
@platisd 3 жыл бұрын
That's a good idea. Any of them you're after in particular? Here's my main source on them: sourcemaking.com/design_patterns They have C++ examples as well, however not so modern C++ unfortunately.
@cristianinujazznight3044
@cristianinujazznight3044 3 жыл бұрын
@@platisd Thats the case. I will like it in C++20. If you like the factory :D
@platisd
@platisd 3 жыл бұрын
@@cristianinujazznight3044 I'm new to making such videos so I'd like some feedback. What would you prefer more: a) A single longer video with many design patterns or b) multiple shorter videos with one design pattern at the time?
@cristianinujazznight3044
@cristianinujazznight3044 3 жыл бұрын
@@platisd Mmm... I would prefer shorter b).
@axelman145
@axelman145 3 жыл бұрын
@@platisd I agree with Crhizt! Shorter Videos with one Design pattern at a Time! However, this video is great just as it is! really enjoying it :) Very clear and concise! Perhaps adding Time stamps in the description would be enough to help people sort through it if they don't need to watch all of it.
@bigyang5847
@bigyang5847 Жыл бұрын
the struct interfaces feel so wrong....... especially at 7:35
@platisd
@platisd Жыл бұрын
Because they are `struct` or some other reason?
@bigyang5847
@bigyang5847 Жыл бұрын
@@platisd coming from java and just learning c++ more in depth, it feels wrong in general, but mainly because it's a struct and not a class
@platisd
@platisd Жыл бұрын
@@bigyang5847 aha I see. `struct` is used in the examples to skip one line of code for the `public` access modifier needed in the case of classes. Other than that, it's a matter of convention. The only difference, after all, between classes and structs in C++ is their default visibility.
@bigyang5847
@bigyang5847 Жыл бұрын
@@platisd That's something new I just learnt. Thank you!
@beboba2498
@beboba2498 2 жыл бұрын
With getOrientation example you did not provide a good solution, what was the point of giving just a bad example ? Sometimes it's really hard to follow SOLID principles because there are no principles in real life.
@platisd
@platisd 2 жыл бұрын
Either the example was bad AND "there are no principles in real life" or you failed to understand the example as well as the principles. Which case do you think is the most probable?
@theRECONN
@theRECONN 3 жыл бұрын
Maybe information and intentions were good but your code quality is questionable, not to say it doesn't compile sometimes.
@platisd
@platisd 3 жыл бұрын
Can't see the forrest for the trees perhaps?
@theRECONN
@theRECONN 3 жыл бұрын
If I wanted to know about SOLID there are plenty of resources. You were supposed to be detailed on SOLID C++ which is not about writing SOLID examples in C++, that's just rewriting.
@dimitriosplatis7496
@dimitriosplatis7496 3 жыл бұрын
​@@theRECONN is this a _detailed_ piece on SOLID C++? Yes, or at least most people seem to think so. It explains how to apply the SOLID principles using C++ and particularly using many examples inspired from the embedded systems domain. Now, time for the apparently-not-so-obvious questions: - Should the _focus_ be on the C++ language semantics or how the SOLID principles are applied? - Should we have multi-page examples to ensure conformance with all C++ best practices? E.g. Should we abide by the rule-of-5 when defining a public virtual constructor in interfaces or can we skip that in favor of having all related code in a single page/slide? - Is it important to pass a std::string argument by const reference or std::string_view in an example that illustrates the Single Responsibility Principle? I could go on for a while but I hope you got the point with all these are rhetorical questions. If what made you miss the point of the video was the lack of a disclaimer in the beginning clarifying that the code snippets are simplified, I'll add one next time. I like this one by Vishal Chovatiya (www.vishalchovatiya.com): > The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords like override, final, public(while inheritance) just to make code compact & consumable(most of the time) in single standard screen size. I also prefer struct instead of class just to save line by not writing “public:” sometimes and also miss virtual destructor, constructor, copy constructor, prefix std::, deleting dynamic memory, intentionally. I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.
@theRECONN
@theRECONN 3 жыл бұрын
@@dimitriosplatis7496 If I know SOLID and know C++ - what do I learn? Remove my comment if it triggers you so much. I did not mean you have a typo or use a string instead of a string_view. I expected C++-tailored SOLID principles, simple examples but sophisticated concepts. Did I get CRTP, EBO information in interface segragation or pimpl idiom as a different approach of dependency inversion? Those are rethorical questions as well.
@platisd
@platisd 3 жыл бұрын
@@theRECONN you started off by noting "the code quality is questionable" and that "the snippets do not compile sometimes". Now, I guess my rhetorical questions may have somewhat worked and you mention instead you were expecting sophisticated concepts explained with simple examples. If SOLID is the "sophisticated concepts" and you mention mention PImpl and CRTP as the "simple examples", as alternatives to DI, then we have a very different idea on what is a simple example explaining a sophisticated concept. 🤯 If, on the other hand, SOLID with those "alternatives to DI" is the sophisticated concept, then this deserves a video of its own as a follow up of this.
@lorenzobolis5166
@lorenzobolis5166 3 жыл бұрын
I'm allergic to this amount of 'virtual' and dynamic polymorphism, this is not Java.
SOLID Principles: Do You Really Understand Them?
7:04
Alex Hyett
Рет қаралды 118 М.
Normal vs Smokers !! 😱😱😱
00:12
Tibo InShape
Рет қаралды 117 МЛН
¡Puaj! No comas piruleta sucia, usa un gadget 😱 #herramienta
00:30
JOON Spanish
Рет қаралды 22 МЛН
Forbidden C++
33:07
javidx9
Рет қаралды 1 МЛН
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,5 МЛН
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 882 М.
Don't Write Comments
5:55
CodeAesthetic
Рет қаралды 763 М.
C++ Code Smells - Jason Turner
56:11
NDC Conferences
Рет қаралды 75 М.
BEST WAY to make Desktop Applications in C++
26:00
The Cherno
Рет қаралды 872 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 757 М.
iPhone 15 Pro vs Samsung s24🤣 #shorts
0:10
Tech Tonics
Рет қаралды 9 МЛН
Индуктивность и дроссель.
1:00
Hi Dev! – Электроника
Рет қаралды 1,5 МЛН
How charged your battery?
0:14
V.A. show / Магика
Рет қаралды 2,4 МЛН
как спасти усилитель?
0:35
KS Customs
Рет қаралды 466 М.
3D printed Nintendo Switch Game Carousel
0:14
Bambu Lab
Рет қаралды 4,7 МЛН