In software development, clean code is very important. To write a clean, understandable, and maintainable is an important skill for any software developer. It is frustrating to continue someone else’s code but you cannot understand what it’s doing. In this article, I’ve list some of my tips to make your code a bit cleaner and easier to understand for everyone.
Meaningful Variable Names
You don’t need to write comments on the variable if you name your variable correctly. Don’t use letters like x, y, z for a variable (for loop variables are an exception).
// BAD NAMING
int x = 1; // period number
int c = 0; // counter// GOOD NAMING
int periodNumber = 1;
int counter = 0;
When you need to put a function as an argument you don’t need the function variable, use
_ as the variable name.
Comment when necessary
In flutter, there are 2 types of comment:
// A comment starts with 2 slashes/// A documentation starts with 3 slashes
You can write comments on a complex algorithm, but not on every class or methods. Here’s my example for documentations on an abstract class:
Keep Functions Small
Functions should only do one thing or one responsibility. If you follow this rule, your function will be small. Extract functions from your code to reduce duplication. In my case, from something like this:
To Something like this:
To make it easier to find files in your source code, you should define your project structure that everyone should know. For Flutter code, source files should be
lowercase_with_underscore.dart . Inside our project
README.md contains the project structure.
Know Your Language’s Convention
Flutter uses the Dart language. If you don’t know what to name a class or variable, go to Effective Dart: Style. It contains all the best practices to name them with more details.
If you want to learn more about clean code, I recommend to read this book: Clean Code: A Handbook of Agile Software Craftsmanship, Robert C. Martin, Prentice Hall, 2008.
Thanks for reading my article. I hope you find it interesting :)