I am learning about SOLID principles in a Pluralsight course where Command Query Separation is being discussed. I am fine with the query functions returning something (and crucially being non-mutating).

The question I have is about commands. The idea here is that commands should return void. What if there is a problem executing the command? Some functions that might be mutating return a result code. This idiom is quite common in C programs. So how to address that.

Do you have to assume that these functions should always succeed? Or should it raise an exception if the function does not succeed?

How would you handle it if the language doesn't support exceptions, eg C?

Any comments?

  • If a language does not support exceptions, you have got to work with the features the language supports, such as populating an error variable through a reference or even returning something. – Andy Apr 14 '18 at 15:43

Command/Query seperation is not really appropriate in a language which uses result codes. If you want to implement it in a laguage with result codes, you woud say that a "command" only returns a result code, while a "query" returns a struct or option with either a result code or an actual value.


in Command Query Responsibility Segregation (CQRS for short) you often communicate between services using events. So your "new customer" command might lead to a "customer created" event. Your view might respond to that event by refreshing so the user will see the new customer in the list of customers.

The same thing goes for errors. a "new customer" command might lead to a "insufficient credit rating detected" event (an error for someone who doesn't meet some credit rating requirement) and anyone who needs to know that information could listen to that event.

For simple validations (like username being filled) a CQRS system just won't accept the command. Depending on your specific language you could actually return an error code here instead of void (throwing exception or http response codes are the other common ways to deal with this). The important thing to remember here is that an "ok" means that the command is actually being considered, not an ok in the terms that any and all services will be successful. Validation of your command should really just be limited to checks like are required fields filled and are numeric fields actually numeric.

  • 2
    nothing in my questions asks about CQRS – user619818 Apr 20 '18 at 10:59
  • CQRS stands for Command Query Responsibility Segregation, It's exactly what your question deals with. You didn't ask specifically about an system that deals with events but that's very common with Command Query Separation exactly because if you want to have void returns you need to do something – Batavia Apr 20 '18 at 17:40
  • 2
    Cqs has nothing to do with cqrs – Esben Skov Pedersen Apr 20 '18 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.