Classes considered overused

We all love classes. When I write a class, I get a warm and fuzzy feeling, because I know I'm doing OOP.

A class represents a bundle of data and behavior. A classic example is a BankAccount class, which maintains a "balance" state and various methods for manipulating and querying the balance.

This is a great concept. It lets us think about our programs at a higher level.

The problem with classes is side effects. It's a lot harder to test a function when it depends on internal state — and that's what the "data" part of our data and behavior bundle is. It's harder still when calling the function may twiddle this internal state, which will in turn affect subsequent behavior.

It's easiest to test a function when a given input is guaranteed to produce the same output, no matter when or how many times it's called (i.e. a "pure function"). This yields code that's more reliable, more testable, and easier to reuse.

I don't think it's wise to completely forgo classes though, at least in languages like C++ and Python. They make large-scale development a lot easier. But these languages do give us tools for organizing code that the "classes-only" one-trick-pony languages don't — namespaces in the case of C++, and modules in the case of Python. So we don't have to resort to classes merely to organize related functionality.

Python also gives us functions as first-class objects (and boost does this for us in C++), eliminating another need to use classes: we can pass around functions rather than "command objects". A command object is fine when the command needs to retain state (e.g. for implementing undo/redo), but when a simple function will suffice, use a function.

When I'm developing new functionality, I try to use plain pure functions as much as possible, only using classes when I actually need to wrap state. C++ and Python give us this power, because they don't put us in the class straitjacket.

3 comments to Classes considered overused

  • Exactly my thoughts! The simplest unit of a program is a line of instruction, and we should be able to just write that statement and make the program execute. There should be no need to wrap it in myriad wrappers like functions, classes, packages etc if its not necessary.

  • Ryan Ginstrom

    “There should be no need to wrap it in myriad wrappers like functions, classes, packages etc if its not necessary.”

    Sounds like a good restatement of “do the simplest thing that could possibly work.”

  • Assaf

    stateless code is always easier to test, manage and to scale. I totally agree that in many times classes become very difficult to evolve. As you grow smarter about how you want to run your application, all of your previous decisions about the code-data packaging (classes) become sort of a deadlock and would require huge efforts to re-factor.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>