[TYPO3-dev] RFC: CGL change regarding return statements

Ernesto Baschny [cron IT] ernst at cron-it.de
Wed Mar 9 18:31:35 CET 2011


Hi Andreas,

I see no conflict with your proposal and what we already say in the CGL.
There is no limitation of "only one return" in the validation part as we
have it written down already: there can be several, which makes exactly
what you are trying to do more readable.

What are the "parameters" to validate is of course depending on the on
the method, I would say a parameter can be everything that might
influence the outcome of the method. On a good designed system those
would only be the input parameters, but I doubt it that it will be
possible in our legacy code. ;)

Cheers,
Ernesto

Andreas Wolf schrieb am 09.03.2011 18:21:
> Hi list,
> 
> as this has been discussed for quite some times now, I want to propose a
> change of our Coding Guidelines regarding the use of return statements.
> Currently, we usually allow only one return statement per method:
> 
>> In general there should be a single return statement in the function
>> (see the preceding example). However a function can return during
>> parameter validation before it starts its main logic.
> 
> The only exception is parameter validation. What now bugged me in a
> recent change request [1] is a case when you validate "parameters" that
> are not declared as method parameters, but are required for the method
> to work.
> Or, another case, the deeply nested structures in t3lib_extFileFunc.
> These methods are totally cluttered and - even worse - the error message
> for each condition is at the end of the if-branch, in an else statement.
> So it is very hard to read and debug, especially when doing that without
> an IDE, with only vim on a server.
> (full disclosure: I have a change request for this class running [2], so
> I have a solid interest in getting this sorted out ;-) )
> 
> So what I propose is to drop the limitation to one return statement for
> all validation logic at the start of a method. It should be clear where
> this part ends and when the business logic starts. There, only one
> return statement is allowed.
> Also we should generally deprecate the use of return statements for
> ill-formed parameters, be it method parameters or "parameters" taken
> from objects (as e.g. in [1]). Exceptions serve far better here.
> 
> Regards
> Andreas
> 
> 
> [1] https://review.typo3.org/1003
> [2] https://review.typo3.org/1087




More information about the TYPO3-dev mailing list