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

Andreas Wolf typo3ml at andreaswolf.info
Wed Mar 9 18:21:11 CET 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEAREIAAYFAk13twcACgkQHWtXp155bP+FBQCfTnZt49hm1rYXGhEuL/scb1iH
Wv0AnRwycYv72rryvOvGUmGxwXpMhcGr
=h/sO
-----END PGP SIGNATURE-----




More information about the TYPO3-dev mailing list