SlideShare une entreprise Scribd logo
1  sur  133
Quality Ruby Code
Michael Durrant
m2@snap2web.com
June 2019
Thanks to all the bostonrb organizers!
Thanks to all the bostonrb organizers!
Thank you EzCater, a good sponsor makes a huge difference!
Thanks to all the bostonrb organizers!
Thank you EzCater, a good sponsor makes a huge difference!
Thank you to all my friends in Indianapolis for teaching me so
much! Thank you Stacey, Aldrin, Ben, Bella, Joe, Adam,
Adrian, Orando!
Thanks to all the bostonrb organizers!
Thank you EzCater, a good sponsor makes a huge difference!
Thank you to all my friends in Indianapolis for teaching me so
much! Thank you Stacey, Aldrin, Ben, Bella, Joe, Adam,
Adrian, Orando!
Thank YOU for coming tonight and supporting the community!
My talks are intended to be interactive so
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
- Hearing your opinions and experiences is very welcome
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
- Hearing your opinions and experiences is very welcome
- Prefer questions, answers, conversations during not after
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
- Hearing your opinions and experiences is very welcome
- Prefer questions, answers, conversations during not after
- Expect polls and use them to learn about your community
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
- Hearing your opinions and experiences is very welcome
- Prefer questions, answers, conversations during not after
- Expect polls and use them to learn about your community
- Cell phone and laptop use during main talk is discouraged
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
- Hearing your opinions and experiences is very welcome
- Prefer questions, answers, conversations during not after
- Expect polls and use them to learn about your community
- Cell phone and laptop use during main talk is discouraged
- Cell phone airplane mode is encouraged
My talks are intended to be interactive so
- Ask questions (wave hand, call out ‘Michael’)
- Hearing your opinions and experiences is very welcome
- Prefer questions, answers, conversations during not after
- Expect polls and use them to learn about your community
- Cell phone and laptop use during main talk is discouraged
- Cell phone airplane mode is encouraged
- Public Video is being recorded for most of my talks, !smile
16
But first… Docker...
17
But first… Docker...
git clone --recursive https://github.com/bretfisher/container.training
cd ~/container.training/stacks
docker-compose up
docker-compose down
docker node ls # (err)docker
docker swarm init # (ONE node only!) Err? next line
docker swarm init --advertise-addr eth0
docker swarm join 
--token SWMTKN-1-59fl4ak4nqjmao1ofttrc4eprhrola2l87... 
172.31.4.182:2377
Docker node ls
See https://dockercon19.bretfisher.com/#1
18
And… about deploying code...
19
And… about deploying code...
20
And… about deploying code...
21
And… about deploying code...
22
And… about deploying code...
23
And… about deploying code...
24
And… about deploying code...
25
And… about deploying code...
26
By Icon...
27
And… about deploying code...
28
And… about deploying code...
- IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
29
And… about deploying code...
- IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
- Programming configuration via a DSL (e.g. Chef count attribute)
30
And… about deploying code...
- IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
- Programming configuration via a DSL (e.g. Chef count attribute)
- Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ)
31
And… about deploying code...
- IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
- Programming configuration via a DSL (e.g. Chef count attribute)
- Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ)
- Set the State, let the tool manage it
32
And… about deploying code...
- IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
- Programming configuration via a DSL (e.g. Chef count attribute)
- Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ)
- Set the State, let the tool manage it
- Know the key factors – docker has a daemon, ansible doesn’t and uses ssh
(port 22) to do things, etc.
33
And… about deploying code...
- IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
- Programming configuration via a DSL (e.g. Chef count attribute)
- Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ)
- Set the State, let the tool manage it
- Know the key factors – docker has a daemon, ansible doesn’t and uses ssh
(port 22) to do things, etc.
- Use checklists (for developing IAS is a very good idea)
34
And… about deploying code...
Why we use Terraform and not Chef, Puppet, Ansible, SaltStack, or CloudFormation
https://blog.gruntwork.io/why-we-use-terraform-and-not-chef-puppet-ansible-saltstack-or-cloudformation-
7989dad2865c
Infrastructure as Code: Chef, Ansible, Puppet, or Terraform?
https://www.ibm.com/cloud/blog/chef-ansible-puppet-terraform
Puppet vs. Chef vs. Ansible vs. SaltStack
https://www.intigua.com/blog/puppet-vs.-chef-vs.-ansible-vs.-saltstack
Comparing top DevOps tools: Docker vs Kubernetes vs Puppet vs Chef vs Ansible
https://www.znetlive.com/blog/compare-top-devops-tools-docker-kubernetes-puppet-chef-ansible/
Choosing a deployment tool - ansible vs puppet vs chef vs salt
https://gist.github.com/jaceklaskowski/bd3d06489ec004af6ed9
Puppet vs Salt vs Chef vs Ansible, which to choose?
https://www.reddit.com/r/linuxadmin/comments/9ljveg/puppet_vs_salt_vs_chef_vs_ansible_which_to_choose/
Quality Ruby Code
36
As a Quality Engineer..
● I’m always thinking about more tests
37
As a Quality Engineer..
● I’m always thinking about more tests better tests
38
As a Quality Engineer..
● I’m always thinking about more tests better tests
so we can have less bugs
39
As a Quality Engineer..
● I’m always thinking about more tests better tests
so we can have less bugs higher quality
40
As a Quality Engineer..
● I’m always thinking about more tests better tests
so we can have less bugs higher quality
● Or we can end up solving the wrong problems !
41
We have a codebase with bugs...
42
So we add tests...
43
Now we have two codebases for bugs
44
Now we have two codebases for bugs
And our overall code may now be
45
Now we have two codebases for bugs
And our overall code may now be
● Twice as hard to maintain
46
Now we have two codebases for bugs
And our overall code may now be
● Twice as hard to maintain
● Twice as hard to fix
47
Now we have two codebases for bugs
And our overall code may now be
● Twice as hard to maintain
● Twice as hard to fix
● Twice as hard to change
48
hmmm
49
Tests can reveal issues with Quality
However just having them does not
improve application quality
50
We need to address quality issues
in the code itself (both automation
and application code)
51
So...
52
We need (in all code)
● Quality Code Approaches (todays topic)
● KPIs ! Not # tests/bugs, but code level
● Automated Linting, e.g. Rubocop
● Code Grading, Code Climate
● Robust exception handling (also covered today)
● Open minded Code Review mentality
● Easy to use Code Review tools
● IAC
53
● Has Tests
● Is molded
● Good names
● Low wtf Ratio
● Handles Exceptions
● KISS Mr Meta gently
● Uses few parameters
● Uses Linting and Grading
● Allows a Little Duplication
Quality (Ruby) Code
● Is a choice
● Is easy to read
● Is not optimized
● Is easy to change
● Uses standards that evolve
● Uses the right rules guidelines
● Has small classes and methods
● Values comments as they are so rare
● Has less bugs
54
Is Molded
Through a healthy code review process that features:
- Code is owned by all
- PR Comments are the norm
- Quality is not generally time-boxed
- Humbleness leads with pride in code not ego
- Refactoring is nearly always done in-cycle. Now
55
Uses Good Names
Bad
i
j
k
n
flag
type
max_limit
final_size
num_items
all_items
last_entry
Better
index
auto_payment_on_flag
payment_method_type
max_dollar_amount
final_room_sq_ft
number_current_order_items
all_current_order_items
last_order_entry
last_payment_received_date
payment_final_due_date
student_time_at_last_address
56
Low wtf Ratio
u
57
Handles Exceptions
# Not so good
begin
...
some code
...
rescue
end
58
Handles Exceptions
# Better (scoped)
begin
...
some code
...
Rescue StandardError
end
59
Handles Exceptions
# Even better (scoped with re-raise)
begin
...
some code
...
rescue StandardError => e
some code
If condition still an issue can do `raise e`
end
Rescue the most specific exception possible
60
Handles Exceptions
# Even Even better (scoped more with re-raise)
begin
...
some code
...
rescue StandardError::ArgumentError, ZeroDivisionError => e
some code
If condition still an issue can do `raise e`
end
Rescue the most specific exception(s) possible
61
Handles Exceptions
# Sometimes (code with dependencies) you just want to retry
Example1: An API error for get prices which can be retried:
62
def get_api_prices
tries = 0
begin
tries += 1
prices = api_get_buy_sell
sleep 2
rescue SocketError => e
p e.message
if (tries < 5)
p "but retrying... try: #{tries}"
sleep(2 ** tries)
retry
else
p 'giving up...'
raise e # Do not remove, we need a current price to be set
end
end
end
Handles Exceptions – Retry
Example 1
63
visit_with_retry sell_url
def visit_with_retry(url, tries = 5)
begin
@s.visit(url)
sleep 2
find_link(‘login’)
rescue Capybara::ElementNotFound, Net::ReadTimeout => e
p e.message
if (tries < 1)
p "but retrying... try: #{tries}"
sleep(2 ** tries)
tries -= 1
retry
else
p 'giving up...'
raise e # Do not remove, we need a current price to be set
end
end
end
Handles Exceptions – Retry
Example 2
64
Handles Exceptions – Raise them well
65
raise “just the text and not the best”
Handles Exceptions – Raise them well
66
raise “just the text and not the best”
raise RuntimeError, “stop executing please”
Handles Exceptions – Raise them well
67
raise “just the text and not the best”
raise RuntimeError, “stop executing please”
Create custom errors (“help your fellow / future programmer):
class PaymentError < StandardError
end
raise PaymentError, “zero amount in payment field pmt-001”
Handles Exceptions – Raise them well
68
raise “just the text and not the best”
raise RuntimeError, “stop executing please”
Create custom errors (“help your fellow / future programmer):
class PaymentError < StandardError
end
raise PaymentError, “zero amount in payment field pmt-001”
Pass information to exceptions
raise TemperatureError.new(180)
Then add attribute and set in initialize
Handles Exceptions – Raise them well
69
Handles Exceptions – method missing
70
- Saves the day
Handles Exceptions – method missing
71
- Saves the day
- Lets you code carelessly
Handles Exceptions – method missing
72
- Saves the day
- Lets you code carelessly
- Makes everything feel better
Handles Exceptions – method missing
73
- Saves the day
- Lets you code carelessly
- Makes everything feel better
- Is a swiss Army knife that can tear your code apart
Handles Exceptions – method missing
74
- Saves the day
- Lets you code carelessly
- Makes everything feel better
- Is a swiss Army knife that can tear your code apart
- Can lead to highly opinionated programmer talking at you
Handles Exceptions – method missing
75
Handles Exceptions – Learn More...
76
Handles Exceptions – avoids them
77
Handles Exceptions – avoids them
78
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
79
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
80
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
81
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
82
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
83
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
84
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
- Manages Resources with Blocks and ensure* (db connections, file locks, etc)
85
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
- Manages Resources with Blocks and ensure* (db connections, file locks, etc)
- Prefer throw-catch (last value) to raise-rescue (pass in exception)
86
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
- Manages Resources with Blocks and ensure* (db connections, file locks, etc)
- Prefer throw-catch (last value) to raise-rescue (pass in exception)
- Prefer define_method to method_missing
87
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
- Manages Resources with Blocks and ensure* (db connections, file locks, etc)
- Prefer throw-catch (last value) to raise-rescue (pass in exception)
- Prefer define_method to method_missing
- Simulate determinism with Mock Objects, e.g. make http fail
88
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
- Manages Resources with Blocks and ensure* (db connections, file locks, etc)
- Prefer throw-catch (last value) to raise-rescue (pass in exception)
- Prefer define_method to method_missing
- Simulate determinism with Mock Objects, e.g. make http fail
- Memoize ||= expensive operations
89
Handles Exceptions – avoids them
- Treat all objects as if they could be nil
- Pays attention to warnings
- Prefer Set and Struct to Array and Hash for some structured data
- unique data (Set)
- attribute names (getters, setters)
- instance and class methods
- Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
- Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
- Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
- Manages Resources with Blocks and ensure* (db connections, file locks, etc)
- Prefer throw-catch (last value) to raise-rescue (pass in exception)
- Prefer define_method to method_missing
- Simulate determinism with Mock Objects, e.g. make http fail
- Memoize ||= expensive operations - prefer local over instance
90
KISS Mr Meta gently
Metaprogramming is the
answer to all programming
problems !!!
91
Use few parameters
92
Use few parameters
1 is ok.
93
Use few parameters
1 is ok.
2 is a lot
94
Use few parameters
1 is ok.
2 is a lot
0 is best
95
Use few parameters
1 is ok.
2 is a lot
0 is best
However don’t play the “object
trick” without thinking it through
96
Linting and Grading
97
Linting and Grading
duh
98
Allow a little duplication
99
Allow a little duplication
“A little duplication is better
than the wrong abstraction”
100
Allow a little duplication
Avoid extremes
101
Allow a little duplication
Avoid extremes
102
Allow a little duplication
Avoid extremes
103
Is easy to read
104
Is easy to read
Use english
105
Is not optimized
106
Is not optimized
Very little code in 2019 needs
to be highly performant.
Usually not in anticipation of
performance issues
107
Uses standards that evolve (!)
108
Uses standards that evolve (!)
Over longer time periods new
structures and needs will
become apparent and should
be discussed
109
Uses the right rules guidelines
110
Uses the right rules guidelines
Quality code is code that strikes
a balance between the rules, not
just code that follows all the rules
111
Has small classes and methods
112
Has small classes and methods
SRP
113
Has small classes and methods
SRP
Sandy Metz
114
Has small classes and methods
SRP
Sandy Metz
Duh
115
Value comments - they are so rare
116
Value comments - they are so rare
Exceptions to avoiding comments:
117
Value comments - they are so rare
Exceptions to avoiding comments:
# Module Support to expire 12/1/2019
118
Value comments - they are so rare
Exceptions to avoiding comments:
# Module Support to expire 12/1/2019
# Dependent library randomly fails here, cause unknown
119
Value comments - they are so rare
Exceptions to avoiding comments:
# Module Support to expire 12/1/2019
# Dependent library randomly fails here, cause unknown
# More efficient approach here in order to save memory
120
Has less bugs
121
Has less bugs
See previous 30 slides ;)
122
Quality Code Is
123
A number of voluntary activities and
approaches that Application and
Automation Engineers use to create
better quality code today
Quality Code Is
124
Quality Code Is About
125
Quality Code Is About
126
Quality Code is Iike Tests because
127
It is optional
Quality Code is Iike Tests because
128
It is optional
*** IT IS UP TO YOU **
Quality Code is Iike Tests because
129
Quality Code is
Level II thinking
130
Recursion, Linked lists (single, double,
circular), trees, binary tree searches, maps,
design patterns, etc. are all useful tools at the
right time.
However, as you’ve seen, there is a lot more
to quality than algorithms for highly
performant code </rant>
131
Quality Code is also
more of a pleasure and
less of a pain in the *
132
Thank You
133
Comments ?
Questions ?
Discussion ?
Experiences ?
Advice ?

Contenu connexe

Similaire à Quality Ruby Code Techniques for Maintainable Applications

Day1 - TDD (Lecture SS 2015)
Day1 - TDD (Lecture SS 2015)Day1 - TDD (Lecture SS 2015)
Day1 - TDD (Lecture SS 2015)wolframkriesing
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Danny Preussler
 
Know your platform. 7 things every scala developer should know about jvm
Know your platform. 7 things every scala developer should know about jvmKnow your platform. 7 things every scala developer should know about jvm
Know your platform. 7 things every scala developer should know about jvmPawel Szulc
 
Pair Programming :: Conferencia Agile Spain 2014
Pair Programming :: Conferencia Agile Spain 2014Pair Programming :: Conferencia Agile Spain 2014
Pair Programming :: Conferencia Agile Spain 2014Pedro Gustavo Torres
 
“One man” development process model
“One man” development process model“One man” development process model
“One man” development process modelSilicon Straits
 
Design and Evolution of cyber-dojo
Design and Evolution of cyber-dojoDesign and Evolution of cyber-dojo
Design and Evolution of cyber-dojoJon Jagger
 
Surviving a Hackathon and Beyond
Surviving a Hackathon and BeyondSurviving a Hackathon and Beyond
Surviving a Hackathon and Beyondimoneytech
 
Software as a craft (February, 2018)
Software as a craft (February, 2018)Software as a craft (February, 2018)
Software as a craft (February, 2018)Rachel M. Carmena
 
Test driven development with Jasmine
Test driven development with JasmineTest driven development with Jasmine
Test driven development with Jasmineharshit040591
 
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...ORAU
 
Code Quality Makes Your Job Easier
Code Quality Makes Your Job EasierCode Quality Makes Your Job Easier
Code Quality Makes Your Job EasierTonya Mork
 
Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity Peter Gfader
 
Building a site for people with big imaginations
Building a site for people with big imaginationsBuilding a site for people with big imaginations
Building a site for people with big imaginationsMark Mansour
 
Streaming Media West - Podcast Workshop
Streaming Media West - Podcast WorkshopStreaming Media West - Podcast Workshop
Streaming Media West - Podcast WorkshopJose Castillo
 

Similaire à Quality Ruby Code Techniques for Maintainable Applications (20)

Day1 - TDD (Lecture SS 2015)
Day1 - TDD (Lecture SS 2015)Day1 - TDD (Lecture SS 2015)
Day1 - TDD (Lecture SS 2015)
 
U72 lesson 08
U72 lesson 08U72 lesson 08
U72 lesson 08
 
U72 lesson 08
U72 lesson 08U72 lesson 08
U72 lesson 08
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
Know your platform. 7 things every scala developer should know about jvm
Know your platform. 7 things every scala developer should know about jvmKnow your platform. 7 things every scala developer should know about jvm
Know your platform. 7 things every scala developer should know about jvm
 
Pair Programming :: Conferencia Agile Spain 2014
Pair Programming :: Conferencia Agile Spain 2014Pair Programming :: Conferencia Agile Spain 2014
Pair Programming :: Conferencia Agile Spain 2014
 
Make a better with clean code
Make a better with clean codeMake a better with clean code
Make a better with clean code
 
“One man” development process model
“One man” development process model“One man” development process model
“One man” development process model
 
Design and Evolution of cyber-dojo
Design and Evolution of cyber-dojoDesign and Evolution of cyber-dojo
Design and Evolution of cyber-dojo
 
Surviving a Hackathon and Beyond
Surviving a Hackathon and BeyondSurviving a Hackathon and Beyond
Surviving a Hackathon and Beyond
 
Createinto
CreateintoCreateinto
Createinto
 
Software as a craft (February, 2018)
Software as a craft (February, 2018)Software as a craft (February, 2018)
Software as a craft (February, 2018)
 
Test driven development with Jasmine
Test driven development with JasmineTest driven development with Jasmine
Test driven development with Jasmine
 
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
 
Code Quality Makes Your Job Easier
Code Quality Makes Your Job EasierCode Quality Makes Your Job Easier
Code Quality Makes Your Job Easier
 
Tweakers Anonymous
Tweakers AnonymousTweakers Anonymous
Tweakers Anonymous
 
Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity Achieving Technical Excellence in Your Software Teams - from Devternity
Achieving Technical Excellence in Your Software Teams - from Devternity
 
Building a site for people with big imaginations
Building a site for people with big imaginationsBuilding a site for people with big imaginations
Building a site for people with big imaginations
 
Development tools
Development toolsDevelopment tools
Development tools
 
Streaming Media West - Podcast Workshop
Streaming Media West - Podcast WorkshopStreaming Media West - Podcast Workshop
Streaming Media West - Podcast Workshop
 

Dernier

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Dernier (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Quality Ruby Code Techniques for Maintainable Applications

  • 1. Quality Ruby Code Michael Durrant m2@snap2web.com June 2019
  • 2.
  • 3.
  • 4. Thanks to all the bostonrb organizers!
  • 5. Thanks to all the bostonrb organizers! Thank you EzCater, a good sponsor makes a huge difference!
  • 6. Thanks to all the bostonrb organizers! Thank you EzCater, a good sponsor makes a huge difference! Thank you to all my friends in Indianapolis for teaching me so much! Thank you Stacey, Aldrin, Ben, Bella, Joe, Adam, Adrian, Orando!
  • 7. Thanks to all the bostonrb organizers! Thank you EzCater, a good sponsor makes a huge difference! Thank you to all my friends in Indianapolis for teaching me so much! Thank you Stacey, Aldrin, Ben, Bella, Joe, Adam, Adrian, Orando! Thank YOU for coming tonight and supporting the community!
  • 8. My talks are intended to be interactive so
  • 9. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’)
  • 10. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’) - Hearing your opinions and experiences is very welcome
  • 11. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’) - Hearing your opinions and experiences is very welcome - Prefer questions, answers, conversations during not after
  • 12. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’) - Hearing your opinions and experiences is very welcome - Prefer questions, answers, conversations during not after - Expect polls and use them to learn about your community
  • 13. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’) - Hearing your opinions and experiences is very welcome - Prefer questions, answers, conversations during not after - Expect polls and use them to learn about your community - Cell phone and laptop use during main talk is discouraged
  • 14. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’) - Hearing your opinions and experiences is very welcome - Prefer questions, answers, conversations during not after - Expect polls and use them to learn about your community - Cell phone and laptop use during main talk is discouraged - Cell phone airplane mode is encouraged
  • 15. My talks are intended to be interactive so - Ask questions (wave hand, call out ‘Michael’) - Hearing your opinions and experiences is very welcome - Prefer questions, answers, conversations during not after - Expect polls and use them to learn about your community - Cell phone and laptop use during main talk is discouraged - Cell phone airplane mode is encouraged - Public Video is being recorded for most of my talks, !smile
  • 17. 17 But first… Docker... git clone --recursive https://github.com/bretfisher/container.training cd ~/container.training/stacks docker-compose up docker-compose down docker node ls # (err)docker docker swarm init # (ONE node only!) Err? next line docker swarm init --advertise-addr eth0 docker swarm join --token SWMTKN-1-59fl4ak4nqjmao1ofttrc4eprhrola2l87... 172.31.4.182:2377 Docker node ls See https://dockercon19.bretfisher.com/#1
  • 28. 28 And… about deploying code... - IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc.
  • 29. 29 And… about deploying code... - IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc. - Programming configuration via a DSL (e.g. Chef count attribute)
  • 30. 30 And… about deploying code... - IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc. - Programming configuration via a DSL (e.g. Chef count attribute) - Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ)
  • 31. 31 And… about deploying code... - IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc. - Programming configuration via a DSL (e.g. Chef count attribute) - Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ) - Set the State, let the tool manage it
  • 32. 32 And… about deploying code... - IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc. - Programming configuration via a DSL (e.g. Chef count attribute) - Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ) - Set the State, let the tool manage it - Know the key factors – docker has a daemon, ansible doesn’t and uses ssh (port 22) to do things, etc.
  • 33. 33 And… about deploying code... - IAC Infrastructure As Code. Recipies (chef), Playbooks (Ansible), etc. - Programming configuration via a DSL (e.g. Chef count attribute) - Idempotent. Immutable Infrastructure. Set it. And forget it! (bow to Ronco BBQ) - Set the State, let the tool manage it - Know the key factors – docker has a daemon, ansible doesn’t and uses ssh (port 22) to do things, etc. - Use checklists (for developing IAS is a very good idea)
  • 34. 34 And… about deploying code... Why we use Terraform and not Chef, Puppet, Ansible, SaltStack, or CloudFormation https://blog.gruntwork.io/why-we-use-terraform-and-not-chef-puppet-ansible-saltstack-or-cloudformation- 7989dad2865c Infrastructure as Code: Chef, Ansible, Puppet, or Terraform? https://www.ibm.com/cloud/blog/chef-ansible-puppet-terraform Puppet vs. Chef vs. Ansible vs. SaltStack https://www.intigua.com/blog/puppet-vs.-chef-vs.-ansible-vs.-saltstack Comparing top DevOps tools: Docker vs Kubernetes vs Puppet vs Chef vs Ansible https://www.znetlive.com/blog/compare-top-devops-tools-docker-kubernetes-puppet-chef-ansible/ Choosing a deployment tool - ansible vs puppet vs chef vs salt https://gist.github.com/jaceklaskowski/bd3d06489ec004af6ed9 Puppet vs Salt vs Chef vs Ansible, which to choose? https://www.reddit.com/r/linuxadmin/comments/9ljveg/puppet_vs_salt_vs_chef_vs_ansible_which_to_choose/
  • 36. 36 As a Quality Engineer.. ● I’m always thinking about more tests
  • 37. 37 As a Quality Engineer.. ● I’m always thinking about more tests better tests
  • 38. 38 As a Quality Engineer.. ● I’m always thinking about more tests better tests so we can have less bugs
  • 39. 39 As a Quality Engineer.. ● I’m always thinking about more tests better tests so we can have less bugs higher quality
  • 40. 40 As a Quality Engineer.. ● I’m always thinking about more tests better tests so we can have less bugs higher quality ● Or we can end up solving the wrong problems !
  • 41. 41 We have a codebase with bugs...
  • 42. 42 So we add tests...
  • 43. 43 Now we have two codebases for bugs
  • 44. 44 Now we have two codebases for bugs And our overall code may now be
  • 45. 45 Now we have two codebases for bugs And our overall code may now be ● Twice as hard to maintain
  • 46. 46 Now we have two codebases for bugs And our overall code may now be ● Twice as hard to maintain ● Twice as hard to fix
  • 47. 47 Now we have two codebases for bugs And our overall code may now be ● Twice as hard to maintain ● Twice as hard to fix ● Twice as hard to change
  • 49. 49 Tests can reveal issues with Quality However just having them does not improve application quality
  • 50. 50 We need to address quality issues in the code itself (both automation and application code)
  • 52. 52 We need (in all code) ● Quality Code Approaches (todays topic) ● KPIs ! Not # tests/bugs, but code level ● Automated Linting, e.g. Rubocop ● Code Grading, Code Climate ● Robust exception handling (also covered today) ● Open minded Code Review mentality ● Easy to use Code Review tools ● IAC
  • 53. 53 ● Has Tests ● Is molded ● Good names ● Low wtf Ratio ● Handles Exceptions ● KISS Mr Meta gently ● Uses few parameters ● Uses Linting and Grading ● Allows a Little Duplication Quality (Ruby) Code ● Is a choice ● Is easy to read ● Is not optimized ● Is easy to change ● Uses standards that evolve ● Uses the right rules guidelines ● Has small classes and methods ● Values comments as they are so rare ● Has less bugs
  • 54. 54 Is Molded Through a healthy code review process that features: - Code is owned by all - PR Comments are the norm - Quality is not generally time-boxed - Humbleness leads with pride in code not ego - Refactoring is nearly always done in-cycle. Now
  • 57. 57 Handles Exceptions # Not so good begin ... some code ... rescue end
  • 58. 58 Handles Exceptions # Better (scoped) begin ... some code ... Rescue StandardError end
  • 59. 59 Handles Exceptions # Even better (scoped with re-raise) begin ... some code ... rescue StandardError => e some code If condition still an issue can do `raise e` end Rescue the most specific exception possible
  • 60. 60 Handles Exceptions # Even Even better (scoped more with re-raise) begin ... some code ... rescue StandardError::ArgumentError, ZeroDivisionError => e some code If condition still an issue can do `raise e` end Rescue the most specific exception(s) possible
  • 61. 61 Handles Exceptions # Sometimes (code with dependencies) you just want to retry Example1: An API error for get prices which can be retried:
  • 62. 62 def get_api_prices tries = 0 begin tries += 1 prices = api_get_buy_sell sleep 2 rescue SocketError => e p e.message if (tries < 5) p "but retrying... try: #{tries}" sleep(2 ** tries) retry else p 'giving up...' raise e # Do not remove, we need a current price to be set end end end Handles Exceptions – Retry Example 1
  • 63. 63 visit_with_retry sell_url def visit_with_retry(url, tries = 5) begin @s.visit(url) sleep 2 find_link(‘login’) rescue Capybara::ElementNotFound, Net::ReadTimeout => e p e.message if (tries < 1) p "but retrying... try: #{tries}" sleep(2 ** tries) tries -= 1 retry else p 'giving up...' raise e # Do not remove, we need a current price to be set end end end Handles Exceptions – Retry Example 2
  • 64. 64 Handles Exceptions – Raise them well
  • 65. 65 raise “just the text and not the best” Handles Exceptions – Raise them well
  • 66. 66 raise “just the text and not the best” raise RuntimeError, “stop executing please” Handles Exceptions – Raise them well
  • 67. 67 raise “just the text and not the best” raise RuntimeError, “stop executing please” Create custom errors (“help your fellow / future programmer): class PaymentError < StandardError end raise PaymentError, “zero amount in payment field pmt-001” Handles Exceptions – Raise them well
  • 68. 68 raise “just the text and not the best” raise RuntimeError, “stop executing please” Create custom errors (“help your fellow / future programmer): class PaymentError < StandardError end raise PaymentError, “zero amount in payment field pmt-001” Pass information to exceptions raise TemperatureError.new(180) Then add attribute and set in initialize Handles Exceptions – Raise them well
  • 69. 69 Handles Exceptions – method missing
  • 70. 70 - Saves the day Handles Exceptions – method missing
  • 71. 71 - Saves the day - Lets you code carelessly Handles Exceptions – method missing
  • 72. 72 - Saves the day - Lets you code carelessly - Makes everything feel better Handles Exceptions – method missing
  • 73. 73 - Saves the day - Lets you code carelessly - Makes everything feel better - Is a swiss Army knife that can tear your code apart Handles Exceptions – method missing
  • 74. 74 - Saves the day - Lets you code carelessly - Makes everything feel better - Is a swiss Army knife that can tear your code apart - Can lead to highly opinionated programmer talking at you Handles Exceptions – method missing
  • 75. 75 Handles Exceptions – Learn More...
  • 78. 78 Handles Exceptions – avoids them - Treat all objects as if they could be nil
  • 79. 79 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings
  • 80. 80 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods
  • 81. 81 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize”
  • 82. 82 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy
  • 83. 83 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue
  • 84. 84 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue - Manages Resources with Blocks and ensure* (db connections, file locks, etc)
  • 85. 85 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue - Manages Resources with Blocks and ensure* (db connections, file locks, etc) - Prefer throw-catch (last value) to raise-rescue (pass in exception)
  • 86. 86 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue - Manages Resources with Blocks and ensure* (db connections, file locks, etc) - Prefer throw-catch (last value) to raise-rescue (pass in exception) - Prefer define_method to method_missing
  • 87. 87 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue - Manages Resources with Blocks and ensure* (db connections, file locks, etc) - Prefer throw-catch (last value) to raise-rescue (pass in exception) - Prefer define_method to method_missing - Simulate determinism with Mock Objects, e.g. make http fail
  • 88. 88 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue - Manages Resources with Blocks and ensure* (db connections, file locks, etc) - Prefer throw-catch (last value) to raise-rescue (pass in exception) - Prefer define_method to method_missing - Simulate determinism with Mock Objects, e.g. make http fail - Memoize ||= expensive operations
  • 89. 89 Handles Exceptions – avoids them - Treat all objects as if they could be nil - Pays attention to warnings - Prefer Set and Struct to Array and Hash for some structured data - unique data (Set) - attribute names (getters, setters) - instance and class methods - Use module (scope) to avoid reopening classes, e.g. “class” or “initialize” - Prefer Class Instance (self.thing) over class (@@) variables – hierarchy - Always pass a parameter to reduce / inject, e.g. 0, {}, etc. due to empty issue - Manages Resources with Blocks and ensure* (db connections, file locks, etc) - Prefer throw-catch (last value) to raise-rescue (pass in exception) - Prefer define_method to method_missing - Simulate determinism with Mock Objects, e.g. make http fail - Memoize ||= expensive operations - prefer local over instance
  • 90. 90 KISS Mr Meta gently Metaprogramming is the answer to all programming problems !!!
  • 93. 93 Use few parameters 1 is ok. 2 is a lot
  • 94. 94 Use few parameters 1 is ok. 2 is a lot 0 is best
  • 95. 95 Use few parameters 1 is ok. 2 is a lot 0 is best However don’t play the “object trick” without thinking it through
  • 98. 98 Allow a little duplication
  • 99. 99 Allow a little duplication “A little duplication is better than the wrong abstraction”
  • 100. 100 Allow a little duplication Avoid extremes
  • 101. 101 Allow a little duplication Avoid extremes
  • 102. 102 Allow a little duplication Avoid extremes
  • 103. 103 Is easy to read
  • 104. 104 Is easy to read Use english
  • 106. 106 Is not optimized Very little code in 2019 needs to be highly performant. Usually not in anticipation of performance issues
  • 107. 107 Uses standards that evolve (!)
  • 108. 108 Uses standards that evolve (!) Over longer time periods new structures and needs will become apparent and should be discussed
  • 109. 109 Uses the right rules guidelines
  • 110. 110 Uses the right rules guidelines Quality code is code that strikes a balance between the rules, not just code that follows all the rules
  • 111. 111 Has small classes and methods
  • 112. 112 Has small classes and methods SRP
  • 113. 113 Has small classes and methods SRP Sandy Metz
  • 114. 114 Has small classes and methods SRP Sandy Metz Duh
  • 115. 115 Value comments - they are so rare
  • 116. 116 Value comments - they are so rare Exceptions to avoiding comments:
  • 117. 117 Value comments - they are so rare Exceptions to avoiding comments: # Module Support to expire 12/1/2019
  • 118. 118 Value comments - they are so rare Exceptions to avoiding comments: # Module Support to expire 12/1/2019 # Dependent library randomly fails here, cause unknown
  • 119. 119 Value comments - they are so rare Exceptions to avoiding comments: # Module Support to expire 12/1/2019 # Dependent library randomly fails here, cause unknown # More efficient approach here in order to save memory
  • 121. 121 Has less bugs See previous 30 slides ;)
  • 123. 123 A number of voluntary activities and approaches that Application and Automation Engineers use to create better quality code today Quality Code Is
  • 126. 126 Quality Code is Iike Tests because
  • 127. 127 It is optional Quality Code is Iike Tests because
  • 128. 128 It is optional *** IT IS UP TO YOU ** Quality Code is Iike Tests because
  • 130. 130 Recursion, Linked lists (single, double, circular), trees, binary tree searches, maps, design patterns, etc. are all useful tools at the right time. However, as you’ve seen, there is a lot more to quality than algorithms for highly performant code </rant>
  • 131. 131 Quality Code is also more of a pleasure and less of a pain in the *
  • 133. 133 Comments ? Questions ? Discussion ? Experiences ? Advice ?