Skip to content

Maintainability Index — Junior Level

Roadmap: Code Quality Metrics → Maintainability Index Someone runs a tool on your code and it spits out a single number: 64. Green. "Maintainable." But what does 64 mean, where did it come from, and should you trust it? This page answers all three — and tells you exactly how much to trust it, which is "a little."


Table of Contents

  1. Introduction
  2. Prerequisites
  3. Glossary
  4. Core Concept 1 — What the Maintainability Index Is
  5. Core Concept 2 — The Three Ingredients
  6. Core Concept 3 — The 0–100 Scale and Its Colour Bands
  7. Core Concept 4 — How to Read the Number
  8. Core Concept 5 — What the Number Can't See
  9. Real-World Examples
  10. Mental Models
  11. Common Mistakes
  12. Test Yourself
  13. Cheat Sheet
  14. Summary
  15. Further Reading
  16. Related Topics

Introduction

Focus: What is the Maintainability Index, and how much should you trust one number?

You will eventually open a code-quality report and see a column called Maintainability Index — usually a number between 0 and 100, often coloured green, yellow, or red. Visual Studio shows it. SonarQube has its own version. Plenty of linters and dashboards compute something similar. The pitch is irresistible: one number that tells you how maintainable a piece of code is. High is good. Low is bad. Done.

The Maintainability Index (MI) is real, it is computed from real measurements, and it is genuinely useful — but only if you understand what it is and, more importantly, what it isn't. It is a rough thermometer. It combines three things — how big the code is, how complex the code is, and how much stuff there is to read — into a single score. That score is a decent hint about where to look. It is a terrible grade to put on someone's work.

This page teaches you what the number means in plain language: the three ingredients that go into it, the 0–100 scale and its colour bands, how to read it (higher is better), and the honest limits — why a single number can flatter bad code and slander good code. By the end you'll be able to glance at an MI score and know exactly what it's telling you, which is: "maybe go look at this file." Never more than that.

The mindset shift: stop thinking "the Maintainability Index is a grade for my code." Start thinking "it's a thermometer." A thermometer of 39°C tells you something is wrong and where to point the doctor — it does not tell you the disease, the cause, or the cure. A quality score works the same way: a low number is a hint to investigate, never a diagnosis and never a verdict.


Prerequisites

  • Required: You can read code in at least one language and have a rough sense of when a function "feels" big or tangled versus small and clear.
  • Required: You've met cyclomatic complexity — the count of decision points (if, for, &&, case) in a piece of code. If not, read 01 — Cyclomatic & Cognitive Complexity → Junior first; the Maintainability Index is built partly on top of it.
  • Helpful: You've seen a quality report (Visual Studio's Code Metrics, SonarQube, Code Climate) and wondered where the numbers come from.
  • Helpful: You remember that "lines of code" is a thing people count, even though everyone agrees it's a crude measure.

Glossary

Term Plain-English meaning
Maintainability Index (MI) A single score (commonly 0–100) estimating how easy code is to maintain. Higher = easier. Combines size, complexity, and "amount of stuff."
Maintainability How easy it is to understand, change, and fix code without breaking it. The thing MI is trying — and mostly failing — to capture in one number.
Lines of Code (LOC) How many lines the code is. A crude size measure: more lines = more to read and maintain, roughly.
Cyclomatic complexity A count of the decision points (branches) in code. More branches = more paths = harder to follow and test. From topic 01.
Halstead volume A measure of "how much there is to read" — roughly, the count of operators and operands, scaled. More distinct symbols and more total symbols = bigger volume.
Operator A symbol that does something: +, =, &&, if, a function call.
Operand A thing operators act on: a variable, a constant, a literal like 42 or "hello".
Composite metric A single number built by combining several other numbers with a formula. MI is one. Convenient, and easy to misread.
Threshold / band A cutoff that turns a number into a label — e.g. "below 20 = red = hard to maintain."

Core Concept 1 — What the Maintainability Index Is

The Maintainability Index is a composite metric: one number assembled out of several smaller measurements, by a fixed formula. The question it tries to answer is deliberately broad — "How hard will this code be to live with?" — and it answers with a single score.

The intuition behind it is reasonable. Think about what actually makes code painful to maintain. Three things show up again and again:

  • It's big. A 2,000-line file is harder to hold in your head than a 40-line one.
  • It's tangled. Code full of nested ifs, loops, and special cases is harder to follow than a straight line of steps.
  • It's dense. Code packed with lots of variables, operators, and distinct names is more to read and track than something sparse and repetitive.

The Maintainability Index takes a measurement of each of those three properties, plugs them into a formula, and produces one number. The formula is arranged so that bigger, more tangled, denser code produces a lower score, and smaller, simpler, sparser code produces a higher score.

That's the whole idea. It's not magic and it's not science — it's three crude measurements, combined, scaled into a tidy range. You don't need the exact formula at this level (the middle.md shows it in full, including its famous magic constant). What you need is the shape of it:

   bigger code        ─┐
   more complex code  ─┼──►  [ formula ]  ──►  LOWER maintainability score
   denser code        ─┘

   smaller / simpler / sparser code  ──►  [ formula ]  ──►  HIGHER score

Key insight: The Maintainability Index is not a new, independent thing that someone measured about your code. It is three old measurements wearing a single hat. Everything good and bad about it follows from that — it's convenient because it's one number, and it's misleading because it's only those three things, squashed together so you can't see which one is driving the result.


Core Concept 2 — The Three Ingredients

Let's name the three measurements in plain language. You don't need the math; you need to know what each one is trying to capture and why.

Ingredient 1 — Lines of Code (LOC): "how big is it?"

The simplest one. Count the lines. The reasoning: more lines means more to read, more places for a bug to hide, more to keep in your head. It's crude — 100 lines of clear, repetitive code can be easier than 30 lines of clever density — but as a rough proxy for size, it's not nothing. In the MI formula, more lines pushes the score down.

Ingredient 2 — Cyclomatic complexity: "how tangled is it?"

This is the metric from topic 01. It counts the decision points in the code — every if, else if, for, while, case, and every && or ||. Each one adds a branch, and branches are what make code hard to follow and hard to test: more branches means more distinct paths through the code. The reasoning: tangled control flow is a real source of maintenance pain. In the MI formula, more complexity pushes the score down.

Ingredient 3 — Halstead volume: "how much stuff is there to read?"

This is the unfamiliar one, so go slow. Halstead volume is a rough measure of how much there is to read and track in the code. It's built from two simple counts:

  • Operators — the symbols that do things: +, -, =, ==, &&, if, return, a function call.
  • Operands — the things those symbols act on: variables, constants, literals like 7 or "name".

Halstead volume looks at how many distinct operators and operands you use and how many there are in total, and combines them into a number. The intuition: a chunk of code that juggles many different variables and many different operations has more going on — more for your brain to hold — than a chunk of the same length that does one simple thing over and over. Two functions can have the same line count but very different volumes. In the MI formula, higher volume pushes the score down.

Here's the feel of it. These two snippets are about the same length:

# LOW Halstead volume — few distinct symbols, simple and repetitive
total = 0
for x in items:
    total = total + x

# HIGHER Halstead volume — more distinct operators and operands, more to track
result = (base * rate + tax) - (discount * qty) + shipping - credit

The second line packs in more distinct variables (base, rate, tax, discount, qty, shipping, credit) and more distinct operators (*, +, -). There's simply more to read and keep straight, and Halstead volume is the attempt to put a number on that "more."

Key insight: The three ingredients measure three different kinds of difficulty: size (LOC — how much code), control-flow tangle (cyclomatic complexity — how many branches), and reading load (Halstead volume — how much to track per line). The Maintainability Index blends all three into one score. That blend is exactly why one number can hide what's actually going on — a great score might mean "all three are low," but a mediocre score doesn't tell you which of the three is the problem.


Core Concept 3 — The 0–100 Scale and Its Colour Bands

The raw Maintainability Index formula produces numbers in an awkward range. Microsoft's Visual Studio popularised a cleaned-up version: it rescales the result to a friendly 0–100 range and shows colour bands so you can read it at a glance.

On the Visual Studio scale, higher is better (easier to maintain), and the colours are:

Band Range (VS) Colour What it's hinting
Good 20 – 100 🟢 Green Reasonable maintainability. Nothing screaming for attention.
Moderate 10 – 19 🟡 Yellow Getting harder to maintain. Worth a look.
Low 0 – 9 🔴 Red Hard to maintain. A strong hint to investigate.

The number people remember is 20: on the Visual Studio scale, roughly 20 and below is where code starts being flagged as hard to maintain. Green covers a huge span (20 all the way to 100), which is itself a hint about how blunt this instrument is — a 25 and an 85 are both "green," yet they are not remotely the same code.

  0 ────────── 9 ─────────── 19 ──────────────────────────────── 100
  │   🔴 RED    │   🟡 YELLOW   │              🟢 GREEN               │
  │ hard to     │ moderate     │          reasonable to maintain     │
  │ maintain    │              │                                     │
       LOWER  ◄─────────────────────────────────────────►  HIGHER
                          (easier to maintain)

Two warnings, even at this level. First, the exact bands are a convention, not a law of nature. Visual Studio picked these cutoffs; other tools pick others, and some (like SonarQube) compute maintainability completely differently and put it on an A–E letter scale instead. There is nothing sacred about "20." Second, a green score is not a gold star. Green means "none of the three ingredients is alarmingly high." It does not mean the code is good, correct, well-named, or well-designed — only that it isn't big-and-tangled-and-dense all at once.

Key insight: The colours exist to make a fuzzy number feel decisive — and that's exactly the trap. Red genuinely deserves a look. But green spans 20 to 100, so "it's green" tells you almost nothing beyond "not on fire." Treat red as a useful alarm and green as the absence of one alarm, not as proof of quality.


Core Concept 4 — How to Read the Number

Reading an MI score correctly is mostly about reading it humbly. Here's the right way to use one.

Direction first: higher = easier to maintain. This trips people up because so many code metrics run the other way (high cyclomatic complexity is bad, high coverage is good). For the Maintainability Index on the 0–100 scale, up is good. A file at 78 is, by this rough estimate, easier to maintain than one at 31.

Use it to rank, not to grade. The single most useful thing you can do with MI is sort your files by it and look at the bottom of the list. The lowest-scoring files are where the metric is pointing — "start your investigation here." That's a perfectly good use. What's not good is reporting "our codebase scores 64, that's a B" — the absolute number on its own carries far less meaning than "these five files score worst."

Watch the trend, not the snapshot. A single MI number is a photo. The interesting signal is the movie: is a file's score drifting downward over time as people pile on changes? A file sliding from 70 to 45 over six months is a louder, more trustworthy signal than any one reading — it means the code is decaying as it's touched. Dashboards that track this are covered in 06 — Code Health Dashboards → Junior.

Always read it at the right level. MI can be computed per method, per class/file, or per whole project. A whole-project MI is almost useless — it averages your worst horror together with hundreds of trivial files and lands somewhere comfortably green, hiding everything. The finer the level, the more the number can actually point somewhere. Prefer per-method or per-file scores; distrust project-wide averages.

Key insight: The Maintainability Index is a pointer, not a report card. Its best job is to answer "where should I look first?" by ranking files worst-to-best. The moment you start using it to grade — assigning letters, setting team targets, comparing developers — you've turned a useful hint into a number people will game, and a gamed metric measures nothing.


Core Concept 5 — What the Number Can't See

This is the most important section, so don't skim it. The Maintainability Index has real blind spots, and knowing them is what separates someone who uses the metric from someone the metric fools.

Remember the three ingredients: size, control-flow tangle, reading load. If a quality problem isn't one of those three, MI cannot see it. And an enormous amount of what makes code hard to maintain isn't.

It can't see bad names. A function full of x, tmp, data2, and doStuff() can score exactly the same as the identical logic with clear names, because renaming changes none of the three ingredients. Yet good names are arguably the single biggest factor in real-world readability.

It can't see bad design. Tangled dependencies, a class that knows about everything, logic in the wrong place, a missing abstraction — none of these move the needle if the size and complexity numbers stay low. A file can be beautifully small and simple and still be in completely the wrong place, doing something it shouldn't.

It can't see whether the code is even correct. MI says nothing about bugs. Buggy code and correct code score identically if their shape is the same.

It can't see duplication across files. The same 30 lines copy-pasted into eight files might each score green individually, while the real maintenance nightmare — change one, you have to change all eight — is invisible to a per-file score. (That's what 05 — Duplication & Similarity measures instead.)

And it can be gamed without improving anything. Because the formula rewards fewer lines and fewer branches, you can "improve" the score by cramming logic onto fewer lines or hiding branches inside helper calls — making the code worse to read while the number goes up. The instant a metric becomes a target, people optimise the number instead of the thing the number was supposed to stand for.

Key insight: The Maintainability Index measures the shape of code — its size, its branchiness, its density. It is blind to the meaning of code — its names, its design, its correctness, its duplication. Most real maintenance pain lives in the meaning. So a good score means "the shape is fine," which is genuinely useful and genuinely not the same as "the code is good." Use the number to find suspects; use your own eyes to find crimes.


Real-World Examples

1. The green file everybody hates. A team's payment module scores a comfortable green 68. The metric is happy. The developers are not — every change to it is terrifying, because the names are cryptic (p1, flag, process2), the responsibilities are smeared across it, and nobody fully understands it. None of that touches size, complexity, or volume, so the MI never noticed. The lesson: a green score did not make the code maintainable, because the pain was in the meaning, not the shape. MI was looking in the wrong place because it can only look in three places.

2. The red file that earned its colour. A 1,400-line file with deeply nested branching scores a red 7. A developer, prompted by the colour, opens it — and finds exactly what you'd expect: a monster function doing nine jobs, impossible to test, edited by half the team. Here the metric did its one good job perfectly: it pointed at a real problem so a human would go look. It didn't diagnose anything; it just raised a flag, and the flag was right.

3. The score that "improved" the wrong way. A team puts the Maintainability Index on a dashboard and sets a rule: "no file below 40." A developer with a 38 file doesn't redesign it — they just merge several lines into one dense mega-line and inline a couple of branches into a cryptic helper. The score pops to 44. Green-ish. The code is now harder to read, but the number says it's better. This is the textbook failure: the metric became a target, so people optimised the metric instead of the maintainability. The dashboard now lies, and everyone trusts it.


Mental Models

  • The thermometer. MI is a thermometer for code. A high temperature (here, a low score) tells you something might be wrong and where to point the doctor. It never names the disease, finds the cause, or prescribes the cure. A human does all of that.

  • Three crude measuring sticks in a blender. Picture a ruler (lines), a tangle-meter (complexity), and a how-much-to-read gauge (Halstead volume) all dropped into a blender that pours out one number. The smoothie is convenient to drink, but you can no longer taste which ingredient was off — and you definitely can't taste the ingredients that were never in the blender (names, design, correctness).

  • A smoke detector, not a building inspector. A smoke detector is cheap, automatic, and worth having — it screams when something's burning so you'll go look. It does not inspect the wiring, judge the architecture, or certify the building. MI is the smoke detector. Treating its beep as a full safety report is how you end up trusting a number over your own eyes.

  • Up is good (the odd one out). Most quality metrics are "lower is better" (complexity, defects). MI flips it: higher is better. Keep a sticky note until it's automatic, because mixing up the direction is the fastest way to read a report backwards.


Common Mistakes

  1. Reading the direction backwards. For the 0–100 Maintainability Index, higher is better. It's the opposite of cyclomatic complexity. Mixing them up makes you "fix" your best file and ignore your worst.

  2. Treating the score as a grade. "We're a 64, that's a B" is meaningless. MI is for ranking files to find where to look, not for assigning a quality grade to code, a project, or — worst of all — a person.

  3. Trusting green. Green spans 20 to 100 and only means "not big-and-tangled-and-dense." It says nothing about names, design, duplication, or correctness. A green file can be a nightmare. Green is the absence of one alarm, not a gold star.

  4. Setting a target on it. The moment "keep MI above X" becomes a rule, people optimise the number — cramming lines, hiding branches — and make the code worse while the score goes up. Use it to observe, never to target.

  5. Reading the project-wide average. A single number for the whole codebase averages your worst file into a sea of trivial ones and hides everything. Always drill down to per-file or per-method scores.

  6. Forgetting what it can't see. MI measures shape (size, branching, density), not meaning (names, design, correctness, cross-file duplication). When the metric and your gut disagree, your gut is looking at things the metric structurally cannot.

  7. Comparing scores across different tools. Visual Studio's 0–100 MI, SonarQube's A–E rating, and another linter's number are computed differently. A "70" in one is not a "70" in another. Compare a tool only against itself, over time.


Test Yourself

  1. In one sentence, what is the Maintainability Index trying to do?
  2. Name the three ingredients that go into it, and say in a few words what kind of difficulty each one measures.
  3. On the Visual Studio 0–100 scale, is a score of 75 or a score of 15 the more maintainable code? Which colour band is each in?
  4. Roughly what score, on the VS scale, is the cutoff where code starts being flagged as hard to maintain?
  5. In plain words, what is "Halstead volume" trying to measure?
  6. Give two real maintainability problems that the Maintainability Index cannot detect, and explain why.
  7. Your team puts MI on a dashboard and a file's score jumps from 38 to 46 overnight. Why should you be suspicious rather than pleased?
Answers 1. It tries to estimate, in a **single 0–100 number**, how easy a piece of code will be to maintain — combining its size, its complexity, and how much there is to read. 2. **Lines of code** (size — how much code there is), **cyclomatic complexity** (control-flow tangle — how many branches/paths), and **Halstead volume** (reading load — how much there is to read and track per line). 3. **75 is the more maintainable** (higher = easier). 75 is **green**; 15 is **yellow** (moderate) on the Visual Studio bands. 4. Around **20** — on the VS scale, roughly 20 and below is where code gets flagged as harder to maintain (yellow below 20, red below 10). 5. Roughly **how much there is to read and track** — built from the count of distinct and total **operators** (things that do something) and **operands** (things they act on). More distinct symbols and more total symbols = higher volume = more for your brain to hold. 6. Any two of: **bad names** (renaming doesn't change size/complexity/volume), **bad design / tangled dependencies** (can stay small and simple yet be in the wrong place), **incorrect code / bugs** (MI says nothing about correctness), **cross-file duplication** (each copy scores fine individually). The reason in all cases: these aren't *size, branching, or density*, and those three are the only things MI can see. 7. Because the score can be "improved" without improving the code — cramming logic onto fewer lines or hiding branches in cryptic helpers raises MI while making the code *harder* to read. An overnight jump smells like the **metric being gamed as a target**, not genuine improvement. Go read the diff.

Cheat Sheet

WHAT IT IS
  Maintainability Index (MI) = ONE number (commonly 0–100)
  estimating "how easy is this code to maintain?"
  A composite: three measurements blended by a formula.

DIRECTION
  HIGHER = EASIER to maintain.   (opposite of complexity!)

THE THREE INGREDIENTS (all push the score DOWN when high)
  1. Lines of code      → SIZE          (how much code)
  2. Cyclomatic cmplx.  → TANGLE         (how many branches/paths)   [topic 01]
  3. Halstead volume    → READING LOAD   (how much to read/track)

VISUAL STUDIO 0–100 BANDS
  20 – 100   🟢 GREEN    reasonable
  10 – 19    🟡 YELLOW   moderate — take a look
   0 –  9    🔴 RED      hard to maintain — investigate
  Remember the number: ~20 and below = flagged.

HOW TO USE IT
  ✓ RANK files worst→best; look at the bottom
  ✓ Watch the TREND over time (drifting down = decaying)
  ✓ Read per-FILE / per-METHOD, not project-wide average
  ✗ Don't grade with it      ✗ Don't set a target on it
  ✗ Don't trust green as "good"   ✗ Don't compare across tools

WHAT IT CAN'T SEE (the meaning, not the shape)
  bad names · bad design · bugs · cross-file duplication
  → when metric and gut disagree, trust the gut.

ONE-LINE RULE
  A thermometer, not a diagnosis. Use it to find suspects,
  use your eyes to find crimes.

Summary

  • The Maintainability Index is a composite metric — one number (commonly 0–100) that estimates how easy code is to maintain by blending three smaller measurements with a formula.
  • The three ingredients are lines of code (size — how much code), cyclomatic complexity (tangle — how many branches), and Halstead volume (reading load — roughly how many operators and operands, i.e. how much there is to read and track). Each one, when high, pushes the score down.
  • On Visual Studio's rescaled 0–100 scale, higher is better. The colour bands are green (20–100), yellow (10–19), red (0–9) — and ~20 and below is where code gets flagged as hard to maintain. The bands are a convention, not a law.
  • Read it as a pointer, not a grade: rank files worst-to-best to find where to look, watch the trend over time, and read it per-file, not as a project-wide average. Never set it as a target — targeted metrics get gamed.
  • It measures the shape of code (size, branching, density) and is blind to its meaning (names, design, correctness, cross-file duplication) — which is where most real maintenance pain lives.

The honest one-sentence version: the Maintainability Index is a rough thermometer. A bad reading is a useful hint to go look; it is never a diagnosis, and it is never a grade. Use it to point yourself at suspicious code — then trust your own eyes to decide what's actually wrong.


Further Reading