Code Cleanliness Ratio (CCR) is the total number of Domain Words found in a software code repository divided by the total number of words in the repository.

Required

Most of the time software projects are filled up with words and sentences that just don’t help with figuring out what the projects are actually about.

Some examples of these words are: for, if, util, service, processor, String, int, and many more.

These words make it difficult to find out the intention of the project.

For instance, it’s more clear to see code like this:

Invoice.ifItWasPaid().then().sendToCustomer()

than to see this:

public static void main(String args...) {
  Invoice inv = new Invoice(input)
  boolean wasPaid = inv.wasPaid
  if (boolean) {
    inv.sendToCustomer()
  }
}

The formula to calculate the Code Cleanliness Ratio is:

CCR = ∑DW / ∑W

where ∑DW is the number of words that belong in the Domain Words category and ∑W is the total number of words.

We consider “words” as all the variables, signs, file names, folder names, etc.

The Domain Words are the words related to the domain of the software application or solution.

Examples of Domain Words in a Travel Software Application are:

  • Flight
  • Origin
  • Destination
  • Reservation
  • Booking
  • Check-in Date
  • Check-out Date

Naturally, we do need to write language-specific words like if, for, while, etc. in order to build our system. However, we can sometimes hide them behind reusable libraries.

Let’s calculate the CCR of our previous code.

Suppose the Domain Words are the following:

  • Invoice
  • Paid
  • Send
  • Customer

Let’s analyze the first example.

Invoice.ifItWasPaid().then().sendToCustomer()

If we consider camel case names as separate words, the Total Words ∑W would be 9. (Invoice,If,It,Was,Paid,then,send,To,Customer)

Given that only Invoice, It, Send, and Customer are Domain Words the ∑DW would be 4.

So, the Code Cleanliness Ratio of this code is

CCR = 4 / 9 = 0.44444

Let’s analyze our second example using the same Domain Words list…

public static void main(String args...) {
  Invoice inv = new Invoice(input)
  boolean wasPaid = inv.wasPaid
  if (boolean) {
    inv.sendToCustomer()
  }
}

In this example, ∑DW is 6 and ∑W is 23.

So, the Code Cleanliness ratio of this code is

CCR = 6 / 23 = 0.26086

We can improve the CCR of our latest code by changing the variable name inv to invoice, resulting in ∑DW = 9 which would result in a CCR of 0.391304

CCR = 9 / 23 = 0.391304