Slide 1

Slide 1 text

@jmmastey | http://bit.ly/2rw1W8I "O0QUJNJTUJD1SPQPTBMGPS .BLJOH)PSSJCMF$PEF #FBSBCMF Joe Mastey Nerd Interface June 2017

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

@jmmastey | http://bit.ly/2rw1W8I • Custom system packages (my-libssl) • 6000+ line User model • Four days to run test suite, with 10% flapping tests. • 1,000,000+ lines of ruby

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

@jmmastey | http://bit.ly/2rw1W8I XFDBO`UBMM CFIJQ

Slide 7

Slide 7 text

@jmmastey | http://bit.ly/2rw1W8I PVSPXOWJDUJNT

Slide 8

Slide 8 text

@jmmastey | http://bit.ly/2rw1W8I CVUXIZ

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

@jmmastey | http://bit.ly/2rw1W8I XFDBO`UUFMMXIBUDPEF JTFWFOJOVTFBOZNPSF

Slide 11

Slide 11 text

@jmmastey | http://bit.ly/2rw1W8I DIBOHJOHDPEFCSFBLT DPNQMFUFMZVOSFMBUFEUFTUT

Slide 12

Slide 12 text

@jmmastey | http://bit.ly/2rw1W8I HPPEMVDLNFSHJOHZPVS DPEFTUZMF13T

Slide 13

Slide 13 text

@jmmastey | http://bit.ly/2rw1W8I 69151 offenses detected

Slide 14

Slide 14 text

@jmmastey | http://bit.ly/2rw1W8I XFDBO`UUSVTUUIF UFTUTVJUF

Slide 15

Slide 15 text

@jmmastey | http://bit.ly/2rw1W8I TPXIBUEPUIFZEP JOTUFBE

Slide 16

Slide 16 text

@jmmastey | http://bit.ly/2rw1W8I NBLFBUJOZDIBOHF

Slide 17

Slide 17 text

@jmmastey | http://bit.ly/2rw1W8I DPNNJUJU TLJQUIFUFTUT

Slide 18

Slide 18 text

@jmmastey | http://bit.ly/2rw1W8I SVOMJLFIFMM

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

@jmmastey | http://bit.ly/2rw1W8I MFBSOFEIFMQMFTTOFTT

Slide 21

Slide 21 text

:PV"SF)FSF 5IF5SPVHIPG%FTQBJS

Slide 22

Slide 22 text

@jmmastey | http://bit.ly/2rw1W8I TUFQ OBNFUIFFWJM

Slide 23

Slide 23 text

@jmmastey | http://bit.ly/2rw1W8I CBEOFTTJTVOFWFOMZ EJTUSJCVUFE

Slide 24

Slide 24 text

@jmmastey | http://bit.ly/2rw1W8I wc -l $(find . -name *.rb) | sort

Slide 25

Slide 25 text

@jmmastey | http://bit.ly/2rw1W8I … 220 other files here 339 ./lib/devise/models/confirmable.rb 347 ./test/integration/recoverable_test.rb 350 ./test/models/lockable_test.rb 363 ./test/integration/registerable_test.rb 499 ./lib/devise.rb 513 ./lib/devise/rails/routes.rb 519 ./test/models/confirmable_test.rb 698 ./test/integration/authenticatable_test.rb 16262 total

Slide 26

Slide 26 text

Badness Number of Files

Slide 27

Slide 27 text

@jmmastey | http://bit.ly/2rw1W8I bundle exec rake stats

Slide 28

Slide 28 text

@jmmastey | http://bit.ly/2rw1W8I +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 3347 | 2570 | 71 | 310 | 4 | 6 | | Helpers | 432 | 347 | 0 | 49 | 0 | 5 | | Models | 1957 | 1451 | 51 | 200 | 3 | 5 | | Mailers | 85 | 71 | 3 | 9 | 3 | 5 | | Javascripts | 9344 | 6447 | 0 | 724 | 0 | 6 | | Libraries | 2196 | 1532 | 27 | 143 | 5 | 8 | | Controller specs | 1675 | 1237 | 0 | 5 | 0 | 245 | | … | View specs | 283 | 236 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 24956 | 18451 | 153 | 1449 | 9 | 10 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 12418 Test LOC: 6033 Code to Test Ratio: 1:0.5

Slide 29

Slide 29 text

@jmmastey | http://bit.ly/2rw1W8I +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 3347 | 2570 | 71 | 310 | 4 | 6 | | Helpers | 432 | 347 | 0 | 49 | 0 | 5 | | Models | 1957 | 1451 | 51 | 200 | 3 | 5 | | Mailers | 85 | 71 | 3 | 9 | 3 | 5 | | Javascripts | 9344 | 6447 | 0 | 724 | 0 | 6 | | Libraries | 2196 | 1532 | 27 | 143 | 5 | 8 | | Controller specs | 1675 | 1237 | 0 | 5 | 0 | 245 | | … | View specs | 283 | 236 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 24956 | 18451 | 153 | 1449 | 9 | 10 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 12418 Test LOC: 6033 Code to Test Ratio: 1:0.5

Slide 30

Slide 30 text

@jmmastey | http://bit.ly/2rw1W8I +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 3347 | 2570 | 71 | 310 | 4 | 6 | | Helpers | 432 | 347 | 0 | 49 | 0 | 5 | | Models | 1957 | 1451 | 51 | 200 | 3 | 5 | | Mailers | 85 | 71 | 3 | 9 | 3 | 5 | | Javascripts | 9344 | 6447 | 0 | 724 | 0 | 6 | | Libraries | 2196 | 1532 | 27 | 143 | 5 | 8 | | Controller specs | 1675 | 1237 | 0 | 5 | 0 | 245 | | … | View specs | 283 | 236 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 24956 | 18451 | 153 | 1449 | 9 | 10 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 12418 Test LOC: 6033 Code to Test Ratio: 1:0.5

Slide 31

Slide 31 text

@jmmastey | http://bit.ly/2rw1W8I PSNBLFZPVSPXO

Slide 32

Slide 32 text

@jmmastey | http://bit.ly/2rw1W8I def methods_per_activerecord_class children = ObjectSpace.each_object(Class) .select { |klass| klass < ActiveRecord::Base } sorted = children.sort_by { |klass| klass.methods(false).count } sorted.reverse! sorted.map { |klass| "#{klass.methods(false).length} #{klass}" } end

Slide 33

Slide 33 text

@jmmastey | http://bit.ly/2rw1W8I 75 Business, 74 User, 59 Product, 53 Group, 48 JournalEntry, 37 Notification, 30 Order, 30 Subscription, 22 Audited::Audit, 12 CombinedPurchaseRecord, 8 PgSearch::Document

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

TUFQ TUPQUIF CMFFEJOH

Slide 36

Slide 36 text

6 *`NCBDL

Slide 37

Slide 37 text

@jmmastey | http://bit.ly/2rw1W8I # arrowhead of dooooom Metrics/BlockNesting: Max: 4 # short methods, man... Metrics/MethodLength: Max: 414 Metrics/LineLength: Max: 324 # fewer parameters Metrics/ParameterLists: Max: 15

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

@jmmastey | http://bit.ly/2rw1W8I # ratchets/models_spec.rb ar_classes = ObjectSpace.each_object(Class) .select { |klass| klass < ApplicationRecord } METHOD_THRESHHOLD = 505 ar_classes.each do |klass| describe klass do it "shouldn't have more than #{METHOD_THRESHHOLD} methods" do expect(klass.methods(false).count).to be < METHOD_THRESHHOLD end end end

Slide 40

Slide 40 text

@jmmastey | http://bit.ly/2rw1W8I TUFQ JNQSPWF TUSBUFHJDBMMZ

Slide 41

Slide 41 text

@jmmastey | http://bit.ly/2rw1W8I pYJUJOQJFDFT

Slide 42

Slide 42 text

@jmmastey | http://bit.ly/2rw1W8I CSFBLBHFJT HPJOHUP IBQQFO

Slide 43

Slide 43 text

@jmmastey | http://bit.ly/2rw1W8I 1SJPSJUJ[F • make your tests better • use less magic • cut off the head • prioritize high churn code

Slide 44

Slide 44 text

@jmmastey | http://bit.ly/2rw1W8I GPPMQSPPGUFTUpY

Slide 45

Slide 45 text

@jmmastey | http://bit.ly/2rw1W8I EFMFUFUIFN

Slide 46

Slide 46 text

@jmmastey | http://bit.ly/2rw1W8I NBLFOFXUFTUTGBTU

Slide 47

Slide 47 text

@jmmastey | http://bit.ly/2rw1W8I # require 'rails_helper' require 'spec_helper'

Slide 48

Slide 48 text

@jmmastey | http://bit.ly/2rw1W8I VTFMFTTNBHJD

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

@jmmastey | http://bit.ly/2rw1W8I strategy = user.payroll_strategy.constantize strategy.new(user, params) strategy.dispatch

Slide 51

Slide 51 text

@jmmastey | http://bit.ly/2rw1W8I next_state = purchase_lifecycle.next_state purchase.send(:”mark_as_#{next_state}!")

Slide 52

Slide 52 text

@jmmastey | http://bit.ly/2rw1W8I define_method constantize send method_missing eval (?!)

Slide 53

Slide 53 text

@jmmastey | http://bit.ly/2rw1W8I strategy = case user.payroll_strategy when 'biweekly' then BiweeklyPayrollStrategy when 'fortnightly' then FortnightlyPayrollStrategy when 'monthly' then MonthlyPayrollStrategy end strategy.new(user, params) strategy.dispatch

Slide 54

Slide 54 text

@jmmastey | http://bit.ly/2rw1W8I DVUPGGUIFIFBE

Slide 55

Slide 55 text

Badness Number of Files

Slide 56

Slide 56 text

@jmmastey | http://bit.ly/2rw1W8I # POST /api/users/create_from_facebook def create_from_facebook service = RegistersUserFromFacebook.new(params[:authentication_token]) service.call @user = service.user if service.errors.empty? render status: :created, location: logged_in_profile_url else render json: { message: service.message, errors: service.errors }, status: :unprocessable_entity end end

Slide 57

Slide 57 text

@jmmastey | http://bit.ly/2rw1W8I class RegistersUserFromFacebook attr_reader :token, :user, :errors, :message def initialize(token) @token = token @errors = [] end def call # 200 lines of doom here end end

Slide 58

Slide 58 text

@jmmastey | http://bit.ly/2rw1W8I XBJU XUG

Slide 59

Slide 59 text

DIVSO

Slide 60

Slide 60 text

@jmmastey | http://bit.ly/2rw1W8I 69151 offenses detected

Slide 61

Slide 61 text

@jmmastey | http://bit.ly/2rw1W8I 1 file inspected, 36 offenses detected

Slide 62

Slide 62 text

@jmmastey | http://bit.ly/2rw1W8I OBNFUIFFWJM TUPQUIFCMFFEJOH TUSBUFHJDBMMZJNQSPWF

Slide 63

Slide 63 text

@jmmastey | http://bit.ly/2rw1W8I GPDVTPOUIFQSPDFTT

Slide 64

Slide 64 text

@jmmastey | http://bit.ly/2rw1W8I

Slide 65

Slide 65 text

@jmmastey | http://bit.ly/2rw1W8I 0UIFS3FTPVSDFT • Refactoring Ruby (Jay Fields) • Why Programs Fail: A Guide to Systematic Debugging (Andreas Zeller) • Service Objects in Ruby • Gems: • rubocop / hound ci • rspec-activerecord-formatter • bundler-stats UIBOLT