“The single most important reason to create a class is to reduce a programs complexity.”
Steve McConnell – Code Complete 2
“Always write code like the person who will maintain it is a serial killer and knows where you live”
Unknown
I’m going to assume you know what a class is and how to write one, so if this is not true, I suggest you look that up before reading this article.
So why or when should we create a class? This is one of those great theoretical questions, but has many real world consequences. There are many specific reasons to create a class. McConnell goes into detail identifying many reasons and elaborating on them. Since I have no desire to transcribe his wonderful book or write a book length article, I’ll point you in his direction after you’ve read this more entry point article.
For a lot of beginning programmers, classes are seen as real world objects. A button, a menu, or a gallery. While these are valid to be represented in their own classes, it isn’t the right reason to have put them in a class. For me a class is everything needed to contain a single responsibility. So while managing the display and control of a menu is a responsibility, so is parsing an xml file or grouping and representing the data that constitutes an employee. So creating classes strictly to represent real world objects doesn’t go nearly far enough. Continue reading