How to write SOLID C++

Поделиться
HTML-код
  • Опубликовано: 19 май 2024
  • 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 21 день назад +1

    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.

  • @antonypace8907
    @antonypace8907 3 года назад +6

    One of the best videos I have ever seen on OO embedded design. Thank you.

  • @svensk8squad
    @svensk8squad 3 года назад +1

    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 года назад +1

    A concise and practical guide. Thank you so much!

  • @ninadgade
    @ninadgade 3 года назад +2

    Very well explained. One of the best videos on this topic.

  • @reksie7816
    @reksie7816 3 года назад +2

    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.

  • @BeWrecker
    @BeWrecker 2 года назад

    One of the best video available on solid principal, great effort man :)

  • @chandankumarmishra336
    @chandankumarmishra336 3 года назад +2

    you really did a wonderful work here...thanks a ton :) ...very well explained here...best in the market as of now ...cheers..

  • @mohamedhussien4013
    @mohamedhussien4013 5 месяцев назад

    Thank u so much for the great explanation.
    Real-world examples are awesome.

  • @bigbitesaint
    @bigbitesaint 3 года назад +6

    Thank you. This is exactly what I need. Subscribed =)

  • @odedkadshai
    @odedkadshai 3 года назад +2

    thank you, the aws file upload example is perfect

  • @antonfernando8409
    @antonfernando8409 2 года назад +3

    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! 👍

  • @raselhasan2433
    @raselhasan2433 Год назад

    Loved your content for using cpp, thanks.

  • @vinay_sac
    @vinay_sac 2 года назад

    This is very very clear explanation of SOLID principles.

  • @101Crock
    @101Crock 2 года назад +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! ⭐👍👍

  • @AJSquirrel53
    @AJSquirrel53 3 года назад +4

    This video was awesome! Subscribed!

  • @AlexDrastico380
    @AlexDrastico380 3 года назад +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.

  • @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! 🧡

  • @dhananjayah.s9796
    @dhananjayah.s9796 2 года назад

    Awesome explanation with code walk through

  • @estebanm.8668
    @estebanm.8668 3 года назад +1

    Fantastic explanation !

  • @errodememoria
    @errodememoria 2 года назад +1

    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 😊

  • @siedamout3904
    @siedamout3904 2 года назад

    Awesome explanation. Thanks!

  • @felipelopessss
    @felipelopessss 2 года назад +1

    Excelent video, thank you!

  • @CodeStation
    @CodeStation 3 года назад +10

    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 года назад +1

      Ooops! Good catch. I fixed it on the written versions of this tutorial.

    • @CodeStation
      @CodeStation 3 года назад +1

      @@platisd It's not a big deal. What matters is the explanation and getting your point across. Which you did! Congratulations!

  • @proxy8918
    @proxy8918 2 года назад

    Amazing video, thank you so much.

  • @kleanlins
    @kleanlins 3 года назад +2

    what a great video!

  • @chandankumarmishra336
    @chandankumarmishra336 3 года назад +1

    your blogs are worth reading...well done...

    • @platisd
      @platisd  3 года назад

      Thank you! :)

  • @arizona_ranger_connoisseur
    @arizona_ranger_connoisseur 3 года назад

    Φίλε μου, πραγματικά φανταστικός. Έχω παρακολουθήσει τουλάχιστον 2-3 courses και μπορώ ειλικρινά να πω, κανένα δεν με βοήθησε να καταλάβω τα solid principles. Αν και σε συντομία, τα εξήγησες πάρα πολύ απλά και παραστατικά και, βεβαίως, τα παραδείγματα στην cpp βοήθησαν και αυτά.

    • @platisd
      @platisd  3 года назад +1

      Ευχαριστώ πολύ Ηλία!

    • @platisd
      @platisd  3 года назад

      Αν τυχόν δεν είσαι ήδη μέλος, υπάρχει και το grcpp στο meetup και στο Facebook όπου διοργανωνονται εκδηλώσεις τακτικά πάνω σε θέματα σχετικά με C++ στα ελληνικά:
      facebook.com/grcpp
      www.meetup.com/grcpp-athens/

    • @arizona_ranger_connoisseur
      @arizona_ranger_connoisseur 3 года назад

      @@platisd Όχι δεν το είχα ακουστά, οπότε θα γραφτώ. Σε ευχαριστώ πολύ Δημήτρη.

  • @TheMorrigan
    @TheMorrigan Год назад

    Perfect !

  • @berksteraydo9517
    @berksteraydo9517 3 года назад +1

    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 года назад +2

      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. : )

  • @raimonestanol8234
    @raimonestanol8234 3 года назад +2

    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! :)

  • @chandankumarmishra336
    @chandankumarmishra336 3 года назад +2

    loved it totally....Do you have the complete source code for these ?

    • @platisd
      @platisd  3 года назад +1

      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/

  • @404lab9
    @404lab9 2 года назад +1

    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

  • @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 :))

  • @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.

  • @FrostGamingHype
    @FrostGamingHype 3 года назад +1

    some one distrub him with 9999 cups coffees so he makes a great video imagine getting these

  • @NaP608
    @NaP608 3 года назад

    Ty

  • @greatbullet7372
    @greatbullet7372 3 года назад +1

    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 года назад +2

    Thank you. Could you explain Design patterns in C++ or Rust?

    • @platisd
      @platisd  3 года назад +2

      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 года назад +1

      @@platisd Thats the case. I will like it in C++20. If you like the factory :D

    • @platisd
      @platisd  3 года назад +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 года назад +1

      @@platisd Mmm... I would prefer shorter b).

    • @axelman145
      @axelman145 3 года назад +4

      @@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  Год назад +1

      @@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 Год назад +1

      @@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 года назад +1

      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 года назад +1

      @@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 года назад +4

    I'm allergic to this amount of 'virtual' and dynamic polymorphism, this is not Java.