I think that returning an error code or throwing an exception is something very valid that you might think about, and cross-linguistic comparison can be useful and informative. I think that a very generalized answer to this concern is just a consideration: the set of valid return values โโfor any function should be made as small as possible and as necessary .
As a rule, this will mean that if this method returns an integer in one test case, users can rightfully expect that the method always returns an integer number or throws an exception. But, of course, a conceptually simple way is not always the best way to handle things.
The return value of the smallest surprise is usually None ; and if you look at it, you will see that its semantics is None , which licenses its use in all directions: it is a singleton immutable value, which in many cases evaluates to False or prohibits further calculations - not concatenation, without arithmetic. Therefore, if you decide to write a frob(x) method that returns a number for string input, and None for non-numeric strings and any other input, and you use this inside an expression like a=42+frob('foo') , you still you get the exception very close to the point at which the fictitious action occurred. Of course, if you type frob('foo') into a database column that was not defined using NOT NULL , you may run into problems, possibly in a few months. This may or may not be justified.
Therefore, in most cases, when you, for example, want to print a number from a string using something like bare float(x) or int(x) , this is the way to go, since these built-in modules will throw an exception if averaged data is not specified. If this is not suitable for your use, consider returning None from the user method; basically, this return value tells consumers that "Sorry, I could not understand your contribution. But you only want to do this if you positively know that your program makes sense from this point of view onwards.
You see, I just found out how to turn every notification, warning, and error message into a potential show-stopping exception in, m, PHP. It just drives me crazy that a typo in the variable name is generated in the standard PHP configuration, nothing but notification to the user . This is therefore bad. The program simply continues to do something with program code that does not make sense at all! I canโt believe that people find this feature.
In the same way, it should be considered as follows: if at any moment of time it is possible to assert with sufficient justification that the execution of a part of the code no longer makes sense - since there are no values, they come from either an unexpected type, or when resources such as connecting to the database have decreased - It is extremely important to minimize debugging headaches, disrupt execution and manual control to any level in the code that has the right to handle failure.
Experience has shown that abstaining from early action and the ability to push dummy values โโto your data is useless, except that your code is more difficult to debug. So many examples of overly hard casting: allows you to add integers to floating elements. So that a string containing only digits added to a number is a fictitious practice that can create strange, non-localized errors that can occur on any given string that processes this data.