[TYPO3-core] Suggestion about use statement

Philipp Gampe philipp.gampe at typo3.org
Fri Feb 7 19:55:27 CET 2014


Hi Stephan,

Stephan Schuler wrote:
> In TYPO3 situations, you very often go like this:
>> /** @var $foo \Foo\Bar\Domain\Service\SomService */
>> $foo =
>> $this->objectManager->get('Foo\\Bar\\Domain\\Service\\SomService');
> 
> So when adding a "use" here, you would now change it to this one:
>> /** @var $foo SomeService */
>> $foo =
>> $this->objectManager->get('Foo\\Bar\\Domain\\Service\\SomService');
> 
> Well, that looks even more ugly imho.

I suggest to not reduce the namespace in [doc]comments, because it creates 
confusion if the comment is read via reflection (e.g. in extbase).

> And on top of that: There's a reason why we call a thing not just File or
> \File but \TYPO3\CMS\Extbase\Domain\Model\File. It's not to have a
> complete description of something right inside of its class name. I's to
> have a chance to identify different things that both are "Files" in some
> ways. Current example is FAL. When dealing with FAL, you pretty often need
> to decide if it's a \TYPO3\CMS\Extbase\Domain\Model\File or a
> \TYPO3\CMS\Core\Resource\File -- and both have no common parent at all.

\File is something different than File.

One of the reason I want to have use statements on top of the file is, that 
it tells you whether File is from extbase or core.

Using an IDE, this does not matter as you can always hover it (or use quick 
lookup) to find out what the real classname is.

Remember: Use statements in PHP are working as alias unlike compiled 
languages (e.g. Java).

> So what to do in those situations? Would you use "use" like that:
>> use TYPO3\CMS\Extbase\Domain\Model;
>> use TYPO3\CMS\Core\Resource;
>> $foo = new Model\File;
>> $bar = new Resource\File;

Or you could do it like that:
use TYPO3\CMS\Extbase\Domain\Model\File as FileModel;
use TYPO3\CMS\Core\Resource\File as FileResource;
$model = new FileModel;
$resource = new FileResource;

> Besides the fact that it's unlikely to create two of those different files
> in a short distance (first: There is an API for that in the first place,
> second: You likely wouldn't create new ones like that, third: You would
> objectManager instead of new, but I would keep the quoted example code
> short), situations like that in general occur every now and then: Two
> classes with a common class name but different namespaces to be used in
> the very same file.
> 
> Ok, you could "rename" classes when calling "use". But that's not a good
> idea, too, because then you need to remember if the class is called File
> or ExtbaseFile or ModelFile in every single class.

The IDE does that for you. If your class becomes so long that you cannot 
remember it, consider to refactor it into smaller concerns.

> I think using "use" doesn't give any benefit.

It makes the classnames much smaller. With an 120 char limit per line, you 
cannot nest functions very much and you code becomes very cluttered.

> You want to know which classes you use in a file? For what? As an
> indicator for code quality? Hudson shouldn't count "use" but real class
> names if it's down to that.

To have an overview. It makes it far easier to track class relations if you 
do not have an IDE at hand and you want to fix something.

> Code can be understood better? When writing
> e.g. doc comments for properties or method arguments, I usually have more
> characters per line inside of my human readable sentences as the actual
> class names have. So I wouldn't call it an improvement.

It is not about doc comments. As noted above, I rather keep the fully 
qualified name for those.

> If you decide to change that and introduce heavy usage of "use", I will
> get used to that. But I really would like to not need to.

Well, this is totally up to you. I prefer clean code that can be read like a 
book.
Having use statements allows me to understand the class context inside an 
application before I have to read the full class body. I am a very visual 
person. Having clear layouts and short lines help me a lot while skimming a 
class.

Best regards
-- 
Philipp Gampe – PGP-Key 0AD96065 – TYPO3 UG Bonn/Köln
Documentation – Active contributor TYPO3 CMS
TYPO3 .... inspiring people to share!



More information about the TYPO3-team-core mailing list