SlideShare a Scribd company logo
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)

PPTX
Clean Code
PDF
Clean Code
PPTX
Coding standards
PDF
Chapter17 of clean code
PPT
Clean code
PPTX
Coding conventions
PPT
c-coding-standards-and-best-programming-practices.ppt
PPTX
Clean code
PPTX
Clean Code - Writing code for human
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
ODP
Clean Code - Part 2
PDF
Write your Ruby in Style
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PDF
Naming Things (with notes)
PPT
Writing Good Code
PPTX
Quality rails coding
PPTX
Coding standards
PDF
WordCamp US: Clean Code
PDF
Clean Code
PPTX
Best-Practices-for-Writing-Clean-Code.Presentation
Clean Code
Clean Code
Coding standards
Chapter17 of clean code
Clean code
Coding conventions
c-coding-standards-and-best-programming-practices.ppt
Clean code
Clean Code - Writing code for human
Clean Code. An Agile Guide to Software Craft Kameron H.
Clean Code - Part 2
Write your Ruby in Style
Clean Code. An Agile Guide to Software Craft Kameron H.
Naming Things (with notes)
Writing Good Code
Quality rails coding
Coding standards
WordCamp US: Clean Code
Clean Code
Best-Practices-for-Writing-Clean-Code.Presentation
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Construction Project Organization Group 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Welding lecture in detail for understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Sustainable Sites - Green Building Construction
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
additive manufacturing of ss316l using mig welding
Construction Project Organization Group 2.pptx
Internet of Things (IOT) - A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Welding lecture in detail for understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Lesson 3_Tessellation.pptx finite Mathematics
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Sustainable Sites - Green Building Construction
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
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 !