[TYPO3-core] RFC #12066: Code cleanup: Add some constants

Oliver Klee typo3-german-02 at oliverklee.de
Sun Feb 21 17:31:04 CET 2010


Hi,

Benjamin Mack schrieb:
>  * it's not really shorter than before
>  * it's not namespaced, what if I include a third-party tool that does
> the same?
>  * it's limited: "ONE_MINUTE"? Also, everybody can easily do the "60*24*7"
> 
> Maybe I just don't get it. Could you help me out here?

The main point of this is to improve code readability by removing any
"magic numbers" like 10 (for linefeed) or 86400 (for the number of
seconds per day) and to make coding a bit easier. As a general rule,
magic numbers are considered bad style. (And it's _not_ about reducing
the number of characters used for coding.)

So you'll be able to write this ...

  $foo = $salutation . LF . $body;

... instead of this:

  $foo = $salutation . chr(10) . $body;

Or this ...:

  $delayInSeconds = ONE_HOUR;

... instead of this:

  $delayInSeconds = 3600;

So this will make the code more "speaking", ie. using less of your
(space-limited) short-term memory while reading code, allowing you to
"get" more of the code. (That's the psychological explanation why
"speaking" code is good.)


This doesn't cover all possible cases for constants, but the most
commonly used ones - and it's a starter. (I don't think not having a
perfect solution should keep us from starting.)


Another side-effect will be improved performance because chr(10) is a
function call while LF is not.


Currently, we don't use a third-party tool that does the same. And
defining LF as a linefeed twice shouldn't be a problem. The only problem
that could arise if we include a tool that defines LF as something
different than a linefeed, eg. "Hello world". This is possible, but very
improbable.

(And I'm already using those constants in my own extensions and would
like to get them into the core so I can drop them from my library
extension.)


Oli
-- 
Certified TYPO3 Integrator | TYPO3 Security Team Member


More information about the TYPO3-team-core mailing list