Skip to content

Instantly share code, notes, and snippets.

@dyigitpolat
Last active October 25, 2022 16:58
Show Gist options
  • Save dyigitpolat/7883569bb60aa89e67df293f7424dd23 to your computer and use it in GitHub Desktop.
Save dyigitpolat/7883569bb60aa89e67df293f7424dd23 to your computer and use it in GitHub Desktop.
C++ Code Cleanliness Metrics

Quantitatively Measuring Cleanliness of C++ Code

Metrics and Symbols

(Metric description : Symbol)

  • Ratio of number of lines with more than 80 columns over all lines : R_80C
  • Ratio of source files with more than 100 lines orver all files : R_100L
  • Nesting depth for each of {}, (), [] and <> : N$ where $ is {}, (), [] or <>
    • Average nesting depth : Avg_N$
    • Maximum nesting depth : Max_N$
    • Ratio of nested blocks with Ndepth$ more than 4 over all nested blocks of the same type : R_4N$
  • Length of a sequence that appears X times for X > 1, multiplied by X (within min. edit distance < one third of the length of it) : QX (Edit distance: add/remove/replace will count as 1 character edits)
    • Total QX for sequences with QX > 50 : T_QX50
    • Ratio of T_QX50 over total number of characters in source : R_T_QX50
  • Number of member functions in a class : CF
    • Avg_CF
    • Max_CF
    • Ratio of classes with more than 6 member functions over all classes : R_CF6
  • Number of list items that are spearated by commas in a list : I
    • Avg_I
    • Max_I
    • Ratio of lists with more than 3 items over all lists : R_I3

Clean Code Characteristics

  • R_80C < 0.1%
  • R_100L < 5%
  • Avg_N$ < 4
  • Max_N$ < 6
  • R_4N$ < 10%
  • R_T_QX50 < 1%
  • Avg_CF < 6
  • Max_CF < 10
  • R_CF6 < 10%
  • Avg_I < 3
  • Max_I < 6
  • R_I3 < 20%

Clean Code Policies

  • Every single successful merge into the "main" branches MUST improve the metrics.
    • No merge can make any of the metrics worse.
  • If characteristics do not hold (i.e. codebase is not clean), a refactoring merge that improves two of the metrics by at least 50% (only in refactoring diff) must be done before doing a new merge. The refactoring merge needs to modify at least the same number of lines as the new merge.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment