Have you heard this joke?
A programmer walks into a bar and says to the bartender, “Can I get a beer? And add a while(1)?”The bartender is puzzled: “Do you want me to create an infinite loop?”The programmer laughs and says, “No, boss, I just want a drink that never ends.”
As you can see, in the programmer’s world, infinite loops are almost everywhere, like “black holes” in code. But worse, programmers often write their code in a way that is as hard to understand and maintain as an infinite loop. Today, let’s talk about how to improve the readability of C code and avoid letting others (and your future self) fall into this “infinite loop”.
1. Code Standards: Standards Equals Readability
The term “code standards” may sound a bit dull, but it is really important. Every line of code you write may not be for yourself, but for your future self or others.
1.1 Use Appropriate Naming
The simplest way to improve code readability is to have clear naming. If a function name or variable name makes it immediately clear what it does, then you’ve succeeded. For example, don’t use <span>int a</span>
, instead use <span>int student_count</span>
; don’t use <span>foo()</span>
, instead use <span>calculate_total_price()</span>
. Although these names may be longer, they reduce the question of “what does that function do?”
int student_count = 0; // Instead of int a
1.2 Consistent Indentation and Spacing
Imagine reading an article where words are jumbled together with no paragraph separation; the reading experience would be maddening. Code is the same. Consistent indentation clarifies code hierarchy and makes it organized. Use spaces or tabs consistently, and don’t let different people argue over such trivial matters.
if (student_count > 0) {
printf("Number of students greater than 0\n");
} else {
printf("Number of students is 0\n");
}
2. Comments: Not Just for Passing Checks, But for Understanding
The idea that “code should be self-explanatory” has some merit, but not every line of code can be self-explanatory, especially some complex algorithms or logic. Appropriate comments can give your code more “warmth” and prevent confusion when faced with your complex expressions.
// Calculate the average score of students
double calculate_average(int grades[], int num_students) {
int total = 0;
for (int i = 0; i < num_students; i++) {
total += grades[i];
}
return total / num_students; // Return total score divided by number of students
}
3. Refactoring: Making Code Simpler and Easier to Understand
“Refactoring” means restructuring code to remove redundancy and improve readability and efficiency. It does not change the external behavior of the code, but can make it more elegant and concise. Here’s a simple example:
Suppose you wrote a function to check if a student’s score is passing. The initial version might look like this:
int check_pass(int score) {
if (score >= 60) {
return 1;
} else {
return 0;
}
}
But this code can actually be simplified further:
int check_pass(int score) {
return score >= 60; // Directly return the result
}
It’s so concise and clear; the logic is easy to understand, and others can grasp it easily.
4. Decomposing Complex Functions: Large Functions Are “Monsters”
If you find a function that has hundreds of lines of code, it is definitely a “monster.” Decomposing functions is an important step in refactoring. A function should only do one thing. If a function contains too much logic, you need to break it into several smaller functions, each focused on solving one problem.
// Complex function
void process_student_data(int grades[], int num_students) {
// Calculate average score
double average = calculate_average(grades, num_students);
// Check passing status
for (int i = 0; i < num_students; i++) {
if (grades[i] >= average) {
printf("Student %d passed\n", i);
} else {
printf("Student %d failed\n", i);
}
}
}
You can break it down into two smaller functions, making it look simpler and clearer in logic.
void print_pass_status(int student_index, int grade, double average) {
if (grade >= average) {
printf("Student %d passed\n", student_index);
} else {
printf("Student %d failed\n", student_index);
}
}
void process_student_data(int grades[], int num_students) {
double average = calculate_average(grades, num_students);
for (int i = 0; i < num_students; i++) {
print_pass_status(i, grades[i], average);
}
}
Conclusion: Code Is Your “Friend”
In summary, improving the readability of C code is not just for others to understand, but also for “future you” to quickly comprehend and easily modify when reviewing this code. Code standards, clear comments, reasonable naming, and necessary refactoring are all important means of improving code quality.
Remember, when programmers write code, they shouldn’t show off like writing poetry, but should write it as simply and straightforwardly as chatting with a friend. This way, there are no infinite loops between you and other developers, only efficient collaboration and smooth code flow.