Learn to Code via Tutorials on Repl.it!

← Back to all posts
Clean Code Introduction
JustAWalrus (1146)

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

Commentshotnewtop
JustAWalrus (1146)

Upvote for me to work harder on tutorials.

Jakman (455)

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

JustAWalrus (1146)

Yea, they need to be stopped. @Jakman

JustAWalrus (1146)

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 (455)

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

JustAWalrus (1146)

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

DynamicSquid (3635)

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

Jakman (455)

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

HahaYes (1229)

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

DynamicSquid (3635)

@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 (455)

@DynamicSquid is auto all inclusive of every cpp type?

DynamicSquid (3635)

@Jakman Yes, I think. Even user defined ones

xxpertHacker (390)

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

xxpertHacker (390)

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

Bookie0 (4039)

um lol © 2020 Bookie0


anyways
also i disaggree with you about comments. I think they are essential, because they can seperate parts. for example, you can have a part named imports, or functions, or classes, etc.


also

when you say "blahblahblah", not many people do that to call variables. So that example isnt really good in my opinion. and sometimes code can be complicated, so thats why when people share big projects on repl.it, they put comments so that beginners or people that dont understand will be able to comprehend what the code is about and then they can learn!


Also you can add a part saying about spaces, like make the code breathable but not too spacey either.
eg:

A:

def getRandNum():
  randNum1 = randint(1,5)
  randNum2 = randint(1,4)
  randNum3 = randNum1 + randNum2
print("two random numbers added together:")
getRandNum()

B:

def getRandNum():
  randNum1 = randint(1,5)

  randNum2 = randint(1,4)

  randNum3 = randNum1 + randNum2

print("two random numbers added together:")

getRandNum()

C:

def getRandNum():



  randNum1 = randint(1,5)


  randNum2 = randint(1,4)



  randNum3 = randNum1 + randNum2



print("two random numbers added together:")



getRandNum()

so i think the best in that case is B

JustAWalrus (1146)

I agree with the spaces thing and that will be added soon. But as for the comments your code should be able to speak for itself if you use correct variable and function names. Make your lines short and all around readable your code shouldn't need comments. Anyway thanks for the suggestion! @Bookie0

Bookie0 (4039)

yea i see what you mean. np for the sugggestion ;) @Wuru

Coder100 (8832)

Hi! Although comments are sometimes bad, they usually do more good than bad:

/**
 * @param {number} a the first number
 * @param {number} b the second number
 * @returns {number} the sum.
 */
function something(a, b) {
  return a + b;
}

As you can see, JSDOCS are super awesome ;)

DynamicSquid (3635)

Is it fine if my code doesn't look like this:

But instead looks like this:

JustAWalrus (1146)

Haha, I guess the second one is better. @DynamicSquid

xxpertHacker (390)

@DynamicSquid The first one has meat, and actually looks better lmao!

ZDev1 (657)

can ya gimme da link of da huge web dev tutorial?

JustAWalrus (1146)

For now, @Coder100 and I are still working on it. Will ping you when it is done :) @ZDev1

DungeonMaster00 (133)

@ZDev1 it appearently might be canceled

xxpertHacker (390)

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 (1146)

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 (390)

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

JustAWalrus (1146)

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

xxpertHacker (390)

@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 (1146)

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 (390)

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

xxpertHacker (390)

@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 (1146)

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 (390)

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

JustAWalrus (1146)

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 (390)

@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 (1146)

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

DungeonMaster00 (133)

just use comments to divide up sections of the program. the code can speak for themselves in those sections.

EpicGamer007 (611)

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 (1146)

Nope, your code should speak for itself. @AbhayBhat

Jakman (455)

also some people use repl because they like me, have potato pc's. Lots of people here have not made a python lib in C. but it does make everything a few seconds faster. I program like its 1954 on my own computer. It hurts but it works.

GermanIvk05 (0)

Nice man, I enjoyed reading this. Thanks.

JustAWalrus (1146)

@adityaru I see you are a fellow cuber :P

adityaru (150)

@Wuru Yea, my pb is 23 seconds, what is yours?

JustAWalrus (1146)

9.13 seconds. I use Roux what do you use? @adityaru

adityaru (150)

@Wuru WOW!!!! Anyways, CFOP

CodeLongAndPros (1374)

they failed to abide by the practice.

Wait what?
I just glanced at System V's Killall, and it looked very clean.

CodeLongAndPros (1374)

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

You do know it's not LOC that determines disk space but bytes?

To put a 🐧 into your code is a whole lot more bytes than [.

We are not using 1 line displays anymore guys.
True, but which is better:

l = ["1", "2", "3"]
newl = []
for i in l:
  newl.append(int(i))

or:

l = ["1", "2", "3"]
l = [int(x) for x in l]

You seem to like comparing a computer in 1970 that runs UNIX to a bloated, fat, blubbery Windows 10 computer in 2019.

But what if I told you that most things have a history?
Keeping it small was one reason.
But just because we have bigger backpacks, does not mean we have to cram it full of stuff.
Sometimes we need to use Gb of ram or Gb of hard disk space.

But there's an entire community dedicated to keeping things light and fast.

JustAWalrus (1146)

@CodeLongAndPros We now have the space to make our code nice. And just because you may or may not be too lazy to make your code readable doesn't make me wrong. You are just making your code a pile of unreadable cypher-text.

CodeLongAndPros (1374)

@Wuru I'm just saying we had the space in 1970.

sugarfi (527)

keep in mind python uses snake_case instead of camelCase

HahaYes (1229)

my code is so messy. just ask @DynamicSquid

DynamicSquid (3635)

Also thank you, I also hate adding comments to my code