My next multi-part series: Calculating NI with HMRC

Many years ago I embarked on a journey to change my career from being a computer repair technician, a certified electrician to a software developer. Over the past years, and little did I know, I was creating some cool sophisticated side projects.

I remembered converting the National Insurance Contributions guidance for software developers calculations into the Swift language and Ruby (Ruby on Rails). Here are what they look like, unedited:

Swift:

import Foundation
    
    let lel: Int = 5824
    let pt: Int = 8060
    let qq: Int = 8112
    let aa: Int = 43000
    let basicRangeMax: Double = 32000
    let higherRangemax: Double = 150000
    
    // Default HMRC Values:
    var personalAllowanceRange: ClosedRange<Double> = (0...11000) // no tax
    var basicRateRange: ClosedRange<Double> = (11001...43000) // 20% tax
    var higherRateRange: ClosedRange<Double> = (43001...150000) // 40% tax
    var additionalRateRange: Double = 150000 // 45% tax
    
    
    // Standard NIC Calculation
    func stepOne(grossProfit: Double, weeksWorked: Int = 12, weekMonth: Int = 12) -> Double {
        var result : Double?
        
        let a = grossProfit
        let b = (lel * weeksWorked) / weekMonth
        let answer = Double(a) - Double(b)
        
        if answer < 0 {
            result = 0.0
        } else if answer >= 0 {
            result = Double(b)
        }
        
        return result!
    }
    
    [...]

Obviously there is more to the code above as it gets worst. It's like inviting someone over when your home is turned upside down. Embarrassing 🙈 Below is the Sketch I did:

I've learnt a ton implementing this with Xcode. I can't stress enough that side projects are the main driver if you want to be the best in what you do.

Ruby (Ruby on Rails):

module TimesheetsHelper
  def personal_allowance(gross_profit)
    hrmc_100k = 100000
    personal_allowance = 11000
    if gross_profit.to_f > hrmc_100k
      value_to_minus = (gross_profit.to_f - hrmc_100k) / 2
      pa_value = personal_allowance - value_to_minus
      pa_value < -1 ? 0 : pa_value
    else
      personal_allowance
    end
  end



  # Guide: https://www.gov.uk/income-tax-rates
  # Total amount tax to be paid to HMRC
  # AKA Income Tax
  def total_tax_paid(gross_profit)
    pa = personal_allowance(gross_profit)
    personal_allowance = (0..11000)
    basic_rate = (11001..43000)
    higher_rate = (43001..150000)
    additional_rate = 150000

    case gross_profit
    when personal_allowance
      (gross_profit - pa) * 0
    when basic_rate
      (gross_profit - pa) * 0.2
    when higher_rate
      (gross_profit - pa) * 0.4
    when additional_rate
      (gross_profit - pa) * 0.45
    else
      (gross_profit - pa) * 0.45
    end
  end
  
  [...]

Do not judge 😅 This was a very long time ago and I've seen many areas where I could "DRY-UP". I couldn't get the iPhone app to work as it was showing too many errors for me to look at but I did eventually booted up the Rails project, with many failed attempts, and it still looks great 😱

An old side project of mine from 2016

It started off as a basic NI calculator for the UK contractors where you'd see your take home pay after a completed job. I remembered learning about expenses, the maximum expense a self-employed person could claim for a given working day etc and factored it into this side project. This little project seems huge for me to go through in one blog. I will have to go through and refactor - a lot of refactoring needed, for a multi-part series.

What's next?

First, I will start off by creating a basic NI calculator using the Ruby on Rails framework, it just make sense here, then I'll work my way up to the final application with a timesheet. It won't be fancy. The aim is to open your eyes to see what else you could do with this little project. Who knows, your own personal money manager? We just may end up with both iPhone and Desktop applications 😉 Stay tuned! ✌🏼