DEV Community


Discussion on: Are you using Void correctly?

designpuddle profile image
Chris Bertrand Author

The CQRS pattern has been around for a while and is gaining a lot of traction in our new microservices, event sourcing, multi tenancy environments. It's definitely a valid use case. Equally it adds a whole level of complexity.

aleksikauppila profile image
Aleksi Kauppila

Many seem to mix CQRS (the architectural pattern) to this lot simpler practice that Martin describes. Check the post again 😊

Thread Thread
designpuddle profile image
Chris Bertrand Author

They're very much related though. Interestingly if you look at what he says, He in fact determines that changing state and returning values should be deemed as two separate operations.

"It would be nice if the language itself would support this notion. I could imagine a language that would detect state changing methods, or at least allow the programmer to mark them. "

This one could argue is Martin himself notioning that using a void function should be called a "command", and a standard function that returns an object is called a "query". I'd go along with that, and a way of the compiler enforcing that rule would be even better!

"The really valuable idea in this principle is that it's extremely handy if you can clearly separate methods that change state from those that don't. This is because you can use queries in many situations with much more confidence, introducing them anywhere, changing their order. You have to be more careful with modifiers."

This is where the problem lies, your return type functions can still change global state, and this is where I feel we are not enforcing enough rules around what a function "should/is allowed" to do.

Thanks for sharing your views, I appreciate the healthy debate!