0% found this document useful (0 votes)
48 views14 pages

Slides Sheshnath Clean Code Seminar Project

Clean code is code that is readable, maintainable, testable and elegant. It follows rules like using descriptive names, keeping things simple, having single purpose functions, minimizing parameters, and avoiding duplicate code. Clean code communicates intent to both the original author and future readers in a way that is clear and avoids misinterpretation. It is written to be easily understood even when revisited after time has passed.

Uploaded by

hyeshesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views14 pages

Slides Sheshnath Clean Code Seminar Project

Clean code is code that is readable, maintainable, testable and elegant. It follows rules like using descriptive names, keeping things simple, having single purpose functions, minimizing parameters, and avoiding duplicate code. Clean code communicates intent to both the original author and future readers in a way that is clear and avoids misinterpretation. It is written to be easily understood even when revisited after time has passed.

Uploaded by

hyeshesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Clean Code

*It helps a programmer to be a good programmer*


Presented by -
Sheshnath Yadav
What is clean code?
Code which is readable, maintainable, testable
and elegant. Example:

Any fool can write code that a computer can understand. Good programmers write
code that humans can understand.
Why should you care to keep your code clean?
● Well Structured Code is easy to
understand.
● Better use of one’s time - for
author and fellow developers.
● Saves Costs by Saving time.
Easier Onboarding of new
members.
● Easier Debugging and efficient
maintenance.
Rules that will help you always keep your code clean

1. Use descriptive names


● Bad example:
var d; // elapsed time in days

● Good example:
var elapsedTimeInDays;

var daysSinceCreation;

var daysSinceModification;

getName() , sendEmail() …
Boolean name should answer y/n

isGoldClient() , areHostsvalid()
2. KISS(Keep it simple,Stupid)
● “Make everything as simple as possible but not
simpler”
- Albert Einstein
● Keep Code as Simple as possible.
● Engineers like to complicate things.
Unnecessary complexity should be avoided.
● It makes it easy to understand and maintain.
● How to keep code simple?
● Break down the problem into small steps and
write minimal code to implement them.
● Also, always ask yourself and the reviewer -
can this be written in simpler way?
3. Clean code requires clear information

Ifyou use words that are too ambiguous, you can end up with messy code.
Always make sure that your code doesn’t contain misinformation.

Say what you mean :-


● This is pretty straightforward, but it’s probably the most common and maybe
the easiest one to forget.
● Easily the most frustrating thing for another developer looking at your code is
seeing a variable with a misleading name or, worse, named with a single
letter.
4 . Make sure names are easy to pronounce
only you understand is never a good solution. Although it may be obvious, it’s good to
remember this rule, especially when you name variables in a hurry.

● Bad example:

const aaabbd = moment().format("YYYY/MM/DD");


Int getinvcdtlmt()

● Good example:

const currentDate=moment().format("YYYY/MM/DD");

Int getInvoiceableCreaditLimit()
5. Give each function a single purpose
Every function or class should
represent one concept or perform
one action only. openDoor should not
also call putClothesOn. Once it does
two things, it becomes more difficult
to test

Example →
6 . Polish your comments
The common trap, especially for beginners, is
overexplaining how your code works in comments.
Unfortunately, too much information in the comments can
turn your code into chaos.

Here are some rules that will help you write your
comments like a professional:

● Don’t keep extra code lines in comments – never


leave your ideas for later because it’s confusing to
others. It’s better to delete unnecessary code
already!
● Shorten your comments – too much writing can
distract your teammates from the main points.

Commenting saves lives — or at least headaches


7 . Minimize the number of parameters
It’s important to minimize the no of parameters for methods. Some time large no of parameters rises
problems to handle the methods.

For example-

myBadMethod(“john” , “Michael” , “Paris” , “St. Albergue” , 1234);


What is Paris here ?

‘Paris’ is Street .

It does too many thing ? So break down it for single purpose

For more , their order confuses the reader

Solution - Try to introduce a parameter object


params.setStreet(“Paris”);
And see how much code it simplifies ………
No Boolean parameters myBadMethod(params);
removeOrder(customer , false , true ,false );
8 . Don’t repeat yourself
Good morning, good afternoon, good evening, good night, what they are in
common? Yeap, the word “Good”. The concept DRY consists in look this
and “dry” your repetitions, after dry could be: Good, morning, afternoon,
evening and night. Being thus the word would serve for all, without losing
the logic.

● A piece of code should not be repeated across the software.


● Reduce duplicate and increase reusability .
● Reusability means less code which leads to better maintainability.
● Opposite of DRY is WET - Write Everything Twice, Waste Everyone’s time,
We Enjoy Typing. This applies when you over repeat.
9 . Keep it classy
● Similar to the functional problem, if there’s a large amount of functionality you’re
keeping all in one place, it could be better to create a separate class to handle that
functionality.
● When it comes to writing, reading and maintainability, clean code is essential.
● The important thing is this: Keep it tidy, clearly sectioned, and consistent.
10.We don’t code, we communicate !
We are working in a team , we are not a rockstar that composing their code each . Here we are
working in team for enterprise application , basically in large application development , we should
have convention ,we should respect each other .
● Respect your readers
Details matter : 10x more reading , remember ?
● Write children Literature
Always the simplest code that works. Never obfuscate
- Emagin you will be reading that code 3 month from now 10 o’clock in the evening in the
delivery day ? Emagin that and then write your code
Clean , Simple code , Simple as possible , fast , non overlapping
Thank You!

You might also like