SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
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 !

Weitere ähnliche Inhalte

Ähnlich wie Extracts from "Clean Code"

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 & StructureNick Pruehs
 
Robot Framework Dos And Don'ts
Robot Framework Dos And Don'tsRobot Framework Dos And Don'ts
Robot Framework Dos And Don'tsPekka Klärck
 
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 WorldDavid McCarter
 
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 WorldDavid McCarter
 
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 developmentKetan Raval
 
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.pptVinayakHospet1
 
Unit testing
Unit testingUnit testing
Unit testingAdam Birr
 
GTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVAGTU Guidelines for Project on JAVA
GTU Guidelines for Project on JAVATOPS Technologies
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"Kazuhiro Sera
 
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
 
Unit Testing
Unit TestingUnit Testing
Unit TestingAdam Birr
 
Game Programming 04 - Style & Design Principles
Game Programming 04 - Style & Design PrinciplesGame Programming 04 - Style & Design Principles
Game Programming 04 - Style & Design PrinciplesNick Pruehs
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applicationsChandra Sekhar Saripaka
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 

Kürzlich hochgeladen (20)

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 

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 !