php-ddd
php-ddd copied to clipboard
Naming Interfaces - To (pre- / suf)fix or not to fix
For instance @Symfony uses the Interface
Suffix e.g. FooRepositoryInterface
:
- http://symfony.com/doc/current/contributing/code/standards.html#naming-conventions
I've seen C# and @dotnet examples prefixing Interfaces with an I
e.g. IFooRepository
:
- https://msdn.microsoft.com/de-de/library/8bc1fexb%28v=vs.71%29.aspx
Why the prefix in C# but not in Java?
The difference between Java and C# is that Java allows you to easily distinguish whether you implement an interface or extend a class since it has the corresponding keywords implements and extends.
As C# only has the : to express either an implementation or extension, I recommend following the standard and put an I before an interface's name.
- http://stackoverflow.com/a/2728119/1937050 by @hamvocke
Or:
Because of that, I don't think conventions are nearly as important in java for interfaces, since there is an explicit difference between inheritance and interface implementation.
- http://stackoverflow.com/a/542135/1937050 by @mbriggs
Further links for discussion:
- http://www.richardlord.net/blog/the-i-in-interface
- http://programmers.stackexchange.com/questions/117348/should-interface-names-begin-with-an-i-prefix
Since PHP offers the implements
keyword I think it is best practice to NOT USE any prefix or suffix.
At least inside the Domain Model
namespace:
- Domain\Model\Foo\Foo.php
- Domain\Model\Foo\FooId.php
- Domain\Model\Foo\FooRepository.php <-- Interface
While working with Symfony you should adapt their naming conventions.
Personally and since I started to put all my Interfaces into my Application or Infrastructure namespace I don't use any prefix or suffix at all anymore.
Related:
- https://github.com/webdevilopers/php-ddd/issues/6
Hey @webdevilopers,
I personally go with the Symfony naming scheme aka ClientInterface
.
Generally - even in your Domain Model namespace? Or just inside the bundles?
I drop the Interface and place all interfaces inside Contracts directory.
I recently wrote a blog post about this: https://www.alainschlesser.com/interface-naming-conventions/
TL;DR:
Interface
suffix (or prefix) is an unnecessary form of Hungarian Notation, and actually severely limits your architecture (by making it impossible to replace classes with interfaces later on, because their nature is encoded in their name). This forces you to proactively put interfaces everywhere to not cause a lock-in to a specific architecture.
For long time I've agreed with prefix/suffix like in Symfony or PSR naming conventions
but recently I've changed my mind and I think exactly like @schlessera. You don't need to put Interface
in name because there IS information that this is interface
(the same goes for traits). IDE will display it too. The only thing I accept now is Abstract
prefix for abstract classes, but I think it's possible I'll drop it too.
You don't introduce yourself JohnName SmithSurname ;-)
Upcoming PHP BC Break: PSR and all frameworks will remove Interface suffix and Abstract prefix from their naming conventions by default.
...Couldn't wait until April fools!
There is topic on PHP-FIG mailing group about changing conventions in PSRs (only future ones), but it's dead without conclusion.
Nobody expects such BC break, but for any new software it's not the only way of naming.
But PSR-8 doesn't use Interface
suffix.