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
Jakman (457)

Nice course. You might want to tell the script kiddies not to use global variables all the time

JustAWalrus (1158)

Yea, they need to be stopped. @Jakman

JustAWalrus (1158)

In my opinion I like functional variables like that of F# better than the norm, but global ones are a crime against humanity. @Jakman

Jakman (457)

@Wuru these are usually the little python kids who have hundreds of cycles and dont know jack about a return value.

JustAWalrus (1158)

I do F# and C++ the most on my own time. @Jakman

DynamicSquid (4359)

@Wuru global variables are hated in C++, especially since we have pass-by-reference

Jakman (457)

@DynamicSquid nice to hear that every nearly language community hates globals.

HahaYes (1247)

@DynamicSquid hehe that one person who uses auto instead of int, long, string

DynamicSquid (4359)

@HahaYes

Yeah, i agree lol, but...

std::initializer_list<std::size_t*>::iterator it = arr.cbegin();

or

auto it = arr.cbegin();

it's not completely useless

Jakman (457)

@DynamicSquid is auto all inclusive of every cpp type?

DynamicSquid (4359)

@Jakman Yes, I think. Even user defined ones

xxpertHacker (485)

@Jakman Auto is more inclusive that you think, destructuring in C++ isn't possible without auto at all!

xxpertHacker (485)

@Jakman Global variables are okay, as long as they're constant, btw, usually functions are global, although namespaces are much better.