Clean Code: Etiquette for your Code

Rabialco Argana
5 min readApr 4, 2021

--

Image Source from Unsplash

What is Clean Code Architecture?

I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations. Clean code does one thing well.

Bjarne Stroustrup, inventor of C++ and author of the C++ Programming Language

Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests. It has meaningful names. It provides one way rather than many ways for doing one thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API. Code should be literate since depending on the language, not all necessary information can be expressed clearly in code alone.

“Big” Dave Thomas, founder of OTI, the godfather of the Eclipse strategy

I could list all of the qualities that I notice in clean code, but there is one overarching quality that leads to all of them. Clean code always looks like it was written by someone who cares. There is nothing obvious that you can do to make it better. All of those things were thought about by the code’s author, and if you try to imagine improvements, you’re led back to where you are, sitting in appreciation of the code someone left for you — code left by someone who cares deeply about the craft.

Michael Feathers, author of Working Effectively with Legacy Code

In recent years I begin, and nearly end, with Beck’s rules of simple code. In priority order, simple code:

• Runs all the tests;

• Contains no duplication;

• Expresses all the design ideas that are in the system;

• Minimizes the number of entities such as classes, methods, functions, and the like.

Ron Jeffries, author of Extreme Programming Installed and Extreme Programming Adventures in C#

You know you are working on clean code when each routine you read turns out to be pretty much what you expected. You can call it beautiful code when the code also makes it look like the language was made for the problem.

Ward Cunningham, inventor of Wiki, inventor of Fit, coinventor of eXtreme Programming. The motive force behind Design Patterns. Smalltalk and OO thought leader. The godfather of all those who care about code.

How to implement Clean Code?

Here are some tips for your code to implement clean code :

  • Make readable code for people

The purpose of these tips is when you want to review your code you can read it easily without having a problem when you read it. Another purpose is there will always a chance that another human will get to our code, or will have to work with it. So, in this case, you will help them to understand it quickly and easily.

// Bad Example.alert{background-color: #43978D;color: white;font-family: 'Open Sans', sans-serif;}// Good Example.alert{     background-color: #43978D;     color: white;     font-family: 'Open Sans', sans-serif;}
  • Use a meaningful name for variable, function, and methods

The purpose of these tips is so the variable/function/method names descriptive enough that other people, and not just us, will be able to understand. In other words, the name itself should suggest what the variable, function, or method is used for, or what it contains.

// Bad Exampleusrnm = form.cleaned_data.get('usrnm')pswrd = form.cleaned_data.get('pswrd')// Good Exampleusername = form.cleaned_data.get('username')password = form.cleaned_data.get('password')
  • One function, one task

The purpose of this is so the function that being made will focus on one task and one task only. These simple tips changed everything and helped you write clean code, at least cleaner than before. From that moment, other people were finally able to understand your code. Or, they didn’t need as much time as they needed before. The functions and methods also become predictable. And, naming got much easier as well.

// Bad Exampledef render_login_logout(request):
if login:
form = AuthenticationForm() username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) else: return render(request, 'logout.html')// Good Exampledef render_logout(request): return render(request, 'logout.html')
  • Make comment for clarification and explanation

The purpose of these tips is so people know what the code does easily without guessing or understand it first. When you explain your reasons, other people may find a better way to solve the problem and improve the code. This will be only possible because they know what the problem is and what is the desired outcome. It would be much harder for others to create a better solution without knowing this information. Or, they may not even try it because they wouldn’t think there is a need for it. They could think that it was our intent.

// Good Example def user_logout(request):    request.session.flush()    logout(request)    return redirect('login_petani_username')
// Bad Example# Function for login_petani_username.def user_logout(request): request.session.flush() logout(request) return redirect('login_petani_username')
  • Be consistent

When you write your code, you must find your specific coding practices or style you like and should stick to it and use it everywhere. The purpose of this tip is when you want to benchmark to your previous code you can read it or continue where you left off easily. To implement these tips you can practice by using the same style in all your projects.

  • Review your code regularly

The purpose of this tip is so you can see if there any mistake in your previous work and fix it. Therefore, when you want to continue your work to the next step it will help you to write code easily. When you write something, you should review it on a regular basis, clean it up and try to improve it. Otherwise, if we don’t review and update our old code it will soon get outdated. Just like with our devices. If we want to keep them in the best shape we need to update them regularly.

  • Implement TDD

The purpose of this tip is so you can have a safety procedure for your code by making code by test-driven. Therefore, when you finish your code the feature will correspond to what you intend to and reduce bugs possibility.

Example of TDD implementation with a unit test :

Example of TDD implementation with functional test :

Disclaimer :

This is my first article for my Individual Review for PPL course at the Faculty of Computer Science, University of Indonesia.

Resources :

--

--

Rabialco Argana
Rabialco Argana

No responses yet