What Is Clean Code
Richard Porteous
What is clean code (beginner)
“Code must be readable”
If you can quickly understand the code by skimming over it with your eyes, then your code is readable.
This must hold true even if you put down the code and don’t come back to it for three months.
The code also needs to eliminate (reduce) dependencies, and use concepts like dependency injection where necessary.
Why you should care if your code is clean
-
If you can read and understand the code quickly it is more likely going to be easy to change. The place to change it will become obvious.
-
Moving code around will have fewer side effects. You may want to do this when you find the current design needs to be changed (even slightly).
-
Even after initial development Clean Coding should still be practiced.
Rewriting an app shouldn’t be necessary after a few years. And new features can be easily added to the application.
Sadly messy/spaghetti/smelly code often needs to be rewritten when it becomes difficult to change.
When you are done adding new code or fixing a bug you must check to see if the code is still easy to read.
Refactoring
is simply keeping “connected code” together and moving it around WITHOUT behaviour changes. These changes are done in small testable steps.
Product design changes or just simply making code readable should pay attention to Refactoring.
Refactoring has lots of suggestions on best ways, but ultimately some of the changes will be for preference or product design.
So how does code get smelly?
Most people are taught to write code fairly well and even some best practices. Designs may not be perfect, but overall it should be good.
Making changes to code seems to be where most bad things happen.
Learning Refactoring will help.
When did I learn this
I knew a lot of good practices but had a few bad habits, and my code wasn’t easy to change.
-
I wrote an android 2.3 java game specifically to teach myself how to refactor and to have clean code. I grabbed “free to copy” code from several “YouTubers” and “Bloggers” and threw it all together to make one game. What a mess!
-
I got the game working as is, but it was full of bugs as you could expect. I started making changes to make the code easier to understand.
-
After many changes, I found keeping track of changes, where the code broke, or when it last worked as intended was difficult to keep track of. My version problems grew and I looked and found Git which is a free version system.
Git is a good place to start with version-control-software (VCS) as it creates a local repository even if you have an online repository - so you can use it offline if needed.
Now Git doesn’t just keep track of versions, it keeps track of every single change.
You do need to keep your ‘commit’ changes small with a clear message. This helps finding where a change was made easily without needing to search though.
I learnt more in that 6 months than I had in the decade before that, well at least it felt like it.
Bugs are easy to find
Cleaning & Refactoring is not just cleaning and moving stuff. it is also a bug locating process.
Yep, a refactor that should not change behavior, but does, often has exposed a bug or difficult to understand code. If that happens, you need to break your refactoring into smaller parts.
I found many bugs, dependencies, and hard to understand code by simply by moving code around.
Other basic common sense stuff
- Function names should help make the code understandable in a glance. “Oh I know what that does” is a good indicator.
- code simple - no more than one conceptual thing per method or function i.e. makes it easy to name. It can be further refactored later if necessary. So don’t fret if you have a mess, just do one thing, then another time.
- Avoid duplication i.e. “fewer places to make changes”
Readable code means you can have less comments.
When are comments bad
Comments are generally bad in a non-educational setting. People changing code don’t usually change comments so they become outdated and meaningless.
This is why descriptive code naming is important.
Remember
Clean Code is more a GUIDE meant to make you think.
Plan but avoid ‘over planning’.