Slide 1

Slide 1 text

Grow a bonsai, not a shrub

Slide 2

Slide 2 text

Kevin Deisz @kddeisz

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Whose fault?

Slide 5

Slide 5 text

the people that built automatic cars Whose fault?

Slide 6

Slide 6 text

the people that built automatic cars the people that still rent out manual cars Whose fault?

Slide 7

Slide 7 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 8

Slide 8 text

None of these

Slide 9

Slide 9 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 10

Slide 10 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 11

Slide 11 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 12

Slide 12 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 13

Slide 13 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 14

Slide 14 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how Whose fault?

Slide 15

Slide 15 text

the people that built automatic cars the people that still rent out manual cars your fault, for never learning how to drive a manual car even though your friend Greg told you multiple times he would teach you, your mother-in-law told you to pick a time and she would rent a manual car for the weekend and teach you, and your father is more than a little embarrassed that you don’t know how a societal failure to standardize Whose fault?

Slide 16

Slide 16 text

Code is written by the first engineer that has to understand the problem.

Slide 17

Slide 17 text

Code is read by every engineer that has to understand the problem.

Slide 18

Slide 18 text

Bad outcomes

Slide 19

Slide 19 text

Microservices Bad outcomes

Slide 20

Slide 20 text

Microservices Halt and refactor Bad outcomes

Slide 21

Slide 21 text

Microservices Halt and refactor Complete rewrite Bad outcomes

Slide 22

Slide 22 text

Microservices Halt and refactor Complete rewrite Bad outcomes

Slide 23

Slide 23 text

Solutions

Slide 24

Slide 24 text

Static Analysis Solutions

Slide 25

Slide 25 text

Static Analysis
 Reflection Tests
 Solutions

Slide 26

Slide 26 text

Static Analysis
 Reflection Tests
 Strategic Investment
 Solutions

Slide 27

Slide 27 text

Static Analysis
 Determine the standards Reflection Tests
 Strategic Investment
 Solutions

Slide 28

Slide 28 text

Static Analysis
 Determine the standards Reflection Tests
 Enforce standards on new code Strategic Investment
 Solutions

Slide 29

Slide 29 text

Static Analysis
 Determine the standards Reflection Tests
 Enforce standards on new code Strategic Investment
 Enforce standards on old code Solutions

Slide 30

Slide 30 text

Static Analysis • Reflection Tests • Strategic Investment

Slide 31

Slide 31 text

Static Analysis Static Analysis • Reflection Tests • Strategic Investment Determine the standards

Slide 32

Slide 32 text

rubocop Static Analysis • Reflection Tests • Strategic Investment

Slide 33

Slide 33 text

rubocop Static Analysis • Reflection Tests • Strategic Investment

Slide 34

Slide 34 text

rubocop Static Analysis • Reflection Tests • Strategic Investment

Slide 35

Slide 35 text

Bundler Gemspec Layout Lint Metrics rubocop Static Analysis • Reflection Tests • Strategic Investment Naming Performance Rails Security Style

Slide 36

Slide 36 text

Metrics rubocop Static Analysis • Reflection Tests • Strategic Investment

Slide 37

Slide 37 text

AbcSize BlockLength BlockNesting ClassLength CyclomaticComplexity Metrics Static Analysis • Reflection Tests • Strategic Investment LineLength MethodLength ModuleLength ParameterLists PerceivedComplexity

Slide 38

Slide 38 text

reek Static Analysis • Reflection Tests • Strategic Investment

Slide 39

Slide 39 text

Attribute Boolean Parameter Class Variable Control Parameter Data Clump Duplicate Method Call Feature Envy Instance Variable Assumption Irresponsible Module Long Parameter List reek Long Yield List Manual Dispatch Missing Safe Method Module Initialize Nested Iterators Nil Check Repeated Conditional Subclassed From Core Class Too Many Constants Too Many Instance Variables Too Many Methods Too Many Statements Uncommunicative Method Name Uncommunicative Module Name Uncommunicative Parameter Name Uncommunicative Variable Name Unused Parameters Unused Private Method Utility Function Static Analysis • Reflection Tests • Strategic Investment

Slide 40

Slide 40 text

reek Long Yield List Manual Dispatch Missing Safe Method Module Initialize Nested Iterators Nil Check Repeated Conditional Subclassed From Core Class Too Many Constants Too Many Instance Variables Too Many Methods Too Many Statements Uncommunicative Method Name Uncommunicative Module Name Uncommunicative Parameter Name Uncommunicative Variable Name Unused Parameters Unused Private Method Utility Function Static Analysis • Reflection Tests • Strategic Investment Attribute Boolean Parameter Class Variable Control Parameter Data Clump • Duplicate Method Call Feature Envy Instance Variable Assumption • Irresponsible Module Long Parameter List

Slide 41

Slide 41 text

Rules Static Analysis • Reflection Tests • Strategic Investment BlockLength ClassLength LineLength ModuleLength MethodLength Too Many Constants Too Many Methods Too Many Statements AbcSize BlockNesting Cyclomatic Complexity Long Parameter List Long Yield List Nested Iterators ParameterLists Perceived Complexity Feature Envy Too Many Instance Variables Utility Function

Slide 42

Slide 42 text

Rules - variance Static Analysis • Reflection Tests • Strategic Investment BlockLength ClassLength LineLength ModuleLength MethodLength Too Many Constants Too Many Methods Too Many Statements

Slide 43

Slide 43 text

Rules - complexity Static Analysis • Reflection Tests • Strategic Investment AbcSize BlockNesting Cyclomatic Complexity Long Parameter List Long Yield List Nested Iterators ParameterLists Perceived Complexity

Slide 44

Slide 44 text

Rules - design Static Analysis • Reflection Tests • Strategic Investment Feature Envy Too Many Instance Variables Utility Function

Slide 45

Slide 45 text

Variance Static Analysis • Reflection Tests • Strategic Investment

Slide 46

Slide 46 text

class Event ... def self.in_locations(locations) joins(in_locations_join_sources(locations)) .group(:id) .having(in_locations_match_conditions) end class << self def in_locations_explicit_location_match(locations) ... end def in_locations_join_sources(locations) ... end def in_locations_match_conditions ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 47

Slide 47 text

class Event ... def self.in_locations(locations) Queries::EventsInLocations.new(self, locations).results end end Static Analysis • Reflection Tests • Strategic Investment

Slide 48

Slide 48 text

class Event ... def self.in_locations(locations) Queries::EventsInLocations.new(self, locations).results end end Static Analysis • Reflection Tests • Strategic Investment

Slide 49

Slide 49 text

class Event ... def self.in_locations(locations) Queries::EventsInLocations.new(self, locations).results end end Static Analysis • Reflection Tests • Strategic Investment

Slide 50

Slide 50 text

class Event ... def self.in_locations(locations) Queries::EventsInLocations.new(self, locations).results end end Static Analysis • Reflection Tests • Strategic Investment

Slide 51

Slide 51 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end Static Analysis • Reflection Tests • Strategic Investment

Slide 52

Slide 52 text

module Queries class EventsInLocations attr_reader :relation, :locations def initialize(relation, locations) @relation = relation @locations = locations end def results relation.joins(join_sources).group(:id) .having(match_conditions) end private def events @events ||= Event.arel_table end ... end end Static Analysis • Reflection Tests • Strategic Investment

Slide 53

Slide 53 text

Complexity Static Analysis • Reflection Tests • Strategic Investment

Slide 54

Slide 54 text

class User < ApplicationRecord ... def can_admin?(model) case model when Event model.host == self when Photo model.uploader == self || can_admin?(model.event) when Post model.creator == self when Recognition model.creator == self when Rsvp model.user == self || can_admin?(model.event) ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 55

Slide 55 text

class User < ApplicationRecord ... def can_admin?(model) # rubocop:disable all case model when Event model.host == self when Photo model.uploader == self || can_admin?(model.event) when Post model.creator == self when Recognition model.creator == self when Rsvp model.user == self || can_admin?(model.event) ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 56

Slide 56 text

class User < ApplicationRecord ... def can_admin?(model) case model when Event model.host == self when Photo model.uploader == self || can_admin?(model.event) when Post model.creator == self when Recognition model.creator == self when Rsvp model.user == self || can_admin?(model.event) ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 57

Slide 57 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 58

Slide 58 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 59

Slide 59 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 60

Slide 60 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 61

Slide 61 text

module Users class AdminCheck attr_reader :user def initialize(user) @user = user end def can_admin?(model) case model when Event model.host == user when Photo model.uploader == user || can_admin?(model.event) when Post model.creator == user when Recognition model.creator == user ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 62

Slide 62 text

Design Static Analysis • Reflection Tests • Strategic Investment

Slide 63

Slide 63 text

module Users class AdminCheck attr_reader :user def initialize(user) @user = user end def can_admin?(model) case model when Event model.host == user when Photo model.uploader == user || can_admin?(model.event) when Post model.creator == user ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 64

Slide 64 text

module Users class AdminCheck ... def can_admin?(model) case model when Event model.host == user when Photo model.uploader == user || can_admin?(model.event) when Post model.creator == user ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 65

Slide 65 text

module Checks refine Event do def admin?(user) host == user end end end module Users class AdminCheck ... using Checks def can_admin?(model) case model when Event model.admin?(user) when Photo model.uploader == user || can_admin?(model.event) when Post model.creator == user ... end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 66

Slide 66 text

module Checks refine Event do def admin?(user) host == user end end refine Photo do def admin?(user) uploader == user || event.admin?(user) end end end Static Analysis • Reflection Tests • Strategic Investment module Users class AdminCheck ... using Checks def can_admin?(model) case model when Event model.admin?(user) when Photo model.admin?(user) when Post model.creator == user ... end end end

Slide 67

Slide 67 text

module Checks refine Event do def admin?(user) host == user end end refine Photo do def admin?(user) uploader == user || event.admin?(user) end end refine Post do def admin?(user) creator == user end end end Static Analysis • Reflection Tests • Strategic Investment module Users class AdminCheck ... using Checks def can_admin?(model) case model when Event model.admin?(user) when Photo model.admin?(user) when Post model.admin?(user) ... end end end

Slide 68

Slide 68 text

module Checks refine Event do def admin?(user) host == user end end refine Photo do def admin?(user) uploader == user || event.admin?(user) end end refine Post do def admin?(user) creator == user end end ... end module Users class AdminCheck ... using Checks def can_admin?(model) model.admin?(user) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 69

Slide 69 text

Extract common behavior Static Analysis • Reflection Tests • Strategic Investment

Slide 70

Slide 70 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end Static Analysis • Reflection Tests • Strategic Investment

Slide 71

Slide 71 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end Static Analysis • Reflection Tests • Strategic Investment

Slide 72

Slide 72 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end Static Analysis • Reflection Tests • Strategic Investment

Slide 73

Slide 73 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end Static Analysis • Reflection Tests • Strategic Investment

Slide 74

Slide 74 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 75

Slide 75 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 76

Slide 76 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 77

Slide 77 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 78

Slide 78 text

class Event ... scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 79

Slide 79 text

class Event ... extend Ext::Query scope :in_locations, -> (locations) { Queries::EventsInLocations.new(self, locations).results } end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 80

Slide 80 text

class Event ... extend Ext::Query query :in_locations, Queries::EventsInLocations end module Ext module Query def query(name, constant) scope name, ->(*args) { constant.new(self, *args).results } end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 81

Slide 81 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 82

Slide 82 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 83

Slide 83 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 84

Slide 84 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end Static Analysis • Reflection Tests • Strategic Investment

Slide 85

Slide 85 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 86

Slide 86 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 87

Slide 87 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 88

Slide 88 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 89

Slide 89 text

class User < ApplicationRecord ... def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 90

Slide 90 text

Static Analysis • Reflection Tests • Strategic Investment class User < ApplicationRecord ... extend Ext::Factory def can_admin?(model) Users::AdminCheck.new(self).can_admin?(model) end end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end

Slide 91

Slide 91 text

class User < ApplicationRecord ... extend Ext::Factory factory :admin_check, Users::AdminCheck, delegate: :can_admin? end module Ext module Factory def factory(method_name, clazz, delegate: []) define_method(method_name) { clazz.new(self) } return if Array(delegate).empty? public_send(:delegate, *delegate, to: method_name) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 92

Slide 92 text

Reflection Tests Static Analysis • Reflection Tests • Strategic Investment Enforce standards on new code

Slide 93

Slide 93 text

Reflection tests should always answer a question. Static Analysis • Reflection Tests • Strategic Investment

Slide 94

Slide 94 text

What needs to be changed before we can upgrade to the next version of Rails? Static Analysis • Reflection Tests • Strategic Investment

Slide 95

Slide 95 text

What needs to be changed before we can upgrade to the next version of Rails? Static Analysis • Reflection Tests • Strategic Investment # Be sure to test this when we upgrade Rails

Slide 96

Slide 96 text

What needs to be changed before we can upgrade to the next version of Rails? Static Analysis • Reflection Tests • Strategic Investment # Be sure to test this when we upgrade Rails # Monkey-patching Rails here, so we’ll need to
 # test this when we upgrade

Slide 97

Slide 97 text

What needs to be changed before we can upgrade to the next version of Rails? Static Analysis • Reflection Tests • Strategic Investment # Be sure to test this when we upgrade Rails # We can remove this when we upgrade Rails # Monkey-patching Rails here, so we’ll need to
 # test this when we upgrade

Slide 98

Slide 98 text

require 'test_helper' class UpgradeTest < ActiveSupport::TestCase LAST_TESTED_VERSION = Gem::Version.new('5.2.1') def self.test(name) version = Gem::Version.new(Rails::VERSION::STRING) super(name) { flunk if version > LAST_TESTED_VERSION } end test 'Comment#as_json' test 'counter_cache and touch on comment' test 'counter_cache and touch on cheer' test 'ApplicationController::make_response!' test 'Buildable::TouchAll' end Static Analysis • Reflection Tests • Strategic Investment

Slide 99

Slide 99 text

require 'test_helper' class UpgradeTest < ActiveSupport::TestCase LAST_TESTED_VERSION = Gem::Version.new('5.2.1') def self.test(name) version = Gem::Version.new(Rails::VERSION::STRING) super(name) { flunk if version > LAST_TESTED_VERSION } end test 'Comment#as_json' test 'counter_cache and touch on comment' test 'counter_cache and touch on cheer' test 'ApplicationController::make_response!' test 'Buildable::TouchAll' end Static Analysis • Reflection Tests • Strategic Investment

Slide 100

Slide 100 text

require 'test_helper' class UpgradeTest < ActiveSupport::TestCase LAST_TESTED_VERSION = Gem::Version.new('5.2.1') def self.test(name) version = Gem::Version.new(Rails::VERSION::STRING) super(name) { flunk if version > LAST_TESTED_VERSION } end test 'Comment#as_json' test 'counter_cache and touch on comment' test 'counter_cache and touch on cheer' test 'ApplicationController::make_response!' test 'Buildable::TouchAll' end Static Analysis • Reflection Tests • Strategic Investment

Slide 101

Slide 101 text

require 'test_helper' class UpgradeTest < ActiveSupport::TestCase LAST_TESTED_VERSION = Gem::Version.new('5.2.1') def self.test(name) version = Gem::Version.new(Rails::VERSION::STRING) super(name) { flunk if version > LAST_TESTED_VERSION } end test 'Comment#as_json' test 'counter_cache and touch on comment' test 'counter_cache and touch on cheer' test 'ApplicationController::make_response!' test 'Buildable::TouchAll' end Static Analysis • Reflection Tests • Strategic Investment

Slide 102

Slide 102 text

require 'test_helper' class UpgradeTest < ActiveSupport::TestCase LAST_TESTED_VERSION = Gem::Version.new('5.2.1') def self.test(name) version = Gem::Version.new(Rails::VERSION::STRING) super(name) { flunk if version > LAST_TESTED_VERSION } end test 'Comment#as_json' test 'counter_cache and touch on comment' test 'counter_cache and touch on cheer' test 'ApplicationController::make_response!' test 'Buildable::TouchAll' end Static Analysis • Reflection Tests • Strategic Investment

Slide 103

Slide 103 text

When do we split actions out into a different controller? Static Analysis • Reflection Tests • Strategic Investment

Slide 104

Slide 104 text

When do we split actions out into a different controller? Static Analysis • Reflection Tests • Strategic Investment # TODO: Split this out to a different controller

Slide 105

Slide 105 text

When do we split actions out into a different controller? Static Analysis • Reflection Tests • Strategic Investment # TODO: Split this out to a different controller # TODO: Refactor permissions for this controller

Slide 106

Slide 106 text

When do we split actions out into a different controller? Static Analysis • Reflection Tests • Strategic Investment # TODO: Split this out to a different controller # TODO: Share this logic # TODO: Refactor permissions for this controller

Slide 107

Slide 107 text

require 'test_helper' Rails.application.eager_load! class ControllerActionsTest < ActiveSupport::TestCase ALLOWED = %w[index show create update destroy].freeze ApplicationController.descendants.each do |controller| test "#{controller.name} only has standard actions" do violations = controller.action_methods - ALLOWED assert_empty violations end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 108

Slide 108 text

require 'test_helper' Rails.application.eager_load! class ControllerActionsTest < ActiveSupport::TestCase ALLOWED = %w[index show create update destroy].freeze ApplicationController.descendants.each do |controller| test "#{controller.name} only has standard actions" do violations = controller.action_methods - ALLOWED assert_empty violations end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 109

Slide 109 text

require 'test_helper' Rails.application.eager_load! class ControllerActionsTest < ActiveSupport::TestCase ALLOWED = %w[index show create update destroy].freeze ApplicationController.descendants.each do |controller| test "#{controller.name} only has standard actions" do violations = controller.action_methods - ALLOWED assert_empty violations end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 110

Slide 110 text

require 'test_helper' Rails.application.eager_load! class ControllerActionsTest < ActiveSupport::TestCase ALLOWED = %w[index show create update destroy].freeze ApplicationController.descendants.each do |controller| test "#{controller.name} only has standard actions" do violations = controller.action_methods - ALLOWED assert_empty violations end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 111

Slide 111 text

require 'test_helper' Rails.application.eager_load! class ControllerActionsTest < ActiveSupport::TestCase ALLOWED = %w[index show create update destroy].freeze ApplicationController.descendants.each do |controller| test "#{controller.name} only has standard actions" do violations = controller.action_methods - ALLOWED assert_empty violations end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 112

Slide 112 text

Where do we place logic that belongs to ActiveRecord models? Static Analysis • Reflection Tests • Strategic Investment

Slide 113

Slide 113 text

Where do we place logic that belongs to ActiveRecord models? Static Analysis • Reflection Tests • Strategic Investment # TODO: Move this into its own object

Slide 114

Slide 114 text

Where do we place logic that belongs to ActiveRecord models? Static Analysis • Reflection Tests • Strategic Investment # TODO: Move this into its own object # TODO: Refactor to use new factory system

Slide 115

Slide 115 text

Where do we place logic that belongs to ActiveRecord models? Static Analysis • Reflection Tests • Strategic Investment # TODO: Move this into its own object # TODO: Reuse logic from other location # TODO: Refactor to use new factory system

Slide 116

Slide 116 text

require 'ripper' class DefList class DefStatement ... end ... def read(filepath) parse(filepath, Ripper.sexp(File.read(filepath))) end private def parse(filepath, node) defs << DefStatement.new(filepath, node) if node[0] == :def node.each do |subnode| parse(filepath, subnode) if subnode.is_a?(Array) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 117

Slide 117 text

require 'ripper' class DefList class DefStatement ... end ... def read(filepath) parse(filepath, Ripper.sexp(File.read(filepath))) end private def parse(filepath, node) defs << DefStatement.new(filepath, node) if node[0] == :def node.each do |subnode| parse(filepath, subnode) if subnode.is_a?(Array) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 118

Slide 118 text

require 'ripper' class DefList class DefStatement ... end ... def read(filepath) parse(filepath, Ripper.sexp(File.read(filepath))) end private def parse(filepath, node) defs << DefStatement.new(filepath, node) if node[0] == :def node.each do |subnode| parse(filepath, subnode) if subnode.is_a?(Array) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 119

Slide 119 text

require 'ripper' class DefList class DefStatement ... end ... def read(filepath) parse(filepath, Ripper.sexp(File.read(filepath))) end private def parse(filepath, node) defs << DefStatement.new(filepath, node) if node[0] == :def node.each do |subnode| parse(filepath, subnode) if subnode.is_a?(Array) end end end Static Analysis • Reflection Tests • Strategic Investment

Slide 120

Slide 120 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 121

Slide 121 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 122

Slide 122 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 123

Slide 123 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 124

Slide 124 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 125

Slide 125 text

Other reflection tests Static Analysis • Reflection Tests • Strategic Investment

Slide 126

Slide 126 text

No multi-line scopes without using a query object Other reflection tests Static Analysis • Reflection Tests • Strategic Investment

Slide 127

Slide 127 text

No multi-line scopes without using a query object No associations without automatic inverses or inverse_of specified Other reflection tests Static Analysis • Reflection Tests • Strategic Investment

Slide 128

Slide 128 text

No multi-line scopes without using a query object No associations without automatic inverses or inverse_of specified No if statements Other reflection tests Static Analysis • Reflection Tests • Strategic Investment

Slide 129

Slide 129 text

This is not heresy. Static Analysis • Reflection Tests • Strategic Investment

Slide 130

Slide 130 text

It is heretical to expect code quality to be maintained exclusively through code review. Static Analysis • Reflection Tests • Strategic Investment

Slide 131

Slide 131 text

Strategic Investment Static Analysis • Reflection Tests • Strategic Investment Enforce standards on old code

Slide 132

Slide 132 text

Passing a linter
 is not a measure of code quality. Static Analysis • Reflection Tests • Strategic Investment

Slide 133

Slide 133 text

Passing tests
 is not a measure of code quality. Static Analysis • Reflection Tests • Strategic Investment

Slide 134

Slide 134 text

Using design patterns
 is not a measure of code quality. Static Analysis • Reflection Tests • Strategic Investment

Slide 135

Slide 135 text

Code is written by the first engineer that has to understand the problem. Static Analysis • Reflection Tests • Strategic Investment

Slide 136

Slide 136 text

Code is read by every engineer that has to understand the problem. Static Analysis • Reflection Tests • Strategic Investment

Slide 137

Slide 137 text

Readability is the measure of the quality of the code that is going to change. Static Analysis • Reflection Tests • Strategic Investment

Slide 138

Slide 138 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 139

Slide 139 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase LEGACY = File.readlines('legacy.txt').map(&:chomp).freeze test 'no defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) - LEGACY assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 140

Slide 140 text

Static Analysis • Reflection Tests • Strategic Investment require 'test_helper' class DefTest < ActiveSupport::TestCase LEGACY = File.readlines('legacy.txt').map(&:chomp).freeze test 'no more defs in models' do def_list = DefList.from(Dir['app/models/*.rb']) violations = def_list.defs.map(&:to_s) - LEGACY assert_empty violations, <<~MSG Expected #{methods} to be empty. It looks like you defined an instance method on an ActiveRecord model. We prefer to leave AR model declarations to contain only database-level logic (like scopes and associations) and to instead place other logic in delegated objects. Check out `Ext::Factory` and `Ext::Query` for examples. MSG end end

Slide 141

Slide 141 text

When code is readable… Static Analysis • Reflection Tests • Strategic Investment

Slide 142

Slide 142 text

features are shipped more quickly When code is readable… Static Analysis • Reflection Tests • Strategic Investment

Slide 143

Slide 143 text

features are shipped more quickly engineers can onboard more quickly When code is readable… Static Analysis • Reflection Tests • Strategic Investment

Slide 144

Slide 144 text

features are shipped more quickly engineers can onboard more quickly bus factor is reduced When code is readable… Static Analysis • Reflection Tests • Strategic Investment

Slide 145

Slide 145 text

features are shipped more quickly engineers can onboard more quickly bus factor is reduced patterns become self-evident When code is readable… Static Analysis • Reflection Tests • Strategic Investment

Slide 146

Slide 146 text

features are shipped more quickly engineers can onboard more quickly bus factor is reduced patterns become self-evident writing tests is easier When code is readable… Static Analysis • Reflection Tests • Strategic Investment

Slide 147

Slide 147 text

Elegance is not a dispensable luxury but a factor that decides between success and failure. - Edsger Dijkstra Static Analysis • Reflection Tests • Strategic Investment

Slide 148

Slide 148 text

Object-oriented programming is an exceptionally bad idea which could only have originated in California. - Edsger Dijkstra Static Analysis • Reflection Tests • Strategic Investment

Slide 149

Slide 149 text

Static Analysis
 Determine the standards Reflection Tests
 Enforce standards on new code Strategic Investment
 Enforce standards on old code Solutions Static Analysis • Reflection Tests • Strategic Investment

Slide 150

Slide 150 text

Static Analysis
 Determine the standards for future code Reflection Tests
 Enforce standards on new code Strategic Investment
 Enforce standards on old code Solutions Static Analysis • Reflection Tests • Strategic Investment

Slide 151

Slide 151 text

Static Analysis
 Determine the standards for future code Reflection Tests
 Enforce standards on new code as it changes Strategic Investment
 Enforce standards on old code Solutions Static Analysis • Reflection Tests • Strategic Investment

Slide 152

Slide 152 text

Static Analysis
 Determine the standards for future code Reflection Tests
 Enforce standards on new code as it changes Strategic Investment
 Enforce standards on old code if it needs to change Solutions Static Analysis • Reflection Tests • Strategic Investment

Slide 153

Slide 153 text

Grow a bonsai, not a shrub github.com/kddeisz/bonsai Kevin Deisz @kddeisz