Clean Function: Using Exception to Return Error Code

Experienced programmers must have faced cases where a process within function needs to be checked. Some programmers will create a function that returns the status of the process or returning an error code. For example, a function that sent a request to external web services, and the response of web services should check whether a success response, error response or timeout.

def calculate_nett_payroll
  response_tax_service = get_user_tax(

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

Function calculate_nett_payroll will return “ok” when processing gets data from tax service success and calculating the net payroll. If the response tax service is not 200 then the function will return the error code “nok”.

if andi.calculate_tax == "ok"
  if andi.calculate_nett_payroll == 'ok'"Payroll of Andi is #{andi.payroll_net}")
  else"Payroll calculation net payroll error")
else"Payroll calculation tax error")

The caller of the function must check the error code first before he accesses the needed data andi.payroll_net. Not only function calculate_nett_payroll there are function calculate_tax that have same logic. Due to checking the error code of the function client need to make the nested structure.

Robert C. Martin in his book Clean Code prefers to use exception instead of return error code. Here is the updated method of calculate_nett_payroll that used exception.

    ane.calculate_nett_payroll"Payroll of Ane is #{ane.payroll_net}")
rescue StandardError => e"Payroll calculation error: #{e.message}")

You might spot the code become cleaner, less nested structure, and has less line of code. The happy path becomes more clear and straight forward, within begin and rescue, and so the negative path becomes much more simple.

Checkout full example code in here

Or you can play in here




comments powered by Disqus