Re: Gigantic Class
On Dec 27, 5:43 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
Immortal Nephi <Immortal_Ne...@hotmail.com> writes:
However, many small specialized classes are preferred to a bulky
Such metrics look superficial to me. For example, when the
specialized classes are very similar to each other, they
should be combined into a slightly larger but more general
class (DRY). It always depends also on other factors then
just the size in isolation.
members. But bulky classes do get written. Class std::string has a
Yes, yes! Go ahead and write it! It is never a mistake to
write a huge bulky class. It is only a mistake to stop there
and not refactor it. Usually, /after/ the bulky class has been
written one sees oportunities to split it or to extract classes
from it, thus it gets smaller, usually. And in the 0,1 % of all
cases, when there is no reasonable refactor to make it smaller,
it really needs to be that big. Usually, =BBgod class=AB is an anti-
pattern, but under rare circumstances it might become a pattern.
"god class"? Are you referring that giant class A is perfect with
free bugs unless you have already debugged and tested and it is to be
working properly?
A typical cell size is 10 =B5m, but the ostrich egg cell is
15 centimetres (5.9 in) long, 13 centimetres (5.1 in) wide,
and weighs 1.4 kilograms (3.1 lb).
class? Inheritance is the answer, but you write derive class to use
This is just one answer. And also seen to be an anti-pattern by
some (as implementation inheritance - not interface inheritance).
If inheritance is not the answer because protected data members and
protected member functions are not defined, what is the solution? You
might say that composition is the answer. Create hundreds of base
subclasses. Put them into one base main class. How can base
subclasses access base main class' private data members?
I showed you my example as class A, B, C, and D through public
inheritance above. Convert from inheritance to composition. Let's
say for example. class B, C, and D are considered to be base
subclasses and class A is considered to be base main class. Class B,
C, D can't access class A's private data members unless you add friend
class B, C, and D in class A. Class A needs to access class class B,
C, and D's member functions before class B, C, D's member functions in
turn to access class A's private data members directly.
class B { public: /* member functions access A's private data members
*/ };
class C { public: /* member functions access A's private data members
*/ };
class D { public: /* member functions access A's private data members
*/ };
class A
{
friend class B
friend class C
friend class D
public: /* member functions access class B, C, D's member functions */
private: /* private internal states as data members */
B b;
C c;
D d;
};
Class A's data members are always undefined because class A body is
not declared in the top before class B, C, and D are defined. I guess
that composition is not the answer. How do you have the solution?