Java 8 – A Filter By Any Other Name Would Probably Smell Sweeter

I’m thinking the choice of name for the filter function in Java 8 steams was a really poor one.

I mean, what is a filter? Something that keeps things out, or something that catches the things we want?

Yes.

Dictionary.com has several definitions, from “any of various analogous devices, as for removing dust from air or impurities from tobacco smoke, or for eliminating certain kinds of light rays” to “to remove by the action of a filter”.

An air filter, we hope, will keep harmful airborne particles away. We also hope a filter in a sanitation plant will do similar work. (We should all know that the filter on a cigarette is only there for the sales pitch…)

I’m thinking gold-digging would use filters with the opposite purpose, but you pan for gold, don’t usually filter… I guess…

The guy that came up with the name “filter” might be a physicist and using this dictionary.com definition of filter: “Electronics, Physics. a circuit or device that passes certain frequencies and blocks others.”

But thankfully that’s not how the Streams-documentation suggests the filter-function should work…

Or the name was inspired by coffee filters? … no, wait, that’s very wrong!

Regardless…

Using the name “filter” for the function that will decide, like the gold diggers filter (that doesn’t really exist… ok, a physicist’s “circuit or device”… 😐 ) what to keep, seems very unintuitive to me, and I suspect it requires that you learn that in Javaspeak, “filter” means only one of the two possible definitions of filter (of which the “wrong” one seems über quasi… or like you take your coffee the very caffeinated way… with a spoon…)

I suggest adding “include” and “exclude” instead and then one of them (or both) can redirect to filter behind the scenes. (I’m inspired by Rsync…)

I’m thinking it should be quite hard to misunderstand those two. (And very hard to think using “filter” is a better choice)…

Writing programs in programming languages is, just as when you write in any other language, definitely communication with computers, other programmers and yourself in six months when you’ve forgotten what the hell the program code you’re staring at was supposed to do… And poor communication is the root of all evil and the end of all marriages… or so I’ve heard… 😀

I’m certain there are bugs out there right at this moment because of this language choice. (I’ll let you know how many of them are mine as I find them…)

(And by the way, the thing that catches the things you want is called a net, not a filter… maybe filter should be renamed to “net”? Since “catch” already is a keyword… :P)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.