So a coworker and I were having a friendly debate about something and I'd like get the opinions of you fine folks.
In short: Do you think it's good style for a method that is normally expected to return a string to return undef in some cases?
In particular: The Sys::Info::OS module has a name() object method. It's normally is expected to return the operating system's name. The example given in the module's POD offers this as sample usage:
printf "Operating System: %s\n", $os->name( long => 1 );
The problem is that on certain linuxes, the name() method returns undef, which causes the above example code to output something like this:
Use of uninitialized value in printf at /tmp/test.pl line 20. Operating System:
The debate: I'm of the opinion that -- in general -- returning undef isn't a great idea. For subroutines, I think it's fairly terrible due to a caller's possible scalar or list context, which is unknown to the subroutine author and certainly of his control. For methods that are expected to be used in concatenation and the like, as in the example above, I think it's a little rude.
My coworker says that it's up to the end user to check for a valid string being returned. I say that there's nothing in the docs which says that undef might sometimes be emitted and that it would be more polite to have it return the output of 'uname -o -r' or 'unknown' or whatever. It sets up the expectation that a string will be returned, and then forces you to deal with a possible undef. Does every method call return undef sometimes?
I mean, it's not hard to do this instead of the module's example:
my $osname = $os->name() ? $os->name() : `uname -o -r`; chomp($osname); print "Operating System: $osname\n";
But there's a part of me that says it'd be better to have the name() method do that rather than require it for (I suppose; I don't know) every method call the module offers. Where else might there be errors waiting to be checked for?
Also, this comes from code that was shipped to a customer. We've got perhaps 6 various Linuxes in house (including a very old version of RHEL), and all of them print the OS name reliably. The customer got the "Use of uninitialized value ..." error, and we, frankly, looked like chumps because of it.
It's nothing earth-shattering, and the fix was easy, but it's the genesis of the debate. I think it's not really our fault since the module doesn't do what it says it would do (the is_unknown() method notwithstanding). The coworker claims it is our fault due to shoddy error checking. And I can certainly see his side of things since we shipped the code, after all. We're ultimately responsible for what it does.
Your thoughts?
In reply to A question about method return values and error checking by wee
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |