views:

197

answers:

10

I'm rewriting a series of PHP functions to a container class. Many of these functions do a bit of processing, but in the end, just echo content to STDOUT.

My question is: should I have a return value within these functions? Is there a "best practice" as far as this is concerned?

+2  A: 

Do not return a value if there is no value to return. If you have some value you need to convey to the caller, then return it but that doesn't sound like the case in this instance.

Zach
A: 

You should just:

return;
Kevin Crowell
+3  A: 

Perhaps you should read http://en.wikipedia.org/wiki/Command-query_separation

Igor Brejc
A: 

In my opinion the SRP (single responsibility principle) is applicable for methods/functions as well, and not only for objects. One method should do one thing, if it outputs data it shouldn't do any data processing - if it doesn't do processing it shouldn't return data.

Björn
+4  A: 

Can the processing fail? If so, should the caller know about that? If either of these is no, then I don't see value in a return. However, if the processing can fail, and that can make a difference to the caller, then I'd suggest returning a status or error code.

Harper Shelby
Or you could raise an exception
Orion Edwards
Only if it's an exceptional condition. If processing failure is relatively normal, then no, don't. I'm one of those people who thinks an EOF exception when reading a file is rather bizarre, as EOF is an expected condition.
Harper Shelby
+1  A: 

I will often "return: true;" in these cases, as it provides a way to check that the function worked. Not sure about best practice though.

da5id
+4  A: 

In systems that report errors primarily through exceptions, don't return a return value if there isn't a natural one.

In systems that use return values to indicate errors, it's useful to have all functions return the error code. That way, a user can simply assume that every single function returns an error code and develop a pattern to check them that they follow everywhere. Even if the function can never fail right now, return a success code. That way if a future change makes it possible to have an error, users will already be checking errors instead of implicitly silently ignoring them (and getting really confused why the system is behaving oddly).

Mr Fooz
+1  A: 

Note that in C/C++, the output functions (including printf()) return the number of bytes written, or -1 if this fails. It may be worth investigating this further to see why it's been done like this. I confess that

  1. I'm not sure that writing to stdout could practically fail (unless you actively close your STDOUT stream)
  2. I've never seen anyone collect this value, let alone do anything with it.

Note that this is distinct from writing to file streams - I'm not counting stream redirection in the shell.

Brian Agnew
+1  A: 

To do the "correct" thing, if the point of the method is only to print the data, then it shouldn't return anything.

In practice, I often find that having such functions return the text that they've just printed can often be useful (sometimes you also want to send an error message via email or feed it to some other function).

In the end, the choice is yours. I'd say it depends on how much of a "purist" you are about such things.

Orion Edwards
A: 

There is no need to return anything, or indeed to have a return statement. It's effectively a void function, and it's comprehensible enough that these have no return value. Putting in a 'return;' solely to have a return statement is noise for the sake of pedantry.

chaos