tags:

views:

166

answers:

5

So I've been developing a UI toolkit for the past year, and my Window class has gotten to a point where the size of the class (through sizeof) is 540 bytes).

I was thinking, that since not all windows have children, I might split parts of the code that handles having children (its alignment etc) into a separate class and have a pointer to it. Same goes with texts, icons, and scrolling etc.

Is this recommended practice or is there anything I'm blatantly missing?

A: 

I wouldn't worry about the raw size of the class. However, I would consider using the PIMPL idiom or a D-pointer.

rlbond
Ah thanks, after looking at those links it got me another question.. would the extra pointer access needed affect speed in the long term, especially as the class is used quite a lot (it's a UI window class)..
kamziro
If it's a UI window class, it would almost certainly be negligable, since the user's input is typically a bottleneck.
rlbond
@kamziro: Does your window get updated 50 million times per second? If not, it isn't going to be performance-critical. A memory access takes in the order of 2-100 nanoseconds (if it is used often, the CPU caches it, and then it'll be 2, not 100). So you do the math on how often it'd have to follow this pointer, for it to matter performance-wise. :)
jalf
+2  A: 

Object-oriented design of classes should be based on the abstraction of the problem at hand, not the size in bytes of the compiled code. I'd break up a class if it was doing too much.

540 bytes? Do you really mean kbytes here? That doesn't seem excessively large to me.

duffymo
I think this is about the size of the data, not the size of the compiled code: "size of the class (through sizeof)". You can't apply the sizeof operator to code.
bk1e
@bk Size of code usually means the source code file size / line count.
KennyTM
@bk1e - you're probably correct, but my comment still stands. Break up the class because it's doing too much.
duffymo
+2  A: 

Class instance size isn't a reliable metric of code complexity. An overly complex class that does everything could easily be the same size as a well-designed class that is composed of several smaller objects with clear responsibilities.

A class that uses composition can instantiate the composed objects as member variables (resulting in a larger instance size), or it can contain pointers to separate objects allocated using the new operator. The latter approach has benefits, such as physical decoupling, polymorphism, and not paying for features you don't use, but it also has downsides, such as increased memory fragmentation, increased memory allocator overhead, and reduced spatial locality.

bk1e
A: 

I may be misunderstanding your problem, but you may want to look into policy-based design and/or component-based design, both of which are concerned with making larger classes out of smaller pieces of functionality.

From what you said about splitting some functionality off and having a pointer to it, it sounds like component-based design is the direction you were thinking of.

Narfanator
+2  A: 

First, the size of an object instance in itself doesn't really matter. The class should be designed to have a single responsibility, and if that requires 540 bytes of data, then so be it.

However, 540 bytes is a unusually big number. It's 135 integers or pointers. It's something like 22 std::vectors. I have a hard time imagining that so many objects can be required for a single area of responsibility. But again, the size itself isn't the problem. The problem is if your class is responsible for more than it should.

I was thinking, that since not all windows have children, I might split parts of the code that handles having children (its alignment etc) into a separate class and have a pointer to it. Same goes with texts, icons, and scrolling etc.

It sounds like you've basically got a single EverythingUI class. And yes, that is bad design. Split it up for that reason. Handling of text has nothing to do with handing of icons or scrolling or... Put them in different classes.

jalf
Haha, you got that exactly right, texts, icons and scrolling (and children). I've split the functionalities into modules, and so the text module is only there if there's text needed. Same with scrolling/children and also icons, and also appearances (because a lot of the UIbits I have are invisible, for layout only).It's down to 136 bytes now. If all the submodules are added, it's now 412 bytes. A lot of the bytes were removed by using shorts instead of ints (for UI, I think shorts suffices), using bitfields and getting rid of unnecessary variables, which turned out to be plenty in the code.
kamziro
That is *generally* a bad idea. Don't replace ints with shorts just to save space. Ints are faster for the CPU to deal with, and a few bytes more or less make absolutely no difference. If you need to store that many variables, do it. Like I said in my post, the size in itself isn't the problem. The problem is if one class is responsible for too many different things.
jalf