0

I was writing some Java 8 Streams code. I have a list of files, but I want to keep all large files over 100 MiB.

// Filtering large files
details = details.stream()
        .filter(fileDetails -> fileDetails.getSize() > HUNDRED_MEBIBYTES)
        .collect(Collectors.toList());

The filter Javadoc says:

Returns a stream consisting of the elements of this stream that match the given predicate.

Therefore I have to pass a predicate describing what I want to keep.

Writing that // Filtering large files comment gave me an uneasy feel, as I was hesitating on whether saying "Filtering X" meant keeping X or discarding X. I tried to think about it but then became confused (I've rewritten that comment // Keep large files only since).

Here's the heart of the question:

  • If I am filtering water into a cup using a water filter, the cup will contain filtered water, and we discarded impurities.

  • If I am filtering air entering into a room using a air filter, the room will contain filtered air, and we discarded dust.

  • But if I am filtering air entering into my computer case using a dust filter, the computer will contain filtered air, and we discarded dust.

It seems that "filtering X" always mean "I want X and discard Y", but a "Z filter" would either mean "I want Z and discard Y" or "I want X and discard Z". Is it true? Is no one ever filtering impurities, or filtering dust?

Following that logic, I am always filtering large files (I would not say in this case I am filtering small files), but it is ambiguous whether I should have a "large files filter" or a "small files filter".

I have to admit, sometimes I don't remember whether the filter method is meant to have a predicate describing what I want to keep or what I want to discard, for example the following code does not do what one expects at first glance:

allMiddleNames = users.stream()
        .map(User::getMiddleName)
        .filter(String::isEmpty) // FIXME: This is wrong, should be "s -> !s.isEmpty()"
        .collect(Collectors.toSet());

Is there a clear, deterministic use of the word filter, particularly when used with the following expressions?

  • I am filtering X (filtering is a present participle)

  • I have a X filter (filter is a noun)

  • My container contains filtered X (filtered is an adjective)

  • I have filtered X (filtered is at the past tense)

  • Filtering X would not always connote "I want X." With no context, it would be more likely to connote the opposite, to this reader at least. There may be a standard usage in coding jargon, but idiomatically filtering out X would always connote excluding X. Filtering for X could connote including X. – P. E. Dant Reinstate Monica Sep 16 '16 at 3:52
1

Consider the definition below. I have selected three examples from the dictionary - the first is the neutral sense, the second has the filter-out sense, and the third has the filter-in sense.

Filter verb 1 [with object] Pass (a liquid, gas, light, or sound) through a device to remove unwanted material.

  • Fish sauce is what you get when you leave anchovies or similar small fish to pickle in brine until nearly dissolved, and then filter the resulting liquid.
  • We have been given a special machine for our living room which filters pure oxygen into his lungs.
  • the eye filters out ultraviolet radiation

- ODO

Technically, what you are filtering is the stream of things going into the filter. Using the term filter on its own is ambiguous in relation to whether things are removed or retained because both actions are intrinsic to filtering.

In your example, you are filtering the files, and retaining the large ones. Your comment can simply say retain large files.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.