SlideShare uma empresa Scribd logo
1 de 11
Baixar para ler offline
Extracts from “Clean code”
Vlatka Pavišić
Naming
❏ Choose descriptive, pronounceable and
searchable names
❏ Classes should have a noun (phrase) name like
Customer, AddressParser
❏ Avoid generic names like Data, Info, Processor
❏ Pick one word for the concept
❏ Examples of bad practices: using both start_at and
starts_at at different tables; get, fetch and retrieve for the
same action
❏ Use long names for long scopes; loop variables
can have short names
Methods
❏ They should be small
❏ The number of arguments should be minimized
❏ By creating an object for the arguments
❏ By using instance variables
❏ Blocks within if-else should be 1 line long
❏ Have 1 level of abstraction per method
❏ Methods should descend by the level of abstraction
❏ They should have descriptive names even if they’re long
❏ Avoid boolean arguments. Instead, split the function in two
❏ A method should have no hidden side-effects (like a method check_password that also
initializes a session)
Classes
❏ Classes should be small, not in the terms of the number of lines but the number of
responsibilities
❏ Each small class encapsulates a single responsibility, has a single reason to
change, and collaborates with a few others to achieve the desired system
behaviours
❏ For example: Event, EventPresenter, EventsController, EventPolicy...
Comments
❏ Good code is like a good joke: it needs no explanation
❏ Instead of writing comments, try to express yourself with code
❏ It’s better to put in the effort to write expressive code than to update the comments
when the code changes
❏ Exception: documentation tool (like YARD) - all public methods, classes, modules
should have comments for documentation purposes
Formatting
❏ Separate different concepts and sections (in Rails’ models: associations, validations,
scopes…) with blank lines
❏ The caller method should be above the callee and as close as possible
❏ Conceptual affinity - methods that do similar things should be close
❏ Vertical ordering - most important functions should come first
❏ Don’t do horizontal alignment (like in FactoryBot factories) because it tempts you to
read it column by column
❏ Team should agree upon rules so that the project code is consistent
Error handling
❏ Provide context with exceptions; use a descriptive class name and a message
Tests
❏ Try to have one assert per test or at least to minimize their number
❏ Use a test coverage tool (for Ruby, simplecov)
❏ Test boundary conditions
❏ Tests should be fast
Code smells
❏ Obsolete, redundant or poorly written comments
❏ Commented-out code
❏ Methods with too many arguments
❏ Multiple programming languages in one file
❏ Duplication
❏ Dead code - code that’s never executed
❏ Inconsistency - doing similar things in a different way
Heuristics
❏ Simplify complex expressions
❏ With explanatory variables
❏ With encapsulating conditionals - like should_be_deleted
❏ Prefer polymorphism to if-else and switch-case
❏ Follow style guides and standard conventions
❏ https://github.com/bbatsov/ruby-style-guide
❏ https://github.com/bbatsov/rails-style-guide
❏ http://www.betterspecs.org
❏ Use constants for “magic numbers”
❏ Avoid negative conditionals (like !event.deadline_not_passed?)
Merci pour votre attention !

Mais conteúdo relacionado

Semelhante a Extracts from "Clean Code"

c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
VinayakHospet1
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Adam Birr
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
Chandra Sekhar Saripaka
 

Semelhante a Extracts from "Clean Code" (20)

Style & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureStyle & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & Structure
 
Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'ts
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
The right way coding for ios app development
The right way coding for ios app developmentThe right way coding for ios app development
The right way coding for ios app development
 
Clean code
Clean code Clean code
Clean code
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
 
Unit testing
Unit testingUnit testing
Unit testing
 
Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
GTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVAGTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVA
 
Codings Standards
Codings StandardsCodings Standards
Codings Standards
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
Coding standards php
Coding standards phpCoding standards php
Coding standards php
 
Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!Naming Things Book : Simple Book Review!
Naming Things Book : Simple Book Review!
 
Code Metrics
Code MetricsCode Metrics
Code Metrics
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Refactoring
RefactoringRefactoring
Refactoring
 
Game Programming 04 - Style & Design Principles
Game Programming 04 - Style & Design PrinciplesGame Programming 04 - Style & Design Principles
Game Programming 04 - Style & Design Principles
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Extracts from "Clean Code"

  • 1. Extracts from “Clean code” Vlatka Pavišić
  • 2. Naming ❏ Choose descriptive, pronounceable and searchable names ❏ Classes should have a noun (phrase) name like Customer, AddressParser ❏ Avoid generic names like Data, Info, Processor ❏ Pick one word for the concept ❏ Examples of bad practices: using both start_at and starts_at at different tables; get, fetch and retrieve for the same action ❏ Use long names for long scopes; loop variables can have short names
  • 3. Methods ❏ They should be small ❏ The number of arguments should be minimized ❏ By creating an object for the arguments ❏ By using instance variables ❏ Blocks within if-else should be 1 line long ❏ Have 1 level of abstraction per method ❏ Methods should descend by the level of abstraction ❏ They should have descriptive names even if they’re long ❏ Avoid boolean arguments. Instead, split the function in two ❏ A method should have no hidden side-effects (like a method check_password that also initializes a session)
  • 4. Classes ❏ Classes should be small, not in the terms of the number of lines but the number of responsibilities ❏ Each small class encapsulates a single responsibility, has a single reason to change, and collaborates with a few others to achieve the desired system behaviours ❏ For example: Event, EventPresenter, EventsController, EventPolicy...
  • 5. Comments ❏ Good code is like a good joke: it needs no explanation ❏ Instead of writing comments, try to express yourself with code ❏ It’s better to put in the effort to write expressive code than to update the comments when the code changes ❏ Exception: documentation tool (like YARD) - all public methods, classes, modules should have comments for documentation purposes
  • 6. Formatting ❏ Separate different concepts and sections (in Rails’ models: associations, validations, scopes…) with blank lines ❏ The caller method should be above the callee and as close as possible ❏ Conceptual affinity - methods that do similar things should be close ❏ Vertical ordering - most important functions should come first ❏ Don’t do horizontal alignment (like in FactoryBot factories) because it tempts you to read it column by column ❏ Team should agree upon rules so that the project code is consistent
  • 7. Error handling ❏ Provide context with exceptions; use a descriptive class name and a message
  • 8. Tests ❏ Try to have one assert per test or at least to minimize their number ❏ Use a test coverage tool (for Ruby, simplecov) ❏ Test boundary conditions ❏ Tests should be fast
  • 9. Code smells ❏ Obsolete, redundant or poorly written comments ❏ Commented-out code ❏ Methods with too many arguments ❏ Multiple programming languages in one file ❏ Duplication ❏ Dead code - code that’s never executed ❏ Inconsistency - doing similar things in a different way
  • 10. Heuristics ❏ Simplify complex expressions ❏ With explanatory variables ❏ With encapsulating conditionals - like should_be_deleted ❏ Prefer polymorphism to if-else and switch-case ❏ Follow style guides and standard conventions ❏ https://github.com/bbatsov/ruby-style-guide ❏ https://github.com/bbatsov/rails-style-guide ❏ http://www.betterspecs.org ❏ Use constants for “magic numbers” ❏ Avoid negative conditionals (like !event.deadline_not_passed?)
  • 11. Merci pour votre attention !