This question is specifically about PHP, but I’m guessing it might be applicable to other languages as well.
I’ve noticed that between PHP4 and PHP5, the designers of the language shifted away from using -1 as a return value to using constants or other forms of output. This makes sense, as -1 is not particularly evocative, and I’m guessing this practice led to confusion.
That said, I am sometimes inclined to return -1 when I want to quickly add another return option to a function, and -1 often seems like a perfectly valid way to express the outcome I am coding for.
So here are my questions:
-
Is my observation generally correct, regarding the move away from
-1as a return value in PHP5 vs PHP4? -
What are the cons of returning
-1, beyond for the reason I mentioned above, wherein the-1return value doesn’t contribute positively to code clarity?
This is just a hunch, but in my experience there is little reason or consistency to design choices in PHP. I also find a lot of functions that return
FALSEon failure, many with an added note that you have to explicitly distinguish betweenFALSEand other (valid) values that coerce toFALSE, e.g.0. In such cases I think-1is a much nicer return value.Of course you could always just return
-1and define constants that have meaningful names and are just mapped to-1.