Clean Code Introduction
JustAWalrus (1158)

Hello, at the moment I am working hard at a HUGE full web dev course with @Coder100 so I needed to get something out here.

Clean Code: An introduction

Think of programming like poetry it should be beautiful and tell a small story about the creator itself not just making it work, or in the case of poetry, rhyme.

Clean Code is the art of making code well, clean.

The code is a relic, for example if the people writing the Unix Kernel at Bell Labs in 1970 would have used this practice we may be able to learn from their code, but nontheless, they failed to abide by the practice.

Is it really enough just to get the code working? I mean, think about it. Go back to some code you wrote a month ago. Can you read it? Maybe slightly but imagine someone who has never read it before trying to read it. That is why clean code is so important.

Practices.

Cleaning up your code isn't hard and here is why. You won't even have to think about it once you do it for a few months.

Code shouldn't be about how small a program should be. We are past the time when computers had 4KB of RAM we have gigabytes of RAM and even hard drives now.

Let's walk through a few practices right now.

Variable and Function names

Variable names should be `camel case meaning that the first letter of the first word should be lower case and the first letter of the following letters should be upper case.

Variable names should be descriptive, not a mess of letters.

Example:

Which one of these is easier to read?

A:

let randnumaa = 1;

B:

let randomIntegerA = 1;

Let me guess. B?

Even B isn't good enough in most cases.

And if you cringed at A let me just say that I know some of you write code like this.

I am even at fault for some of my repls just because I want to get it done quickly.

Comments

I guess you expected me to say "Put comments everywhere!" or some nonsense like that. No.

NEVER use comments.

Why?

I (and many other people) think comments are a poor excuse for messy code.

Which of these is easier to read?

A:

import random
def getRandomNumber():
  randomNumber = random.randint(0, 10)
  return randomNumber

B:

import random
def rannumretaba():
  # Returns random numbers
  blahblahblah = random.randint(0, 10)
  return blahblahblah

Okay, let me guess. A?

While B used comments it was just an excuse to write lazy code.

Your code should explain itself and not rely on comments.

Short lines

DO NOT make super long lines.

We are not using 1 line displays anymore guys.

Example:

int radnumcool(int numa, int numb) {
  return (numa + numb) * 5 + 789 * numa + 5 + 78 + numb;
}

You know how painful that was to read?

Use your lines you have disk space now it is not 1970.

Spacing.

Thanks to @Bookie0 for suggesting this topic!

Spacing is very important in clean code.

Clean Code emphasizes readability and part of that is making breathable code.

What do I mean by this?

Well let me show you an example of breathable code.

#include <stdio.h>

int main() {
  printf("Clean Code is awesome!");

  int x = 6;

  int y = 5;
}

It is spaced out.

You want to refrain from adding too many spaces or too few.

You want to get a sort of goldilocks paradigm of spaces.

Conclusion

I know this was short and Clean Code is a very expansive topic.

If you want to learn more check out this.

And I will NOT be making a series out of this.

Anyway upvoting is caring :) - Bookie0 2020

You are viewing a single comment. View All
EpicGamer007 (749)

Should you use comments at the top of the program to explain what it does? That is what all the tutorials say but I hate to do that.

JustAWalrus (1158)

Nope, your code should speak for itself. @AbhayBhat