Six Ways for Implementing Math Expressions Calculator in C++ - Amir Kirsh - CppCon 2023

Learn six techniques for implementing math expressions calculator in C++: inheritance, composition, templates, immutability, variadic templates, and delayed inheritance, and discover how to write more efficient, flexible, and maintainable code.

Key takeaways
  • Inheritance is not always the best solution, sometimes it can be replaced with other techniques like Composition or simple classes and functions.
  • Hide implementation details, like using unique ptr instead of raw pointers, to make the code easier to use and understand.
  • Use templates to simplify code and make it more flexible.
  • Favor immutability and value semantics over pointers and ownership.
  • Use variadic templates to implement generic code that can handle any number of arguments.
  • Sometimes it’s better to delay inheritance and use composition instead.
  • The code can be made more concise and expressive by using C++17 features like constexpr and std::make_unique.
  • The use of templates can help to avoid runtime polymorphism and make the code more efficient.
  • Polymorphism is not always necessary, it’s often possible to achieve the same results with simpler code and composition.
  • Composition is a more flexible and extensible way to design classes and systems.
  • In some cases it’s better to use a factory function instead of inheritance to create objects.
  • Compiler error messages can be very helpful in understanding the code and fixing errors.
  • Code should be self-explanatory and easy to understand for others, without needing a long explanation of the code.
  • The use of abstractions and interfaces is a powerful tool for creating reusable and maintainable code.
  • Simplicity and conciseness of code are important for maintainability and readability.