Clean code i don’t really know before my first team project. Case Django.
Table of Contents:
- Table of Content
- Introduction : introduction as to why i write this
- Clean Code in Nutshell : orientation to clean code. what is it
- Importan Clean Code Concept (Mostly Overlooked): main point of this article. clean code concept mostly overlooked or forgotten
- TL;DR : summary
Introduction
As a programmer we might often hear of what clean code is. In my three year studying computer science i have learn this in and outside class. I am confident that at least my code is clean enough to be understood by others. But is it??
This year I have an opportunity to participate in several team project with a bigger project than I ever have done. Now is the time to prove my worth. But reality nuts! There is time i realize that i don’t understand the best practice for this, my friend don’t understand my code, the structure is not unsightly, etc. From that realization i learn even further. I discuss more. And this article will write what I have learn in the past few months.
Clean code in nutshell
Dirty code is only for yourself code. if you write Dirty code for group projects , might as well not write anything. As someone will take time to understand it as much as you write it or even more.
Well known basic clean code concept :
- Software Craftmanship
- Object Oriented Programming (SOLID)
- KISS (Keep It Simple Stupid!)
The “In Nutshell” :
- Follow standard, simple, and understandable.
Important Clean Code Concept (Mostly Overlooked)
- Error Handling
Handling Error is as important as handling the main use case. use try except. and use if ([something unintended happen]) : throw [relevant exception]. - Testing
having a test make sure your code working as intended. and people can also understand your code just by reading the test. so it also increase your code readability. - Use Linter
Linter is a tools to remind programmer of errors, bugs, stylistic errors, and suspicious constructs in their code. Linter remind you by following coding standard accepted for the language you are using. In python you can use pylint.
- Import Order
Beside to write alphabetically. here is the order you might need to know.
- Import [Python Standard Library]
- From [Python standard library] import [something]
- Import [Third Party Library]
- From [Third Party Library] import [something]
- Import [your other module]
- From [your outher module] import [something] - Url naming
Url are treated like variable. it should be snake_case but adopting from big company kebab-case it acceptable. - Conditional precedence (if else, etc)
The most used condition are written uppermost. And the second most used follow until all condition written. - if-else-return
there is a debate about this topic. below is written from the best of the best case to the worst of the best case
#(1)also acceptable by pylint. better readability
if condition :
return_value = "x"
else :
return_value = "y"
return return_value#(2)this will pass pylint. less line
if condition :
return "x"
return "y"#(3)not accepted by pylint. better readability
if condition :
return "x"
else :
return "y"#notes most language doesn't encourage multiple return.
- Modularity
While modularity are well known part of clean code. its not really well implemented especially by newbie developer.
- easier to test (especially unit test)
- easier to understand
- easier to reuse (more adaptable for different case) - Naming
It is said to be the hardest part of programming. Unlike naming a baby human naming a variable need to follow standard, meaningful, short. And what you need to know is different element might have different naming standard.
- Package : lowercase / snake_case
- Module (.py) : lowercase / snake_case
- Class : PascalCase
- Method : camelCase
- Variable : lowercase / snake_case
- Url : kebab-case / lowercase / snake_case
- Static : treated like variable or url
TL;DR (Too Long Didn’t Read)
As we know clean code is important so others can understand your code. There is some basic theory newbies might already know. but there is some point newbies missing in practice. In Python (Django) Case, here is what I found forgotten : import order, url naming, conditional precedence, modularity, naming.