What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hi all, just a dumb question: why do we prefix the module name to all the classnames in Spryker ? Do

U0162AQHATZ
U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

Hi all,
just a dumb question: why do we prefix the module name to all the classnames in Spryker ? Don't we have namespaces for that ?

Comments

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited July 2020

    Namespaces are to prevent naming collisions. The naming pattern in Spryker is required for the Locator to automatically find relating objects and allow for dynamic replacement of objects by having a lookup order and therefore precedence.

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    You can imagine it being Smalltalk Message Passing via stringly typed lookups.

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    As in: The message (interface) matters, not the recipient.

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    tbh I was afraid, that the locator (or in detail the ClassResolver) is the reason for this naming convension πŸ™‚

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    The reason for the convention is the design of Spryker that allows for ducktyping in a rigid type system essentially. You need a common denominator and the lowest one is the name. It's the same in Spring btw. πŸ™‚

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    thx Thomas for your insights. Will try to get my head around it πŸ˜„

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    (although I don't see why we cannot utilize the namespace as the common denominator as well)

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited July 2020

    Because it will be different. You have a lookup order of namespaces...
    Let's say we have namespaces A, B and Vendor.
    Our lookup Order is from A -> Vendor.
    That way, the upper layers can overwrite or extend modules from the bottom layers without disturbing each other or causing tight coupling. It is necessary for a loosely coupled, lately bound system.

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    just imagine, how neat this classname could look like:
    class MerchantRelationshipSalesOrderThresholdGuiDependencyProvider
    πŸ˜„

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    In my example above you can also replace A with and B with Pyz πŸ˜‰

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited July 2020

    (and Vendor with Spryker and SprykerEco of course)

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    And btw: It's not a dumb question, it's a rather important one as it defines what Spryker really is and how things plug together - at least how I understand it. I'm not a Spryker employee but I actually love that part about Spryker a lot.

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    but this hierarchy is reflected in the namespace, right?

    why not having:

    Spryker\Zed\MerchantRelationshipSalesOrderThresholdGui\DependencyProvider
    
    Pyz\Zed\MerchantRelationshipSalesOrderThresholdGui\DependencyProvider
    
    FooBar\Zed\MerchantRelationshipSalesOrderThresholdGui\DependencyProvider
    

    I don't really get, why we need the Modulename as a prefix

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    You are right - one could implement that by Module lookup... I just think it was chosen that way for more clarity. That way you don't have to alias everything and it's more convenient for a class level scan - but that's just wild speculation.

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    (plus it's easier to navigate the project)

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    guess that goes down to personal taste... for me, those long class names are driving me nuts πŸ˜„

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    Just name your module in a short an sweet fashion.

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    A & B πŸ˜„

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    DiscountDependencyProvider isn't half bad πŸ˜‰

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    there are just cases where finding a concise name can be difficult so we get these monsters...

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    That won't help if you do a module scan anyway, since instead of searching for MerchantRelationshipSalesOrderThresholdGuiDependencyProvider you now need to search for MerchantRelationshipSalesOrderThresholdGui\DependencyProvider

    It's even one character more!

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet

    PHPstorm supports camelCase search on namespace.
    you can type "MeRSaDePr" and it will resolve it πŸ™‚

  • U0162AQHATZ
    U0162AQHATZ Posts: 10 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited July 2020

    (if you meant that by "module scan")

  • UQ958DA4U
    UQ958DA4U Posts: 232 πŸ§‘πŸ»β€πŸš€ - Cadet

    What you mean is called "fuzzy search" and would work for either scenario πŸ˜‰ So no savings there, either.

  • vasily.rodin
    vasily.rodin Freelance Software Developer. Ex-Spryker Spryker Solution Partner Posts: 21 πŸ§‘πŸ»β€πŸš€ - Cadet

    I remember in old times it was not really convenient in PHPStorm working with the files with the same name. Just two tabs would have the same name then. Now it’s better, but still, having prefixes helps a lot to avoid mistakes

  • Alberto Reyer
    Alberto Reyer Lead Spryker Solution Architect / Technical Director Posts: 690 πŸͺ - Explorer

    I just think it was chosen that way for more clarity

    I was there in the very beginning of the Spryker journey, when we migrated from a Zend/PEAR Naming (that’s one of the reasons it was named Zed) application to Silex and had to migrate all namespaces too.
    Essentially we had a pretty hard time finding the right Facade if it was not prefixed with the bundle name so the class name was prefixed with the bundle instead of relaying only on the namespace.
    Back in 2014 when the migration happened (or maybe early 2015) PHPStorm had the already mentioned drawback to only show the classname in the tab title which made it also a lot more convenient to prefix the important class names with the bundle and the layer name (e.g.: FooBusinessFactory).