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..

Hello again 🙂, in spryker i noticed several times, that instead of giving null

UPWG9AYH2
UPWG9AYH2 Posts: 509 🧑🏻‍🚀 - Cadet

Hello again 🙂,
in spryker i noticed several times, that instead of giving null or an exception, the implementation gives back an empty transfer object anyway. For example the method getCustomerOrderByOrderReference() in SalesFacadeInterface will always return an OrderTransfer even it is has not found a corresponding entity in the database… does this have a deeper meaning? Is the transfer object also responsible for representing the abscent of the data by just omitting the data?
I ask because i never found a counter example that returns null instead. For me its a bit misleading when asking a client or what else for data that can be abscent in fact but the corresponding method will in any case return a transfer object but it is empty. The clients “client” has to look now if the data in it is empty instead of just checking that the return value was null … i could imagine that some developer is not aware of that. Is there a spryker convention on that that we can/should follow?
Best regards

Comments

  • Andriy Netseplyayev
    Andriy Netseplyayev Sprykee Posts: 519 🧑🏻‍🚀 - Cadet

    Hi Ingo,
    The method you’re mentioning is serving (only) to the GatewayController. Communication between Zed and Yves (RPC) works in a way that the request/response must always be a Transfer object of the expected type. It is not really a GLUE restful API where you can return a status code and empty response. I think this is the reason.

  • Andriy Netseplyayev
    Andriy Netseplyayev Sprykee Posts: 519 🧑🏻‍🚀 - Cadet

    Regarding conventions, it is mostly:
    • method name starts with “get…” assumes that the object is there, in case of collections an empty collection can be returned. Otherwise Exception is thrown
    • method name starts with “find…” assumes that object may not be found, in that case null is returned

  • UPWG9AYH2
    UPWG9AYH2 Posts: 509 🧑🏻‍🚀 - Cadet

    Hi Andriy. Thanks for your response. That makes sense to me at all. But one thing: if there is no value found that can be used for the collection, it makes anyway no sense to return the empty collection. For example “getActiveStores” … if there is no store, the exception is the only logical option here since there MUST be at least one store in this collection (“No active stores found which is a serious problem now”, otherwise it would be findActiveStores() as mentioned, which then should return null for “no active store found but it is okay”). As conclusion: empty collections should never appear in my eyes, or do i miss something? 🤔

  • UPWG9AYH2
    UPWG9AYH2 Posts: 509 🧑🏻‍🚀 - Cadet

    Or is it just because of the PHP language which expect return values anyway even if you prevent return by throwing exceptions?

  • Andriy Netseplyayev
    Andriy Netseplyayev Sprykee Posts: 519 🧑🏻‍🚀 - Cadet

    the collection is kind-off an array. So empty array - is a valid value here. Whether there is “Exception” to be thrown after “getActiveStores” - is for client code to decide, depending on what it does, I would say.

  • Andriy Netseplyayev
    Andriy Netseplyayev Sprykee Posts: 519 🧑🏻‍🚀 - Cadet

    regarding get / find - it applies to singular methods