3

I have a written a function and intend to provide an input variable so that when it is true, user-information is printed to the console. If the variable is false the function is 'silent'. Console data is not necessarily for debugging purposes.

In the course of choosing a name, I came up with "verbose" and this led me to wonder is there a standard \ common variables nomenclature that would be better suited for this variable, given the context above?

2
  • Verbose implies that there's a non-verbose option. How about ShowInfo? Jun 19, 2018 at 16:50
  • 5
    -v / --verbose is the standard name for a command line flag that does the same thing. Jun 19, 2018 at 17:49

3 Answers 3

8

I'd suggest that the parameter name is fairly irrelevant here as, no matter what you call it, SomeFunction(true) tells me nothing of what that true does. Avoid using boolean/flag parameters. Use enums or two methods, with different names that describe their difference, instead.

7

You might as well pass the destination of the output as the parameter to the function.

A program may or may not have verbose command line parameter, but I can always silence it by redirecting its output to /dev/null

6
  • Oh, I like this idea even more than my own. Inject the destination for the win! :)
    – David Arno
    Jun 19, 2018 at 19:30
  • 1
    An awesome idea from the user named null :) Jun 19, 2018 at 21:10
  • I might add a "feature" that a no-arg call outputs to stdout. (Or whatever mekes the most sense to you)
    – user949300
    Jun 20, 2018 at 0:02
  • While clever and transparent, this may not be the best solution if the computation of the output is expensive and you want to skip it if display isn't requested. You'd want to wrap the computation in an if block-- what would you check? The null output would amount to a magic number.
    – John Wu
    Jun 20, 2018 at 1:34
  • @JohnWu in the case of expensive output just shunt it into a polymorphic method that does the expensive work when outputing and does a bunch of very fast nothing when not. No if block. No check. Just doing what needs doing. Jun 20, 2018 at 2:42
4

Presuming Java, and that you're determined to do this yourself rather than use a proper logging facility, I'd prefer to see something like:

outPort.println("Hello world");

... where outPort defaults to a null object unless you passed in something like:

someFunction(System.out);

... to override the default.

Now sure, you could just reconfigure System.out or redirect output when you run the program, but that's all or nothing. If you have 15 objects that talk but you only want to hear from one of them it's nice if you can just tell each of them what to talk to.

You can send output to "/dev/null" or "NUL:" if you know your OS. But if you want to not care here's a quick and dirty version of a null object that will stop the noise before it ever gets magic file names involved.

java.io.PrintStream outPort = new java.io.PrintStream(
    new java.io.OutputStream() { @Override public void write(int b){} }
);

This null object does nothing quietly when called. It's a good default if quiet is your desired typical behavior. Here I'm using an anonymous class because I'm lazy. You can make a more robust class and give it a proper name if you like.

These tricks work in most languages if you change the right names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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