User forums > General (but related to Code::Blocks)

How not to write C++

(1/2) > >>

Krice:
Since I see people not getting C++ (more specific: OOP) I'm going to spill it out. Let's take this example from C::B (app.cpp):


--- Code: ---class Splash
{
    public:
        Splash(const bool show) : m_pSplash(nullptr)
        {
            if (show)
            {
                wxBitmap bmp = cbLoadBitmap(ConfigManager::ReadDataPath() + _T("/images/splash_1312.png"));
                wxMemoryDC dc;
                dc.SelectObject(bmp);
                cbSplashScreen::DrawReleaseInfo(dc);
                dc.SelectObject(wxNullBitmap);
                m_pSplash = new cbSplashScreen(bmp);
                Manager::Yield();
            }
        }
        ~Splash()
        {
            Hide();
        }
        void Hide()
        {
            if (m_pSplash)
            {
                m_pSplash->Destroy();
                m_pSplash = nullptr;
            }
        }

    private:
        cbSplashScreen* m_pSplash;
};

--- End code ---

If this is a local code where you get resources, show them and release right after it, you don't even need a class. This class works a lot like function anyway which is a common mistake. But if it is a dynamic resource (the splash screen is kept in memory) then you always want to avoid conditional construction. Do not pass that show boolean to the constructor, but construct the object's data and release it in the destructor. Then use a Show function to show the splash screen. If you are resetting some pointer to nullptr you are most likely doing it wrong. By following constructor/destructor rule you don't have to check out pointers etc.

As a side note this class probably also has a missing delete, at least there is a new but no delete. Might be some wx magic, but not sure about it.

But the main point is that this is not a class. It's a function and if you have something like this you don't even need a function, you could simply write the code inside a if block (if the resources are local and used only in one place).

oBFusCATed:
Can you show a working patch which makes the code better?  ::) 8)
But make sure to test at least on two operating systems.

Krice:
I don't know anything about wx to code a patch, and even if I did it wouldn't change the fact that the entire source code is what I call "old school C wrapped in classes". If you use classes like that you miss the important things in object-oriented programming, in particular using classes the way they were designed. I think so many programmers miss the point, because C++ is a multi-paradigm language which doesn't force to OOP style only. That's why the bulk of code can be ancient C style "linear" (no generic concepts or data-driven approach) code with macros etc.

In my opinion OOP benefits from generic style programming where classes are mostly used as datatypes that reduce the amount of specific, bulky coding style of C. OOP also works well with modular programming - another concept a lot of programmers don't actually get in a way that would help them to manage large projects. It's something that even I have discovered only recently. But after all things said, it is possible to write better procedural code, too, with generic/modular style. The problem with old school C is not that it works, it does work obviously, but that it creates more code than you would need and a bigger project is always harder to maintain. It's quite clear this is a problem in many of these large and even medium size C open source projects. That's why developers often tell to "patch" them, because it's the only way they can work around the big problem.

oBFusCATed:
A-ha, I understand.  :o

sodev:
I see people who never coded a GUI application or have seen an event loop ...


--- Quote from: Krice on June 29, 2018, 11:01:51 am ---As a side note this class probably also has a missing delete, at least there is a new but no delete. Might be some wx magic, but not sure about it.

--- End quote ---
Yes, it is wx magic, dont delete stuff or the event loop might access deleted objects.


--- Quote from: Krice on June 29, 2018, 11:01:51 am ---But the main point is that this is not a class. It's a function and if you have something like this you don't even need a function, you could simply write the code inside a if block (if the resources are local and used only in one place).

--- End quote ---
If you can show how to transfer control flow from that local block to a different location, execute an arbitrary number of operations and then return back into that local block to clean up, go ahead.


--- Quote from: Krice on July 04, 2018, 11:23:02 am ---In my opinion OOP benefits from generic style programming where classes are mostly used as datatypes that reduce the amount of specific, bulky coding style of C.

--- End quote ---
I always thought OOP is about attaching behavior to data instead of beeing just plain data?


--- Quote from: Krice on July 04, 2018, 11:23:02 am ---That's why developers often tell to "patch" them, because it's the only way they can work around the big problem.

--- End quote ---
And the big problem is? Features don't code themself?

You don't happen to work in one of these consulting companies and are more busy tying a tie than coding code?

Navigation

[0] Message Index

[#] Next page

Go to full version