Clean Code for Better Team Work Environment

Taufik Algi
4 min readMay 24, 2021
https://blog.knoldus.com/keep-your-code-clean/

Team project is common in creating software. To solve the problem that exists at this time, doing a project alone sometimes can be hard. Thus usually developers doing projects in a team. There will be more than one person with different knowledge and coding style that could be a problem as a team, especially when there is a team member who does not understand clean code. For example, the readability of the codes that they write is so bad so the other member needs more time to understand the code before continuing the work. Hopefully, at the end of this article, we can avoid being the member who ruined the project.

What is a Clean Code?

There are different definitions of clean code, but there is no definitive distinction. In short, we could say clean code is code that easy to understand and easy to change. What does that mean? Easy to understand means that the code of course is easy to read, whether by the original author or somebody else in your team. It minimizes the need for guesswork and the possibility of misunderstandings.

Normally, a developer does not create software in one go. There are updates on features or the quality of the codes. That is where easy to change show it “skills”. Easy to change means the code is easy to extend and refactor, and it is easy to fix bugs in the codebase. This could not be achieved if the person who would make the changes do not understand the code. It is different when they understand the code and also feel confident that the changes introduced in the code do not break any existing functionality.

By writing clean code, you are helping your future self and your co-workers by reducing the cost of maintenance of the application you are writing. It is easier to estimate the time needed for new features and it is easier to fix bugs.

Characteristics of a Clean Code

  1. Elegant
    Clean code should be pleasing to read. Reading it should make you smile the way your favorite happy song would.
  2. Focused
    Each function, class, or module exposes a single-minded attitude that remains entirely undistracted, and unpolluted, by the surrounding details.
  3. Readable
    Clean code must be easy to read. The naming, conventions, spacing, structure, and flow should be paid appropriate attention to details and designed with the reader’s mind so the reader will immediately understand the code right away.
  4. Testable
    Testing the code is part of software engineering (or simply creating software). So clean code also must be testable.
  5. Contains no duplication
    Duplicated code makes your program lengthy and bulky. Having well-written code with the least duplications will make sure that your program runs faster and occupy less space.

How to Write Clean Code?

Meaningful Names

Use intention descriptive names. Choosing good names takes time but save more than it takes. The name of a variable, function, or class, should answer all the big questions. It should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent.

E.g.:

int x; // first number
int y; // second number

A better name would be

int firstNumber;
int secondNumber;

Class Names — Class names should have nouns or noun phrases like Customer, User, or Car. Avoid words that are verbs.

Method Names—Methods should have verb or verb phrase names like deletePage or save.

How do I implement clean code for my project?

final Uri verifyAgentUri = Uri.parse('https://admin-apis-dot-warung-isi-ulang.et.r.appspot.com/api/v1/reseller/approve');Future<Data> verifyAgent(OTPAuthentication auth, String status, String storeUID) async {
String token = await auth.getTokenId();
final response = await http.post(verifyAgentUri,
headers: {"Authorization": "Bearer $token"},
body: jsonEncode(
<String, String>{'status': status, 'storeUID': storeUID}));
if (response.statusCode == 200) {
return Data.fromJson(jsonDecode(response.body));
} else {
int status = response.statusCode;
throw Exception('status code: $status');
}
}

In the code snippet above, I already choose meaningful names for a final variable (constant) that has a value of URI of verify agent endpoint. And below that code, there is a method/function whose name is a verb phrase.

That function is used to verify agent with status value either “Approved” or “Rejected”. I do not create two different methods to approve and reject agents to avoid duplication and also application of DRY (Don’t Repeat Yourself) principles.

References

--

--

Taufik Algi

Undergraduate student of Computer Science at Universitas Indonesia