SlideShare une entreprise Scribd logo
1  sur  26
Clean Code


   “Master programmers think of
 systems as stories to be told rather
    than programs to be written”




                      By -: Uday Pratap Singh
Agenda

   What is clean code
   What makes us write bad code
   Talk about meaningful names
   How to make functions readable &
    understandable
Clean Code
A lot like painting a picture
What is clean code

   Is   it   Readable?
   Is   it   Maintainable?
   Is   it   Reusable?
   Is   it   Testable?
What is clean code
   Should be Readable like a well written
    book.
   Should be crisp. Contain only what is
    necessary.
   Provides one way rather than many ways
    of doing one thing.
   Does one thing well. Each class, function
    exposes single minded attitude.
What is clean code
   Easier for other people to enhance it.
   Smaller is better.
   Contains no duplication
   Close to expectations. No surprises
What is clean code


“You need to write that minimizes the
   time it would take someone else to
   understand it – even if that someone
   else is you – Dustin Boswell and
   Trevor Foucher”
Do you know?


       Time spent reading versus writing
               is well over 10:1




if you want your code to be easy to write, make it easy to read
Total cost of owning a Mess
What makes us write bad code?

   Were you trying to go fast?
   Tired of working on this program and
    wanted it to be over.
   Looked at the backlog
   To clean it up later.

    “We forget later equals never”
Why does good code rots?




       Requirements change
          Tight schedule
         Stupid Managers
                ...
Suppose if patient demands to stop hand
 washing because it’s taking too much time.
Doctor will refuse to do that because he knows
              the risk involved in it.


  Fault is Ours. We are Unprofessional.
Meaningful Names
Choosing meaningful name takes time
    but saves more than it takes
Rules
   Name should tell you why it exists, what it
    does, and how it is used
     e.g: int elapsedTimeInDays
   Make meaningful distinctions.
    e.g: customerInfo is indistinguishable
    from customer
   Use Pronounceable names.
    e.g: genymdhms
Rules Cont...
    No magic String or magic numbers.
    Use searchable names
    e.g; MAX_CLASSES_PER_STUDENT is
       easily searchable rather than 7.
    Pick one word per concept.
    eg: it’s confusing to have fetch, retrieve,
     and get as equivalent methods for
     different classes.
Rules Cont...
    You also don’t need to prefix member
     variables.
    e.g; personName variable in Person class
    Class names should have noun or noun
     phrase names like Customer, Account,
     AddressParser etc.
    Method name should start with the verb.
    e.g; postPayment, save, delete etc.
Rules Cont...
   Use problem domain names.
   Variable holding collection should be
    plural.
    e.g; persons, employees
   If a variable is expected to have a
    default value, then assign it at the time
    of declaration
Rules Cont...

   Don’t be cute.
       Choose clarity over entertainment value.
       Use kill rather than whack.
       Say what you mean. Mean what you say.
Functions
Should be easy to read and understand
    which communicates its intent
Rules
   Small should be smaller.
       Avoid more the 10 lines.
    Do not write more than 120 character in
     one line ( Line should be visible at one
     glance)
   Should be properly indented.
       It should not be more than 2.
       Blocks in if, else or loops should be only 1
        line. Probably that 1 line should be a method
        call.
Rules Cont..
   Do one thing.
       Should do it well.
       Should do it only.
   Avoid multiple return statements.
   Long descriptive name is better than
    long comment.
   Well defined parameters (with
    descriptive names) are better than a
    map as parameter.
Rules Cont...
   Step Down Rule – Code read from top to
    bottom.
   Function Arguments
       More than three (polyadic) requires very
        special justification—and then shouldn’t be
        used anyway.
   Passing flag argument is a terrible
    practice. It loudly proclaims that
    the function does more than one
     thing.
Rules Cont...
   Has no side effects.
       checkPassword method should only check
        whether password is correct or not, it should
        not initialize the user session. If we want to
        initialize the session then method name should
        be checkPasswordAndInitializeSession.
       Function should either change the state of
        object or it should return some information
        about the object. Doing both often leads to
        confusion.
Rules Cont...
   Prefer exception to returning error codes.
   Extract body of try catch blocks into
    method to increase the readability and
    structure of the code.
   DRY (Do not Repeat Yourself).
“Leave the campground cleaner
        than you found it”
Thanks...

Contenu connexe

Tendances

Clean Code and Common Engineering Practices
Clean Code and Common Engineering PracticesClean Code and Common Engineering Practices
Clean Code and Common Engineering PracticesRuth Sperer
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentSamnang Chhun
 
Clean code and Coding Standards
Clean code and Coding StandardsClean code and Coding Standards
Clean code and Coding StandardsMahesh Salaria
 
Advanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakesAdvanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakesMilan Vukoje
 
Best Practices of Software Development
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software DevelopmentFolio3 Software
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit TestingDror Helper
 
Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)
Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)
Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)El Mahdi Benzekri
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)Rodrigo Lopes
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summaryJan de Vries
 
Behavior Driven Development
Behavior Driven Development Behavior Driven Development
Behavior Driven Development Dhawal Joshi
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design PrinciplesSerhiy Oplakanets
 
Being Good Developer
Being Good DeveloperBeing Good Developer
Being Good DeveloperSally Ahmed
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on boardRuth Sperer
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
Common design patterns in php
Common design patterns in phpCommon design patterns in php
Common design patterns in phpDavid Stockton
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guidelineDhananjaysinh Jhala
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelinesbrijraj_singh
 

Tendances (20)

Clean Code and Common Engineering Practices
Clean Code and Common Engineering PracticesClean Code and Common Engineering Practices
Clean Code and Common Engineering Practices
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Clean code and Coding Standards
Clean code and Coding StandardsClean code and Coding Standards
Clean code and Coding Standards
 
Advanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakesAdvanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakes
 
Best Practices of Software Development
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software Development
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit Testing
 
Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)
Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)
Caring About Code Quality (Clean Code, GRASP, Effective Java, Design Pattern)
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
 
Clean Code summary
Clean Code summaryClean Code summary
Clean Code summary
 
Behavior Driven Development
Behavior Driven Development Behavior Driven Development
Behavior Driven Development
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
 
Being Good Developer
Being Good DeveloperBeing Good Developer
Being Good Developer
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on board
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Common design patterns in php
Common design patterns in phpCommon design patterns in php
Common design patterns in php
 
Coding standard and coding guideline
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guideline
 
Coding standard
Coding standardCoding standard
Coding standard
 
Code Review
Code ReviewCode Review
Code Review
 
Make a better with clean code
Make a better with clean codeMake a better with clean code
Make a better with clean code
 
Coding standards and guidelines
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelines
 

En vedette

Limpiando espero la arquitectura que yo quiero
Limpiando espero la arquitectura que yo quieroLimpiando espero la arquitectura que yo quiero
Limpiando espero la arquitectura que yo quieroJose Manuel Pereira Garcia
 
Angularjs101-les_SPA:support de la video
Angularjs101-les_SPA:support de la videoAngularjs101-les_SPA:support de la video
Angularjs101-les_SPA:support de la videoeric German
 
Cleancode / Tocea / Introduction
Cleancode / Tocea / IntroductionCleancode / Tocea / Introduction
Cleancode / Tocea / IntroductionSylvain Leroy
 
Design applicatif avec symfony2
Design applicatif avec symfony2Design applicatif avec symfony2
Design applicatif avec symfony2RomainKuzniak
 
Visteme con 'Clean Architecture' que tengo prisas
Visteme con 'Clean Architecture' que tengo prisasVisteme con 'Clean Architecture' que tengo prisas
Visteme con 'Clean Architecture' que tengo prisasJosé María Pérez Ramos
 
Méthodologie de gestion des tâches dans les projets web
Méthodologie de gestion des tâches dans les projets webMéthodologie de gestion des tâches dans les projets web
Méthodologie de gestion des tâches dans les projets webRomainKuzniak
 
Solution de collecte intelligente des déchets (Ecube Labs)
Solution de collecte intelligente des déchets (Ecube Labs)Solution de collecte intelligente des déchets (Ecube Labs)
Solution de collecte intelligente des déchets (Ecube Labs)Ecube Labs
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsRoss Tuck
 
Composer in monolithic repositories
Composer in monolithic repositoriesComposer in monolithic repositories
Composer in monolithic repositoriesSten Hiedel
 
Command Bus To Awesome Town
Command Bus To Awesome TownCommand Bus To Awesome Town
Command Bus To Awesome TownRoss Tuck
 
Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)Ryan Weaver
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Androidintive
 
Clean architecture with ddd layering in php
Clean architecture with ddd layering in phpClean architecture with ddd layering in php
Clean architecture with ddd layering in phpLeonardo Proietti
 
Domain Driven Design using Laravel
Domain Driven Design using LaravelDomain Driven Design using Laravel
Domain Driven Design using Laravelwajrcs
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksPhill Sparks
 
Le pattern View Model avec Symfony2
Le pattern View Model avec Symfony2Le pattern View Model avec Symfony2
Le pattern View Model avec Symfony2RomainKuzniak
 
Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2
Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2
Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2Sofien Benrhouma
 

En vedette (20)

Limpiando espero la arquitectura que yo quiero
Limpiando espero la arquitectura que yo quieroLimpiando espero la arquitectura que yo quiero
Limpiando espero la arquitectura que yo quiero
 
Angularjs101-les_SPA:support de la video
Angularjs101-les_SPA:support de la videoAngularjs101-les_SPA:support de la video
Angularjs101-les_SPA:support de la video
 
Cleancode / Tocea / Introduction
Cleancode / Tocea / IntroductionCleancode / Tocea / Introduction
Cleancode / Tocea / Introduction
 
Clean architectures
Clean architecturesClean architectures
Clean architectures
 
DDD in PHP
DDD in PHPDDD in PHP
DDD in PHP
 
Design applicatif avec symfony2
Design applicatif avec symfony2Design applicatif avec symfony2
Design applicatif avec symfony2
 
Visteme con 'Clean Architecture' que tengo prisas
Visteme con 'Clean Architecture' que tengo prisasVisteme con 'Clean Architecture' que tengo prisas
Visteme con 'Clean Architecture' que tengo prisas
 
Méthodologie de gestion des tâches dans les projets web
Méthodologie de gestion des tâches dans les projets webMéthodologie de gestion des tâches dans les projets web
Méthodologie de gestion des tâches dans les projets web
 
Laravel 5 and SOLID
Laravel 5 and SOLIDLaravel 5 and SOLID
Laravel 5 and SOLID
 
Solution de collecte intelligente des déchets (Ecube Labs)
Solution de collecte intelligente des déchets (Ecube Labs)Solution de collecte intelligente des déchets (Ecube Labs)
Solution de collecte intelligente des déchets (Ecube Labs)
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
 
Composer in monolithic repositories
Composer in monolithic repositoriesComposer in monolithic repositories
Composer in monolithic repositories
 
Command Bus To Awesome Town
Command Bus To Awesome TownCommand Bus To Awesome Town
Command Bus To Awesome Town
 
Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)Symfony: Your Next Microframework (SymfonyCon 2015)
Symfony: Your Next Microframework (SymfonyCon 2015)
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Android
 
Clean architecture with ddd layering in php
Clean architecture with ddd layering in phpClean architecture with ddd layering in php
Clean architecture with ddd layering in php
 
Domain Driven Design using Laravel
Domain Driven Design using LaravelDomain Driven Design using Laravel
Domain Driven Design using Laravel
 
Software Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill SparksSoftware Design Patterns in Laravel by Phill Sparks
Software Design Patterns in Laravel by Phill Sparks
 
Le pattern View Model avec Symfony2
Le pattern View Model avec Symfony2Le pattern View Model avec Symfony2
Le pattern View Model avec Symfony2
 
Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2
Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2
Rapport Projet De Fin D'étude Développent d'une application web avec Symfony2
 

Similaire à Clean Code Fundamentals

Clean code - DSC DYPCOE
Clean code - DSC DYPCOEClean code - DSC DYPCOE
Clean code - DSC DYPCOEPatil Shreyas
 
Raya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readabilityRaya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readabilityAbdel Hady Muhammad
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Clean Code - Writing code for human
Clean Code - Writing code for humanClean Code - Writing code for human
Clean Code - Writing code for humanNETKO Solution
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...DevDay.org
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringEyob Lube
 
Code Quality Makes Your Job Easier
Code Quality Makes Your Job EasierCode Quality Makes Your Job Easier
Code Quality Makes Your Job EasierTonya Mork
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential SkillsJohn Choi
 
Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!Diego Pacheco
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style GuidesMosky Liu
 
Code Smells - Refactoring
Code Smells - RefactoringCode Smells - Refactoring
Code Smells - RefactoringShobi P P
 
Clean code lecture part I
Clean code lecture part IClean code lecture part I
Clean code lecture part IJun Shimizu
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 

Similaire à Clean Code Fundamentals (20)

Clean code - DSC DYPCOE
Clean code - DSC DYPCOEClean code - DSC DYPCOE
Clean code - DSC DYPCOE
 
Clean Code
Clean CodeClean Code
Clean Code
 
Raya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readabilityRaya code quality guidelines - enhancing readability
Raya code quality guidelines - enhancing readability
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Clean Code - Writing code for human
Clean Code - Writing code for humanClean Code - Writing code for human
Clean Code - Writing code for human
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
 
Naming Things
Naming ThingsNaming Things
Naming Things
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Code Quality Makes Your Job Easier
Code Quality Makes Your Job EasierCode Quality Makes Your Job Easier
Code Quality Makes Your Job Easier
 
Object Calisthenics in Objective-C
Object Calisthenics in Objective-CObject Calisthenics in Objective-C
Object Calisthenics in Objective-C
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential Skills
 
Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style Guides
 
Clean code and code smells
Clean code and code smellsClean code and code smells
Clean code and code smells
 
Code Smells - Refactoring
Code Smells - RefactoringCode Smells - Refactoring
Code Smells - Refactoring
 
Clean code
Clean codeClean code
Clean code
 
Clean code lecture part I
Clean code lecture part IClean code lecture part I
Clean code lecture part I
 
Clean code
Clean codeClean code
Clean code
 
Clean_Code
Clean_CodeClean_Code
Clean_Code
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 

Dernier

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 

Dernier (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 

Clean Code Fundamentals

  • 1. Clean Code “Master programmers think of systems as stories to be told rather than programs to be written” By -: Uday Pratap Singh
  • 2. Agenda  What is clean code  What makes us write bad code  Talk about meaningful names  How to make functions readable & understandable
  • 3. Clean Code A lot like painting a picture
  • 4. What is clean code  Is it Readable?  Is it Maintainable?  Is it Reusable?  Is it Testable?
  • 5. What is clean code  Should be Readable like a well written book.  Should be crisp. Contain only what is necessary.  Provides one way rather than many ways of doing one thing.  Does one thing well. Each class, function exposes single minded attitude.
  • 6. What is clean code  Easier for other people to enhance it.  Smaller is better.  Contains no duplication  Close to expectations. No surprises
  • 7. What is clean code “You need to write that minimizes the time it would take someone else to understand it – even if that someone else is you – Dustin Boswell and Trevor Foucher”
  • 8. Do you know? Time spent reading versus writing is well over 10:1 if you want your code to be easy to write, make it easy to read
  • 9. Total cost of owning a Mess
  • 10. What makes us write bad code?  Were you trying to go fast?  Tired of working on this program and wanted it to be over.  Looked at the backlog  To clean it up later. “We forget later equals never”
  • 11. Why does good code rots? Requirements change Tight schedule Stupid Managers ...
  • 12. Suppose if patient demands to stop hand washing because it’s taking too much time. Doctor will refuse to do that because he knows the risk involved in it. Fault is Ours. We are Unprofessional.
  • 13. Meaningful Names Choosing meaningful name takes time but saves more than it takes
  • 14. Rules  Name should tell you why it exists, what it does, and how it is used e.g: int elapsedTimeInDays  Make meaningful distinctions. e.g: customerInfo is indistinguishable from customer  Use Pronounceable names. e.g: genymdhms
  • 15. Rules Cont...  No magic String or magic numbers.  Use searchable names e.g; MAX_CLASSES_PER_STUDENT is easily searchable rather than 7.  Pick one word per concept. eg: it’s confusing to have fetch, retrieve, and get as equivalent methods for different classes.
  • 16. Rules Cont...  You also don’t need to prefix member variables. e.g; personName variable in Person class  Class names should have noun or noun phrase names like Customer, Account, AddressParser etc.  Method name should start with the verb. e.g; postPayment, save, delete etc.
  • 17. Rules Cont...  Use problem domain names.  Variable holding collection should be plural. e.g; persons, employees  If a variable is expected to have a default value, then assign it at the time of declaration
  • 18. Rules Cont...  Don’t be cute.  Choose clarity over entertainment value.  Use kill rather than whack.  Say what you mean. Mean what you say.
  • 19. Functions Should be easy to read and understand which communicates its intent
  • 20. Rules  Small should be smaller.  Avoid more the 10 lines. Do not write more than 120 character in one line ( Line should be visible at one glance)  Should be properly indented.  It should not be more than 2.  Blocks in if, else or loops should be only 1 line. Probably that 1 line should be a method call.
  • 21. Rules Cont..  Do one thing.  Should do it well.  Should do it only.  Avoid multiple return statements.  Long descriptive name is better than long comment.  Well defined parameters (with descriptive names) are better than a map as parameter.
  • 22. Rules Cont...  Step Down Rule – Code read from top to bottom.  Function Arguments  More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.  Passing flag argument is a terrible practice. It loudly proclaims that the function does more than one thing.
  • 23. Rules Cont...  Has no side effects.  checkPassword method should only check whether password is correct or not, it should not initialize the user session. If we want to initialize the session then method name should be checkPasswordAndInitializeSession.  Function should either change the state of object or it should return some information about the object. Doing both often leads to confusion.
  • 24. Rules Cont...  Prefer exception to returning error codes.  Extract body of try catch blocks into method to increase the readability and structure of the code.  DRY (Do not Repeat Yourself).
  • 25. “Leave the campground cleaner than you found it”