Is there a right way to code? Maybe. Depends on who you ask. You’ll most likely get different answers from every programmer you talk to, even if the differences are small.
Programming languages are different. They are written and read differently. Of course, there will be preferences when it comes to typing out each line.
Some people like tabs. Others like spaces.
In some languages, you need a semicolon. In others, you don’t.
Some languages are beginner-friendly and forgiving. Others aren’t.
And on and on and on.
One thing that remains constant, though, is that writing clean code is important.
What Do We Mean By Writing Clean Code?
Computers are smart, so they can do things easily that humans can’t. For instance, they can parse a giant block of code and figure out what to do with it. Whether that block of code is properly formatted for human eyes or not, the machine can read it and carry out instructions.
So, writing clean code is for humans.
If you write code that a computer can’t read, it’ll give you an error message. It won’t say, “I can’t read this because it’s a giant block of code.” It will say, “You forgot something I’m expecting in this giant block of code,” or, “You forgot something I’m expecting in this nicely formatted code.”
Writing clean code is all about making it easily readable and understandable for programmers.
You know, humans.
How Do You Write Clean Code?
Writing clean code begins with understanding how people read.
Line breaks help with scannability, which means information can be parsed more quickly by humans. Short lines also make scanning easier.
Keeping sections of code that are related to one another together also makes it easier for programmers to understand how large chunks of code affect the program.
In fact, keeping related sections of code together can also help computers interpret the code more efficiently, depending on which programming language you are using (such as Python).
All of this comes down to white space and order. If you have those two things, you are well on your way to writing clean code.
- White Space
- Line Breaks
- Short Lines
What About Comments?
Comments in code are essentially helpful notes (we hope) for humans that are not read by the computer. As far as the machine is concerned, comments don’t exist.
So, how are they helpful?
When done properly, comments can let fellow programmers know what a section of code does (or is supposed to do).
Of course, you can write anything in a comment, so comments aren’t inherently helpful.
If you are writing a comment in Python, you’ll start a new line and type:
Without the quotation marks. The space after the # is important.
Each line of code that belongs to the comment will need to start this way.
Other programming languages, like Java and and C, use asterisks and slashes:
If the comment is on a single line, then you can simply type:
// followed by the comment
Even if you think your code is super clean, always leave comments for reference. Your future self and other programmers will thank you for it later.
Why Write Clean Code?
If the program runs, the code is fine, right?
There are two really good reasons beyond aesthetics for writing clean code:
1 You Don’t Write Clean Code for You
Even if you find coding fun and invigorating (like we do), you have to remember that you’re not writing code for yourself. You are writing it for the future. Mostly, you are writing it for other programmers who may need to revisit your code at a later time. But, you are also writing it for future you, which is not the same thing as present you.
Don’t make the mistake of thinking you’ll remember what each bit of code does in the future. Chances are, you won’t, and you’ll have to spend a lot of time figuring it out then.
2 You Don’t Write Clean Code for the Machine
We covered this a bit already, but it’s important to understand, so here it is again. You aren’t writing clean code for the machine. The computer doesn’t care how pretty your code is. As long as it can parse and act on the instructions, it’s good.
But wait, isn’t that an argument for not writing clean code? No, not really. Remember, just because a computer can read your code doesn’t make it acceptable to not write clean code.
See reason number one if you’re still confused as to why.
How Can You Improve Your Coding Skills?
Learning how to write clean code takes time and practice, and that process can be helped along by Hackbright Academy. If you’re new to coding, you can check out the 5-week Prep program to get a taste of what it’s like to be a programmer by gaining foundational knowledge and experience.
- Admissions Office (28)
- Adora Cheung (5)
- Alum (98)
- Blog (162)
- Career Services (26)
- Diversity (18)
- Engineering Advice (67)
- Hackbright Field Trips (24)
- Hackbright Mentors (21)
- Hackbright News (109)
- Homejoy (5)
- Podcast (2)
- Profiles of Woman Engineers (110)
- Recruiting & Hiring (16)
- Resources (47)
- Student Blogs (24)
- Tech (53)
- Thought Piece (21)
- Uncategorized (63)
- Video (20)