• 08/17/13 06:40 AM
    Sign in to follow this  

    Why Is It Building So Long?

    General and Gameplay Programming

    Fen
    • Posted By Fen
    Most of Object Oriented guides revolve around the word encapsulation: we should hide implementation details of a class and reduce coupling between objects as much as possible. But in C++ (and also in other compiled languages) we also need to care about the physical side of the design. How can we do that?

    Example

    Here is a 'common' style of class declaration: [code] #include "Engine.h" #include "Gearbox.h" #include "..." #include "Wheels.h" class Car { private: Engine m_engine; Gearbox m_gearbox; Wheels m_wheels; public: Car() { } virtual ~Car() { } void drive() { } private: void checkInternals(); } [/code] Assume the logic design is good. What are the problems related to physical side of such class? First thing: what if we add a method in the [tt]Engine[/tt] class? Not only the [tt]Car.cpp[/tt] will have to be compiled again (this is expected of course), but clients of the [tt]Car[/tt] class as well! Second thing: what if we change/add some private method or a private field of the [tt]Car[/tt] class? That way clients also have to be recompiled. What is the point? Why is it important? For small projects there is almost no difference between good and bad physical design. But when a project is large the need for recompilation means long (very long and even longer) building time. Do clients of a class need to be recompiled every time? Do they need to know its internal guts - private fields and methods? Additionally, although clients can assume that the (public) interface of a class will not change, it is not the same as with the compiler. It depends on all information in header files, so even if a private implementation of a module is changed, than every dependent module is needed to be compiled again. The tool that could help us in improving the physical side the design is called insulation, see below the result of applying that. Isn't that a bit better? [code] class Engine; class Gearbox; class Wheels; class Car { private: std::unique_ptr m_engine; std::unique_ptr m_gearbox; std::unique_ptr m_wheels; public: Car(); virtual ~Car(); void drive(); private: void checkInternals(); } [/code] Or even: [code] class Car { private: std::unique_ptr m_pImpl; public: Car(); virtual ~Car(); void drive(); // private methods 'moved' to source file... } [/code] Can you see the difference? Will it compile/build faster?

    About the book

    This article is a result of skimming (and some reading) of the book called "Large Scale C++ Software Design", by John Lakos. This is an advanced C++ book, but touches a unique topic in the area: how to design code not only from a logical perspective but from the physical side as well. Sometimes the writing style is a bit 'academic' and hard to understand quickly. But all in all it is a great source for everyone involved in creating something bigger than, let's say, 10,000 lines of code. Main flaw: it is a bit old: from 1996, and it even uses Booch diagrams... I wish there is a second, newer edition. But still a lot of theories are so general that even now we can use them.

    Insulation definition

    So what is this insulation? Let us see what author says about that:
    A contained implementation detail (type, data, or function) that can be altered, added or removed without forcing clients to recompile is said to be insulated.
    What can we do to reduce this physical coupling then?

    Arguments

    When we declare an argument in a method: [tt]void func(A a, B b);[/tt] the compiler has to know the details of [tt]A[/tt] and [tt]B[/tt]. So the simplest way is to include [tt]A.h[/tt] and [tt]B.h[/tt] at the top of our header. But actually we need such details only in the implementation of [tt]func[/tt], it is not needed in (pre)declaration. It is better to use const pointers or const references for such arguments (of course for built-in types there is no such need). When the compiler sees [tt]const A *a[/tt] it has to know only the name, not the full definition of a particular type.

    Members

    As with methods' parameters, we can use a similar technique regarding class members. It can be easily seen in our [tt]Car[/tt] class - the second version. This time we need only predeclarations of particular classes. Unfortunately this comes at a price: we need to allocate those members somewhere on the heap in the implementation file (in constructor for instance). For large members it is even better, but for small classes (for instance like Vector3) it can be too tricky.

    pImpl

    One of the most radical methods is to use the pImpl pattern. It can be seen in the third version of the [tt]Car[/tt] class. Everything private goes to a class named [tt]pImpl[/tt] that is declared and implemented in the cpp file. That way the client of [tt]Car[/tt] does not see its guts, and we do not have any additional includes and physical coupling.

    Private methods

    Keep private stuff private.
    When we see a class and its [tt]private:[/tt] block, we can assume how it works. It is better to diminish this block and move every method to an implementation file. How? It is not that easy unfortunately. We can create a static free function in [tt]cpp[/tt] file, but that way we have no access to private members of the class. It is pointless to create a [tt]static[/tt] member function of the class because it is almost the same as the original method (we need to declare this in header file as well). Sometimes it is good to go through all private methods and check if they really should be in this class? Maybe they should be removed or moved to some helper class, or even made a free function?

    Problems

    Insulation is not a silver bullet for everything. Is it good to insulate a small Point2D class? Probably not! We would cause more harm actually. Below there is a list of issues to consider before doing full, hardcore insulation:
    • Lower performance: when doing insulation lots of variables are placed on the heap, for small pieces of data it can be harmful.
    • It is not that easy to fully insulate a class. We need time (and skills) to do that.
    • Maybe our class is not used that often, maybe there will be no difference in compilation time whatsoever?
    Below there is a picture that could suggest when to insulate (grabbed from the book):
    [attachment=16160:insulate.png]

    Sum up

    It is just a tip of an ice berg! As I mentioned earlier I did not cover include topic and some other stuff like: enums, protocol class, handles and opaque pointers. But I hope that this gives you motivation to look at your code from a slightly different perspective. Reposted with permission from Bart?omiej Filipek's blog logo comes from: openclipart


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now


    hossainiir

    Report ·

      

    Share this review


    Link to review
    Fen

    Report ·

      

    Share this review


    Link to review
    Ectara

    Report ·

      

    Share this review


    Link to review
    edin-m

    Report ·

      

    Share this review


    Link to review