This is an AMP version of the article, its original content can be found here.

Why InputStream Design Is Wrong

It's not just about InputSteam, this class is a good example of a bad design. I'm talking about three overloaded methods read(). I've mentioned this problem in Section 2.9 of Elegant Objects. In a few words, I strongly believe that interfaces must be "functionality poor." InputStream should have been an interface in the first place and it should have had a single method read(byte[]). Then if its authors wanted to give us extra functionality, they should have created supplementary "smart" classes.

This is how it looks now:

What's wrong? It's very convenient to have the ability to read a single byte, an array of bytes or even an array of bytes with a direct positioning into a specific place in the buffer!

However, we are still lacking a few methods: for reading the bytes and immediately saving into a file, converting to a text with a selected encoding, sending them by email and posting on Twitter. It would be great to have the features too, right in the poor InputStream. I hope the Oracle Java team is working on them now.

In the mean time, let's see what exactly is wrong with what these bright engineers designed for us already. Or maybe let me show how I would design InputStream and we'll compare:

This is my design. The InputStream is responsible for reading bytes from the stream. There is one single method for this feature. Is it convenient for everybody? Does it read and post on Twitter? Not yet. Do we need that functionality? Of course we do, but it doesn't mean that we will add it to the interface. Instead, we will create supplementary "smart" class:

Now, we want to read a single byte from the stream. Here is how:

The functionality of reading a single byte is outside of InputStream, because this is not its business. The stream doesn't need to know how to manage the data after it is read. All the stream is responsible for is reading, not parsing or manipulating afterwards.

Interfaces must be small.

Obviously, method overloading in interfaces is a code smell. An interface with more than three methods is a good candidate for refactoring. If methods overload each other—it's serious trouble.

Interfaces must be small!

You may say that the creators of InputStream cared about performance, that's why allowed us to implement read() in three different forms. Then I have to ask again, why not create a method for reading and immediately post it on Twitter? That would be fantastically fast. Isn't it what we all want? A fast software which nobody has any desire to read or maintain.