Clean Function: Command Query Separation

command functions are:

query functions are:

If you are programmers you should spot the differences. Command function should do something and the query function should answer something.

But have you found a function like this

def calculate_nett_payroll
  response_tax_service = get_user_tax(@user.id)

  if response_tax_service.status == 200
    @payroll_net = @payroll_gross - response_tax_service.body.tax_amount
    return "ok"
  end
  return "nok"
end

Those are command query but returning something just like the query function, leads to confusion. So you must go with separating query from command function to prevent ambiguity.

But how to handle the command function that functions caller to make sure the function process is going well? For example, a function that requests to a web service may return a response error. I have explained in my article Clean Function: Using Exception to Return Eror Code.

Tags

clean-code

Comments

comments powered by Disqus