SlideShare a Scribd company logo
1 of 34
Download to read offline
Presented By: Mansi Babbar
Basics of Writing
Clean Code
Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Mute
Please keep your window on mute
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question
Agenda
01 Introduction & Overview
02 Clean Naming Convention
04 Clean Classes
05 Clean Error Handling
06 Clean Unit Tests
05 Clean Comments
07
7
Clean Functions
03
05 Clean Formatting Skills
08
What is clean code?
Clean code
Clean code
● easy to understand the execution flow of the entire application
● easy to understand how the different objects collaborate with each other
● easy to understand the role and responsibility of each class
● easy to understand what each method does
● easy to understand what is the purpose of each expression and variable
● easy to extend and refactor
Clean code
● Classes and methods are small and only have single responsibility
● Classes and methods are predictable and work as expected
● The code is easily testable and has unit tests (or it is easy to write the tests)
● Tests are easy to understand and easy to change
What is the cost of bad
code?
Cost of bad code
Cost of bad code
Cost of bad code
For whom do we write
code ?
● For yourself ?
● For computer ?
● For other programmers ?
How to write clean
code ?
Universal Principles to follow
● Follow SOLID principles
● Follow a Design Pattern if it fits the problem space that you are trying to solve.
● Follow Law of Demeter
● Follow TDD (Test Driven Development)
What to clean ?
What should we clean?
● Naming convention
● Functions
● Classes
● Error Handling
● Tests
● Comments
● Formatting Skills
Naming Convention
Cleaning Naming Convention
● Name should be simple
● Names should reveal intent
● Use pronounceable name
● Use one word per concept
● Use verbs for function names and nouns for classes and objects
Functions
Cleaning Functions
● Functions should be short as possible
● It should follow SRP (Single responsibility principle)
● No side effects
● No nested control structure
● Prefer less number of arguments
● Must only do what the name suggests and nothing else
● Should either do something or answer something, but not both
Classes
Cleaning Classes
● Classes should contain variables and functions that do one thing (SRP)
● The name of a class should describe what responsibilities it fulfills
● Do one thing (SRP) : It is forbidden to create classes that have many
responsibilities
● Variables should be private so that we can change their type or
implementation when required
● Classes should be small
● Classes should provide abstraction
Error Handling
Cleaning Error Handling
● Code should be both clean and robust, it should handle errors with
grace and style.
● Catch everything. Better to be safe than sorry.
● Be informative. Return Exceptions instead of Error Codes. Each
exception thrown should provide enough context to detect the error
root cause and location.
Unit Tests
Cleaning Unit Tests
Clean tests should follow F.I.R.S.T principles:
● Fast: Tests should be fast. They should run quickly.
● Independent: Tests should not depend on each other. One test should not
set up the conditions for the next test.
● Repeatable: Tests should be repeatable in any environment. They should
run in the production environment, the QA environment, and even on your
laptop while riding home on the train without a network.
● Self-Validating: Tests should have a boolean output. Either they pass or
fail.
● Timely: Tests need to be written in a timely fashion.
Comments
Cleaning Comments
● The only truly good comment is the comment you found a way not
to write.
Don’t comment bad code, rewrite it
● Don’t comment bad code, rewrite it
● Explain your intention in comments
● Warn of consequences in comments
● Never leave code commented
Formatting Skills
Cleaning Formatting Skills
● When people look into your code, you want them to be impressed
with the neatness, consistency, orderliness and attention to
detail that they perceive
● You should strive to keep your lines short. You should never have to
scroll to the right.
● A function that is called should be below a function that does the
calling.
● Each line represents an expression or a clause, and each group of
lines represents a complete thought.
Conclusion
Conclusion
We can simple remember these points while writing code...
● KIS : Keep It Simple
● DRY : Don’t Repeat Yourself
● YAGNI : You Aren’t Gonna Need It
● Composition over Inheritance
● Favor Readability
● Practice Consistency
Thank You !!!!

More Related Content

What's hot

Python type hints
Python type hintsPython type hints
Python type hintsAyla Khan
 
Improving code quality
Improving code qualityImproving code quality
Improving code qualityJano Suchal
 
Fundamentals of testing
Fundamentals of testingFundamentals of testing
Fundamentals of testingMadi Myrzabek
 
Effective Code Review
Effective Code ReviewEffective Code Review
Effective Code ReviewEyal Kenig
 
Test-Driven Development with Plone
Test-Driven Development with PloneTest-Driven Development with Plone
Test-Driven Development with PloneTimo Stollenwerk
 
Tdd - Test Driven Development
Tdd - Test Driven DevelopmentTdd - Test Driven Development
Tdd - Test Driven DevelopmentDavid Paluy
 
Code Smells - Refactoring
Code Smells - RefactoringCode Smells - Refactoring
Code Smells - RefactoringShobi P P
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Peter Kofler
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftAmey Tavkar
 
Java final keyword
Java final keywordJava final keyword
Java final keywordSujit Kumar
 
Practical pointers for better code review
Practical pointers for better code reviewPractical pointers for better code review
Practical pointers for better code reviewkhink
 
Math-Bridge Author AdvdEx
Math-Bridge Author AdvdExMath-Bridge Author AdvdEx
Math-Bridge Author AdvdExmetamath
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentdcsunu
 
Better tests automagically (big sky dev con 2015)
Better tests automagically (big sky dev con 2015)Better tests automagically (big sky dev con 2015)
Better tests automagically (big sky dev con 2015)roblund
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsHarshith Shetty
 
Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaSoftware Guru
 

What's hot (20)

Tdd red-green-refactor
Tdd red-green-refactorTdd red-green-refactor
Tdd red-green-refactor
 
Python type hints
Python type hintsPython type hints
Python type hints
 
Improving code quality
Improving code qualityImproving code quality
Improving code quality
 
Fundamentals of testing
Fundamentals of testingFundamentals of testing
Fundamentals of testing
 
Effective Code Review
Effective Code ReviewEffective Code Review
Effective Code Review
 
Test-Driven Development with Plone
Test-Driven Development with PloneTest-Driven Development with Plone
Test-Driven Development with Plone
 
Tdd - Test Driven Development
Tdd - Test Driven DevelopmentTdd - Test Driven Development
Tdd - Test Driven Development
 
Code Smells - Refactoring
Code Smells - RefactoringCode Smells - Refactoring
Code Smells - Refactoring
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)
 
Tdd in swift
Tdd in swiftTdd in swift
Tdd in swift
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in Swift
 
Java final keyword
Java final keywordJava final keyword
Java final keyword
 
Practical pointers for better code review
Practical pointers for better code reviewPractical pointers for better code review
Practical pointers for better code review
 
Math-Bridge Author AdvdEx
Math-Bridge Author AdvdExMath-Bridge Author AdvdEx
Math-Bridge Author AdvdEx
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Better tests automagically (big sky dev con 2015)
Better tests automagically (big sky dev con 2015)Better tests automagically (big sky dev con 2015)
Better tests automagically (big sky dev con 2015)
 
TDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not ImplementationsTDD - Designing with Expectations, not Implementations
TDD - Designing with Expectations, not Implementations
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y Gomega
 
Final keyword
Final keywordFinal keyword
Final keyword
 

Similar to Basics of Clean Code

Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
Writing Tests Effectively
Writing Tests EffectivelyWriting Tests Effectively
Writing Tests EffectivelyPaul Boocock
 
Software Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeySoftware Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeyCefalo
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Abhijeet Vaikar
 
Pragmatic software testing education - SIGCSE 2019
Pragmatic software testing education - SIGCSE 2019Pragmatic software testing education - SIGCSE 2019
Pragmatic software testing education - SIGCSE 2019Maurício Aniche
 
Basic software engineering principles with code examples - Session 2
Basic software engineering principles with code examples - Session 2Basic software engineering principles with code examples - Session 2
Basic software engineering principles with code examples - Session 2LahiruWijewardana1
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD introVitaliy Kulikov
 
How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)Asier Barrenetxea
 
Basic software engineering principles - Session 1
Basic software engineering principles - Session 1Basic software engineering principles - Session 1
Basic software engineering principles - Session 1LahiruWijewardana1
 
РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021
РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021
РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021GoQA
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentSamnang Chhun
 
Writing Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designsWriting Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designslachlandonald
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCefalo
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 

Similar to Basics of Clean Code (20)

Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2
 
Clean code
Clean codeClean code
Clean code
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Writing Tests Effectively
Writing Tests EffectivelyWriting Tests Effectively
Writing Tests Effectively
 
Software Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit DeySoftware Design Principles and Best Practices - Satyajit Dey
Software Design Principles and Best Practices - Satyajit Dey
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
Pragmatic software testing education - SIGCSE 2019
Pragmatic software testing education - SIGCSE 2019Pragmatic software testing education - SIGCSE 2019
Pragmatic software testing education - SIGCSE 2019
 
Basic software engineering principles with code examples - Session 2
Basic software engineering principles with code examples - Session 2Basic software engineering principles with code examples - Session 2
Basic software engineering principles with code examples - Session 2
 
Xtreme Programming
Xtreme ProgrammingXtreme Programming
Xtreme Programming
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD intro
 
How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)
 
Basic software engineering principles - Session 1
Basic software engineering principles - Session 1Basic software engineering principles - Session 1
Basic software engineering principles - Session 1
 
TDD
TDDTDD
TDD
 
РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021
РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021
РОМАН ЯКИМЧУК «Оптимізація QA процесів» Kyiv QADay 2021
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Writing Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designsWriting Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designs
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Best pratice
Best praticeBest pratice
Best pratice
 

More from Knoldus Inc.

GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfKnoldus Inc.
 
NuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxNuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxKnoldus Inc.
 
Data Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable TestingData Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable TestingKnoldus Inc.
 
K8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesK8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesKnoldus Inc.
 
Introduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxIntroduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxKnoldus Inc.
 
Robusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxRobusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxKnoldus Inc.
 
Optimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxOptimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxKnoldus Inc.
 
Azure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxAzure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxKnoldus Inc.
 
CQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxCQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxKnoldus Inc.
 
ETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationKnoldus Inc.
 
Scripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics PresentationScripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics PresentationKnoldus Inc.
 
Getting started with dotnet core Web APIs
Getting started with dotnet core Web APIsGetting started with dotnet core Web APIs
Getting started with dotnet core Web APIsKnoldus Inc.
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II PresentationKnoldus Inc.
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Configuring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAConfiguring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAKnoldus Inc.
 
Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)Knoldus Inc.
 
Azure Databricks (For Data Analytics).pptx
Azure Databricks (For Data Analytics).pptxAzure Databricks (For Data Analytics).pptx
Azure Databricks (For Data Analytics).pptxKnoldus Inc.
 
The Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and KotlinThe Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and KotlinKnoldus Inc.
 
Data Engineering with Databricks Presentation
Data Engineering with Databricks PresentationData Engineering with Databricks Presentation
Data Engineering with Databricks PresentationKnoldus Inc.
 
Databricks for MLOps Presentation (AI/ML)
Databricks for MLOps Presentation (AI/ML)Databricks for MLOps Presentation (AI/ML)
Databricks for MLOps Presentation (AI/ML)Knoldus Inc.
 

More from Knoldus Inc. (20)

GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdf
 
NuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxNuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptx
 
Data Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable TestingData Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable Testing
 
K8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesK8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose Kubernetes
 
Introduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxIntroduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptx
 
Robusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxRobusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptx
 
Optimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxOptimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptx
 
Azure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxAzure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptx
 
CQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxCQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptx
 
ETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake Presentation
 
Scripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics PresentationScripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics Presentation
 
Getting started with dotnet core Web APIs
Getting started with dotnet core Web APIsGetting started with dotnet core Web APIs
Getting started with dotnet core Web APIs
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Configuring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAConfiguring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRA
 
Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)
 
Azure Databricks (For Data Analytics).pptx
Azure Databricks (For Data Analytics).pptxAzure Databricks (For Data Analytics).pptx
Azure Databricks (For Data Analytics).pptx
 
The Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and KotlinThe Power of Dependency Injection with Dagger 2 and Kotlin
The Power of Dependency Injection with Dagger 2 and Kotlin
 
Data Engineering with Databricks Presentation
Data Engineering with Databricks PresentationData Engineering with Databricks Presentation
Data Engineering with Databricks Presentation
 
Databricks for MLOps Presentation (AI/ML)
Databricks for MLOps Presentation (AI/ML)Databricks for MLOps Presentation (AI/ML)
Databricks for MLOps Presentation (AI/ML)
 

Recently uploaded

Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

Basics of Clean Code

  • 1. Presented By: Mansi Babbar Basics of Writing Clean Code
  • 2. Lack of etiquette and manners is a huge turn off. KnolX Etiquettes Punctuality Respect Knolx session timings, you are requested not to join sessions after a 5 minutes threshold post the session start time. Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Mute Please keep your window on mute Avoid Disturbance Avoid leaving your window unmuted after asking a question
  • 3. Agenda 01 Introduction & Overview 02 Clean Naming Convention 04 Clean Classes 05 Clean Error Handling 06 Clean Unit Tests 05 Clean Comments 07 7 Clean Functions 03 05 Clean Formatting Skills 08
  • 6. Clean code ● easy to understand the execution flow of the entire application ● easy to understand how the different objects collaborate with each other ● easy to understand the role and responsibility of each class ● easy to understand what each method does ● easy to understand what is the purpose of each expression and variable ● easy to extend and refactor
  • 7. Clean code ● Classes and methods are small and only have single responsibility ● Classes and methods are predictable and work as expected ● The code is easily testable and has unit tests (or it is easy to write the tests) ● Tests are easy to understand and easy to change
  • 8. What is the cost of bad code?
  • 9. Cost of bad code
  • 10. Cost of bad code
  • 11. Cost of bad code
  • 12. For whom do we write code ?
  • 13. ● For yourself ? ● For computer ? ● For other programmers ?
  • 14. How to write clean code ?
  • 15. Universal Principles to follow ● Follow SOLID principles ● Follow a Design Pattern if it fits the problem space that you are trying to solve. ● Follow Law of Demeter ● Follow TDD (Test Driven Development)
  • 17. What should we clean? ● Naming convention ● Functions ● Classes ● Error Handling ● Tests ● Comments ● Formatting Skills
  • 19. Cleaning Naming Convention ● Name should be simple ● Names should reveal intent ● Use pronounceable name ● Use one word per concept ● Use verbs for function names and nouns for classes and objects
  • 21. Cleaning Functions ● Functions should be short as possible ● It should follow SRP (Single responsibility principle) ● No side effects ● No nested control structure ● Prefer less number of arguments ● Must only do what the name suggests and nothing else ● Should either do something or answer something, but not both
  • 23. Cleaning Classes ● Classes should contain variables and functions that do one thing (SRP) ● The name of a class should describe what responsibilities it fulfills ● Do one thing (SRP) : It is forbidden to create classes that have many responsibilities ● Variables should be private so that we can change their type or implementation when required ● Classes should be small ● Classes should provide abstraction
  • 25. Cleaning Error Handling ● Code should be both clean and robust, it should handle errors with grace and style. ● Catch everything. Better to be safe than sorry. ● Be informative. Return Exceptions instead of Error Codes. Each exception thrown should provide enough context to detect the error root cause and location.
  • 27. Cleaning Unit Tests Clean tests should follow F.I.R.S.T principles: ● Fast: Tests should be fast. They should run quickly. ● Independent: Tests should not depend on each other. One test should not set up the conditions for the next test. ● Repeatable: Tests should be repeatable in any environment. They should run in the production environment, the QA environment, and even on your laptop while riding home on the train without a network. ● Self-Validating: Tests should have a boolean output. Either they pass or fail. ● Timely: Tests need to be written in a timely fashion.
  • 29. Cleaning Comments ● The only truly good comment is the comment you found a way not to write. Don’t comment bad code, rewrite it ● Don’t comment bad code, rewrite it ● Explain your intention in comments ● Warn of consequences in comments ● Never leave code commented
  • 31. Cleaning Formatting Skills ● When people look into your code, you want them to be impressed with the neatness, consistency, orderliness and attention to detail that they perceive ● You should strive to keep your lines short. You should never have to scroll to the right. ● A function that is called should be below a function that does the calling. ● Each line represents an expression or a clause, and each group of lines represents a complete thought.
  • 33. Conclusion We can simple remember these points while writing code... ● KIS : Keep It Simple ● DRY : Don’t Repeat Yourself ● YAGNI : You Aren’t Gonna Need It ● Composition over Inheritance ● Favor Readability ● Practice Consistency