Search code examples
javafunctionvariablesvariable-names

Type name choosing :: An Interface name for functions returning a value


It's always hard to choose better variable/Type names. I am stuck choosing a name for an interface

I want to classify functions based on whether they return values or not. Here are some choices I made and why I rejected them.

ReturningFunction  : a function which doesn't return is hung. I don't want to confuse
OutputtingFunction : a function which outputs to stdout can return void so we can't call it with this name
VoidFunction       : a function which is void?

I tried googling but no luck. Please suggest some names :)


Solution

  • You may wish to reuse the interfaces provided by Java or, if they're not sufficient, follow their naming convention in the java.util.function package:

    Similar to VoidFunction:

    Consumer<T> Represents an operation that accepts a single input argument and returns no result.

    BiConsumer<T,​U> Represents an operation that accepts two input arguments and returns no result.

    Similar to ReturningFunction:

    Function<T,​R> Represents a function that accepts one argument and produces a result.

    BiFunction<T,​U,​R> Represents a function that accepts two arguments and produces a result.

    Supplier<T> Represents a supplier of results.

    For "outputs to stdout", you probably don't want an interface at all. Instead, you'd want a concrete class like the following:

    final class SystemOutConsumer<A> implements Consumer<A> {
    
        private final BiConsumer<PrintStream, A> printStreamABiConsumer;
    
        public SystemOutConsumer(
                final BiConsumer<PrintStream, A> printStreamABiConsumer) {
    
            this.printStreamABiConsumer = printStreamABiConsumer;
        }
    
        public void accept(final A a) {
    
            printStreamABiConsumer.accept(System.out, a);
        }
    }