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 !

More Related Content

PDF
Extracts from "Clean code"
PPTX
Code reviews
PPTX
Oopsinphp
PDF
Advanced PHP Simplified
PPTX
Writing Clean Code (Recommendations by Robert Martin)
PDF
Quality in coding (phpmd & phpcpd with Symfony 2)
PDF
Contest Tips and Tricks
PPTX
C# Generics
Extracts from "Clean code"
Code reviews
Oopsinphp
Advanced PHP Simplified
Writing Clean Code (Recommendations by Robert Martin)
Quality in coding (phpmd & phpcpd with Symfony 2)
Contest Tips and Tricks
C# Generics

Similar to Extracts from "Clean Code" (20)

PDF
Style & Design Principles 01 - Code Style & Structure
PDF
Robot Framework Dos And Don'ts
PPTX
Back-2-Basics: .NET Coding Standards For The Real World
PPTX
Back-2-Basics: .NET Coding Standards For The Real World
PPTX
The right way coding for ios app development
PDF
Clean code
PPT
c-coding-standards-and-best-programming-practices.ppt
PPTX
Unit testing
PPTX
Coding conventions
PPT
GTU Guidelines for Project on JAVA
PPT
Codings Standards
KEY
Learning from "Effective Scala"
DOC
Coding standards php
PDF
Naming Things Book : Simple Book Review!
PPTX
Code Metrics
PPTX
Unit Testing
PDF
PDF
Game Programming 04 - Style & Design Principles
PPTX
Lecture-3.pptx and faculty. His research interests include RF sensing,
PDF
Software Craftmanship - Cours Polytech
Style & Design Principles 01 - Code Style & Structure
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 World
The right way coding for ios app development
Clean code
c-coding-standards-and-best-programming-practices.ppt
Unit testing
Coding conventions
GTU Guidelines for Project on JAVA
Codings Standards
Learning from "Effective Scala"
Coding standards php
Naming Things Book : Simple Book Review!
Code Metrics
Unit Testing
Game Programming 04 - Style & Design Principles
Lecture-3.pptx and faculty. His research interests include RF sensing,
Software Craftmanship - Cours Polytech
Ad

Recently uploaded (20)

PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
First part_B-Image Processing - 1 of 2).pdf
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
Principal presentation for NAAC (1).pptx
PPTX
Measurement Uncertainty and Measurement System analysis
PDF
Design Guidelines and solutions for Plastics parts
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
Module 8- Technological and Communication Skills.pptx
PPTX
CyberSecurity Mobile and Wireless Devices
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Java Basics-Introduction and program control
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
wireless networks, mobile computing.pptx
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
First part_B-Image Processing - 1 of 2).pdf
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Principal presentation for NAAC (1).pptx
Measurement Uncertainty and Measurement System analysis
Design Guidelines and solutions for Plastics parts
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Soil Improvement Techniques Note - Rabbi
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Abrasive, erosive and cavitation wear.pdf
Module 8- Technological and Communication Skills.pptx
CyberSecurity Mobile and Wireless Devices
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Java Basics-Introduction and program control
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Information Storage and Retrieval Techniques Unit III
wireless networks, mobile computing.pptx
Ad

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 !