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
xxpertHacker (485)

CamelCase is not a standard to just abide by, each and every language has their own rules and best practices, btw: snake_case is far more readable than camelCase; example:

let randomIntegerA = 1;

or

let random_integer_a = 1;

the _ mimics an actual space, leading to a more real seperation of words.

NEVER use comments.

What!? Comments aren't an excuse to write poor code, they're to explain why decisions were made, code can do anything, but comments explain why you did it. I can easily see what you did, you made a function to return a random number, cool, but why did you prefer your idea, over... say my idea? No one will even know, and that is why comments exist. I hope you see the light.

JustAWalrus (1158)

You should always be using best practices that should be readable in your code. For example, I shouldn't make my own printf with inline assembly in C for no good reason I should use the best practice. And if you ever even think about using a comment, you should revise your code. camelCase is the standard in the language that aren't the dirty scripting languages. And it is entirely your opinion that snake_case is easier to read, so I am still at a loss as to why you put that in there. If someone used a certain idea over your idea it is probably because their idea was the best practice or fit better in the section than whatever idea you may or may not have even come up with. The naming schemes and other principles if used to their full power, should show why you did a certain method of doing something. There is only 1 excuse for a comment which is to state who wrote the code. @StudentFires

xxpertHacker (485)

@Wuru C and C++ are real programming languages, that use snake_case.

JustAWalrus (1158)

So that's all you have in defense, huh? @StudentFires

xxpertHacker (485)

@Wuru My snake_case > camelCase is based on actual prior research.

And foolish script kiddies write garbage code all the time, usually they don't make the best decisions. It can't be determined why they do anything, but hey, maybe I should just leave what I see as it is, instead of question and improve it.

JustAWalrus (1158)

First of all, you used way too many commas and have suffered from runOnSentanceDisease. Secondly, I was just pitching a naming scheme that worked and that a lot of people like. I don't make the industry standards here. And lastly, you have seemed to stop rambling all your comments nonsense. Could it be that I was right? @StudentFires

xxpertHacker (485)

@Wuru No, I was just afk, and pretty tired.

xxpertHacker (485)

@Wuru Also, looking back at my writing: my initial writing had exactly one point at which I had used a comma where a semicolon would've been more grammatically correct; otherwise, I wasn't creating run-on sentences. Even that last sentence wasn't a run-on sentence, it was a complex, compound sentence.

Actually, looking at it closer, I was missing a comma!

Besides that, it just seems like you're a lost soul.

JustAWalrus (1158)

Still dodging comments... I guess that I was right. Please refrain from calling me a "lost soul" you're probably too young to even understand what your saying. And because you are also too young to comprehend it the comma thing was a joke used to be a fake hypocrite, via also going off on tangents and beating around the bush. Maybe, before you say anything, you can think. @StudentFires

xxpertHacker (485)

@Wuru Too young? Because I'm some child? Okay, sure. Pretty sure I've got something better to do than continue this.

JustAWalrus (1158)

1:

Pretty sure I've got something better to do that continue this

2:

Too young? Because I'm some child?

I'll even list your mistakes:

  • Using because at the start of a sentance
  • "Got something better to do that continue this."

Nice grammar buddy, sure you've got it all figured out.
@StudentFires

xxpertHacker (485)

@Wuru Oh right, I forgot you have spellcheck on your side. Who cares about 1 letter? Why in hell does grammar matter that much here?

JustAWalrus (1158)

I'm deviating from the subject. Making another joke to give you a taste of your own medicine. @StudentFires