-1

I have built a library that is being used by a few people. They say that library has a few print statements which is wrong because a library should give freedom to users to use the library the way they want and forcing a few print statements is not good that way. Should there be print statements in a library by any ideology ?

  • 2
    Are the print statements for logging? – Erik Eidt Apr 30 '16 at 14:45
  • 3
    Possible duplicate of [Should You Log From Library Code?](http://programmers.stackexchange.com/questions/145078/should-you-log-from-library-code) – gnat Apr 30 '16 at 14:46
  • Not exactly. Just to print some info. like printing return value before returning it, so that when library is being tested as standalone, its output is visible. – Gaurav Kumar Apr 30 '16 at 14:47
  • 4
    Printing is a pretty big and unexpected side effect for a function. It's also going to slow things down. There are better ways to test things. It is ok (good even, see @gnat's reference) to log if you allow the choice of logging to be the library user's not the library's, and it includes choices to expand/limit verbosity. – Erik Eidt Apr 30 '16 at 15:05
  • @ErikEidt Plus, of course, when you use a library, there might not be a destination to print to. – Ross Patterson Apr 30 '16 at 23:33

1 Answers1

11

Absolutely not, no. I don't want your library spewing noise onto the console, into a log file or whatever else.

At least, that should be the default behaviour. If you really need it, have a way to set a "debug" flag which turns this kind of logging on. However, you shouldn't need that for normal testing - that's done by checking the return values in some kind of test harness, not by logging things.

Philip Kendall
  • 22,899
  • 9
  • 58
  • 61
  • 4
    Provide a debug flag, or a way to provide a custom file descriptor to which to write things, or even a print/log callback -- but give the policy control for such printing to the consuming application, please. – Castaglia Apr 30 '16 at 16:36