3

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?

1
  • 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, 2018 at 15:43

3 Answers 3

3

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.

-3

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.

3
  • 2
    nothing in my questions asks about CQRS
    – user619818
    Apr 20, 2018 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, 2018 at 17:40
  • 2
    Cqs has nothing to do with cqrs Apr 20, 2018 at 17:56
-4

Depending on your scenario, you could simply store the result inside the class being called and then retrieve it with a query. As in the following example:

int main()
{
  A a;
  a.DoSomething();
  if( !a.HasDoneSomething() )
  {
    // handle error
  }
}

class A
{
private:
  bool DoneSomething{false};
public:
  void DoSomething()
  {    
    // activate
    if(isSuccessful)
    {
      DoneSomething = TRUE;
    }
  }
  
  bool HasDoneSomething()
  {
     return DoneSomething;
  }
}
1
  • 1
    I'm not seeing the advantage of this of a return value, and the statefulness definitely looks bad, either from repeated use or the lack of thread safety. You shouldn't force a command to return void just to fit in with some arbitrary guideline. May 28, 2022 at 10:25

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