Search code examples
oopmemberinstance-variablesdecomposition

Is a low number of members in a class considered a code smell?


I am currently making a simple to-do list program using the MVC pattern, and thus have a model class for the Notebook. However, something feels "off" as it has a very low number of members.

The Notebook is composed of categories, which are composed of To-do lists, which are composed of Items.

What I cannot place is whether this is a case poor analysis (e.g. there are more members and responsibilities I am just missing them..) or perhaps a code smell that the class is not needed (in that case I'm not sure what to do as I could just have a list of categories in that controller, but then I don't have a notebook entity modelled which seems wrong as well).

Below is the very simple class I have:

class Notebook
{
    private String title;
    private List<Category> categories;

    public Notebook(String title, List<Category> categories)
    {
    }

    public void setCategories(List<Category> categories)
    {
    }

    public List<Category> getCategories()
    {
    }
}

I often have this issue where it feels like I am making classes for the sake of it and they have a very number of members/responsibilities, so it would be nice to know whether I am stressing for no reason or not.


Solution

  • Not necessarily, there is the concept in Domain Driven Design of what is called a "Standard Type". Which is really a basic primitive wrapped in an object class. The idea is that the primitive contains no information about what information it contains, it's just a string/int/whatever. So by having say an object that surrounds the primitive and ensures that it is always valid ensures that the object has a meaning far beyond just the primitive it contains e.g. a Name is not just a string, it's a Name.

    Here's an example taken from the comments of Velocity

    public class Velocity
    {
        private readonly decimal _velocityInKPH;
    
        public static Velocity VelocityFromMPH(decimal mph)
        {
           return new Velocity(toKph(mph));
        }
    
        private Velocity(decimal kph)
        {
           this._velocityInKPH = kph;
        }
    
        public decimal Kph
        {
           get{ return this._velocityInKPH; }
        }
    
        public decimal Mph
        {
           get{ return toMph(this._velocityInKPH); }
        }
    
        // equals addition subtraction operators etc.
    
        private static decimal ToMph(decimal kph){ // conversion code }
        private static decimal ToKph(decimal mph){ // conversion code }
    }