PDA

View Full Version : Clean way to keep all private variables in class ( private struct, pimpl idiom )



atomic
2nd March 2015, 22:40
Hi, I would ask, how you keep private variables in the class? If there are a few then it is not a problem but now I have already over a dozen variables in class and I look for some way to do it more "clean".

Now I use private structure to storage all my private variables, for example



class CustomClass
{
struct Data {
QTableWidgetItem * hoverItem;
QLabel label;
QString text;
QAction file;
QAction save;
// other variables
};

public:
CustomClass();
~CustomClass();

private:
Data data;
};


I also read about pimpl Idiom
http://qt-project.org/wiki/Dpointer.

How you are doing it in daily work?
Thanks,

Alundra
3rd March 2015, 03:26
pimpl is only really needed if you need platform-independent data or wanting to hide a lib data.
if you don't need to hide data, just stay with private data.

Santosh Reddy
3rd March 2015, 07:29
Hi, I would ask, how you keep private variables in the class?
It will sound silly but it is the fact. You have to declare under the "private:" section in the class.


If there are a few then it is not a problem but now I have already over a dozen variables in class and I look for some way to do it more "clean".
I think you should try to have more organized code.


Now I use private structure to storage all my private variables, for example
You just pushed your "non-clean" private variables to another structure, IMO now the structure is "not-clean".


I also read about pimpl Idiom
By using private implementation, you will me moving the all the variable (non-clean) variables from *.h to *.cpp file. IMO now the *.cpp file is not clean.


How you are doing it in daily work?
I personally give attention to organizing the classes, structs by their functionalities. In a case there are more private variables (more than 15/20) then I consider that the class / struct is taking too much functionality it be better have some nested classes/struct to manage smaller functions or if any of these smaller functions are of general in nature then split the class itself. Make is just a guideline not rule to your code.

atomic
3rd March 2015, 08:55
Maybe I wrong name this post, I thought about a large number of private variables as you say > 15.
Sometimes this "data" structure can eliminated conflict of names and you do not need create prefix to variables like _m or similar.
Thanks,

Alundra
3rd March 2015, 15:37
Coding rules are important, and that's important to keep these rules during all the development.
For class member, keep the "m_" or just "m" front of variables.
Use "Set", "Get", "Count" and "Compute" in function's names.

stampede
3rd March 2015, 21:08
pimpl is only really needed if you need platform-independent data or wanting to hide a lib data.
Or you want to shorten the rebuild time (change in the class internals will require to rebuild only the class.cpp file). Very useful in large projects IMHO.

Kryzon
4th March 2015, 07:12
Hi, I would ask, how you keep private variables in the class? If there are a few then it is not a problem but now I have already over a dozen variables in class and I look for some way to do it more "clean".

I just resort to proper naming, commenting and using whitespace to separate members into logical blocks.


class CustomClass
{
Q_OBJECT

public:
CustomClass();
~CustomClass();

private:
// Widgets.

QTableWidget* m_mainTable;
QTableWidgetItem* m_hoverItem;
QLabel m_label;

// QActions.

QAction m_fileOpen;
QAction m_fileSave;
QAction m_fileClose;

// Data.

QString m_text;
QString m_lastFile;
};
Do you have name conflicts right now? If you're just planning ahead to avoid it, it might be a case of you being overzealous:
http://c2.com/cgi/wiki?YouArentGonnaNeedIt

Although its not a formal source and you have to be careful with what you look at, I sometimes like to reference the source code of other Qt applications to look for stylistic choices and patterns: http://qt-apps.org/

wysota
4th March 2015, 08:07
For complex cases I use pimpl-like solution, for simple ones I use private members directly. I don't consider it cluttering the file. Pimpl is also fine if you want to hide the internal representation of the object when implementing a library.