Member-only story
7 Deadly Sins of Variable Naming (And How to Fix Them)
Writing good code is like writing a story. You want people to read it and understand what happens without getting confused. One of the biggest mistakes programmers make is giving bad names to their variables. Bad variable names make code hard to read and even harder to fix later.
In this article, we will look at 7 common mistakes people make when naming variables in Java. We will also show you how to fix these problems with simple examples.
Sin #1: Using Single Letters for Everything
Many new programmers think using single letters like a
, b
, c
, x
, or y
is fine. This is wrong in most cases.
Bad Example:
int a = 25;
int b = 30;
int c = a * b;
System.out.println("Total: " + c);
What do these variables mean? Nobody knows! You have to read the whole code to guess what they do.
Good Example:
int studentAge = 25;
int hoursWorked = 30;
int totalPay = studentAge * hoursWorked;
System.out.println("Total: " + totalPay);
Now anyone can read this code and understand what each variable does right away.
When Single Letters Are OK:
- Loop counters:
for (int i = 0; i < 10; i++)