开发者

Method return values not consistent

开发者 https://www.devze.com 2023-02-23 01:03 出处:网络
This is a philosophical question, actually. It\'s bee开发者_如何学JAVAn a year I\'m programming using PHP as a language and, as all of you know, PHP is very liberal as for the datatypes.

This is a philosophical question, actually. It's bee开发者_如何学JAVAn a year I'm programming using PHP as a language and, as all of you know, PHP is very liberal as for the datatypes. I was wondering: given that, is it a good practice to allow methods to return different kind of values? Example: I'm used to set the variable that should be returned at a default value (usually false) and, if during the execution of the method everything goes well, the variable gets the value the successful execution has given. Disclaimer: I try to document everything using PHPDoc.


Seems like a correct approach: returning boolean false allows the method/function to return other logical "false" values such as 0 or the empty string as valid while still allowing the caller to check its result with the === operator.


It's a common approach, at least in PHP, and it isn't a bad practice.

It can be documented without problem using the PHPDoc convention: http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.return.pkg.html


I was wondering: given that, is it a good practice to allow methods to return different kind of values?

No, it's not. Not in PHP not in other languages. Better add an other method for a specific type of results.

0

精彩评论

暂无评论...
验证码 换一张
取 消