Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Scandalous Story of the Dreadful Code Written by the Best of Us

Katrina Owen
October 14, 2016
4k

The Scandalous Story of the Dreadful Code Written by the Best of Us

There are overlooked corners of our codebases. Ignored, unloved. Unimportant. Or so we thought.

What once was inconsequential has—somehow—grown into ghastly mess.

This talk tells the story of one such mess, and the taming of it.

Katrina Owen

October 14, 2016
Tweet

Transcript

  1. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    // ... a mountain of complexity... } func Dispatch(s string, p *game.Person) { // ... horrible things... } func Register(e game.Event, p *game.Person) { // ... kind of confusing stuff... }
  2. \o/

  3. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    // ... a mountain of complexity... } func Dispatch(s string, p *game.Person) { // ... horrible things... } func Register(e game.Event, p *game.Person) { // ... kind of confusing stuff... } no-op #1
  4. package comms type Signal struct{} func (*Signal) Broadcast(s string, f

    game.Freq, c Crypto) { } func (*Signal) Dispatch(s string, p *game.Person) { } func (*Signal) Register(e game.Event, p *game.Person) { } no-op #1
  5. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    // ... a mountain of complexity... } func Dispatch(s string, p *game.Person) { // ... horrible things... } func Register(e game.Event, p *game.Person) { // ... kind of confusing stuff... } no-op #1
  6. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    // ... a mountain of complexity... } func Dispatch(s string, p *game.Person) { // ... horrible things... } func Register(e game.Event, p *game.Person) { // ... kind of confusing stuff... } no-op #1
  7. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    // ... a mountain of complexity... Sig.Broadcast(s, f, c) } func Dispatch(s string, p *game.Person) { // ... horrible things... Sig.Dispatch(s, p) } func Register(e game.Event, p *game.Person) { // ... kind of confusing stuff... Sig.Register(e, p) } no-op #1
  8. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    // ... a mountain of complexity... Sig.Broadcast(s, f, c) } func Dispatch(s string, p *game.Person) { // ... horrible things... Sig.Dispatch(s, p) } func Register(e game.Event, p *game.Person) { // ... kind of confusing stuff... Sig.Register(e, p) } no-op #1
  9. package comms var Sig *Signal func (*Signal) Broadcast(s string, f

    game.Freq, c Crypto) { } func (*Signal) Dispatch(s string, p *game.Person) { } func (*Signal) Register(e game.Event, p *game.Person) { } no-op #1
  10. package comms var Sig *Signal func (*Signal) Broadcast(s string, f

    game.Freq, c Crypto) { } func (*Signal) Dispatch(s string, p *game.Person) { } func (*Signal) Register(e game.Event, p *game.Person) { } no-op #1
  11. package comms var Sig *Signal func (*Signal) Broadcast(s string, f

    game.Freq, c Crypto) { } func (*Signal) Dispatch(s string, p *game.Person) { } func (*Signal) Register(e game.Event, p *game.Person) { } no-op #1
  12. no-op #2 package comms func Broadcast(s string, f game.Freq, c

    Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) }
  13. no-op #2 package comms func Broadcast(s string, f game.Freq, c

    Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) }
  14. no-op #2 package comms func Broadcast(s string, f game.Freq, c

    Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) }
  15. no-op #2 package comms func Broadcast(s string, f game.Freq, c

    Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) } func (*Signal) Broadcast(s string, f game.Freq, c Crypto) { }
  16. no-op #2 package comms func Broadcast(s string, f game.Freq, c

    Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) } func (*Signal) Broadcast(s string, f game.Freq, c Crypto) { }
  17. no-op #2 package comms func Broadcast(s string, f game.Freq, c

    Crypto) { Sig.Broadcast(s, f, c) } func (*Signal) Broadcast(s string, f game.Freq, c Crypto) { // ... a mountain of complexity... }
  18. no-op #3 package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p

    *game.Person ) { // ... comms.Broadcast(...) // ... }
  19. no-op #3 package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p

    *game.Person ) { // ... comms.Broadcast(...) // ... }
  20. package comms func Broadcast(s string, f game.Freq, c Crypto) {

    Sig.Broadcast(s, f, c) } func Dispatch(s string, p *game.Person) { Sig.Dispatch(s, p) } func Register(e game.Event, p *game.Person) { Sig.Register(e, p) } no-op #3
  21. package notify type fakeTransmitter struct { broadcasts []fakeBroadcast dispatches []fakeDispatch

    events []fakeEvent } func (*fakeTransmitter) Broadcast(...) { } func (*fakeTransmitter) Dispatch(...) { } func (*fakeTransmitter) Register(...) { } interface
  22. html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a>"+ "

    at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  23. html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a>"+ "

    at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  24. html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a>"+ "

    at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  25. text := fmt.Sprintf( "%s discovered %s at %s.", p.Name, r.Name,

    loc, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  26. text := fmt.Sprintf( "%s discovered %s at %s.", p.Name, r.Name,

    loc, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  27. text := fmt.Sprintf( "%s discovered %s at %s.", p.Name, r.Name,

    loc, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  28. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  29. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  30. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  31. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  32. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  33. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%s&lon=%s'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  34. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%s&lon=%s'>%s</a>.", v4, v5, v6, v7, v8, v9, v10, v11, )
  35. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "%s discovered %s at %s.", anchor1, anchor2, anchor3, )
  36. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "%s discovered %s at %s.", anchor1, anchor2, anchor3, )
  37. anchor1 := fmt.Sprintf( "<a href='/people/%s'>%s</a>", p.Handle, template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='/tribes/%s/resources/%s'>%s</a>", r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='/maps?lat=%f&lon=%f'>%s</a>", loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  38. text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,

    v3, ) html := fmt.Sprintf( "%s discovered %s at %s.", anchor1, anchor2, anchor3, )
  39. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, v1, v2, v3, ) html := fmt.Sprintf( msg, anchor1, anchor2, anchor3, )
  40. anchor1 := fmt.Sprintf( "<a href='/people/%s'>%s</a>", p.Handle, template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='/tribes/%s/resources/%s'>%s</a>", r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='/maps?lat=%f&lon=%f'>%s</a>", loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  41. anchor1 := fmt.Sprintf( "<a href='/people/%s'>%s</a>", p.Handle, template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='/tribes/%s/resources/%s'>%s</a>", r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='/maps?lat=%f&lon=%f'>%s</a>", loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  42. anchor1 := fmt.Sprintf( "<a href='/people/%s'>%s</a>", p.Handle, template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='/tribes/%s/resources/%s'>%s</a>", r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='/maps?lat=%f&lon=%f'>%s</a>", loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  43. anchor1 := fmt.Sprintf( "<a href='/people/%s'>%s</a>", p.Handle, template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='/tribes/%s/resources/%s'>%s</a>", r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='/maps?lat=%f&lon=%f'>%s</a>", loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  44. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  45. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  46. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  47. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.Name), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.Name), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  48. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.String()), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.String()), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  49. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.String()), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.String()), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  50. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.String()), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.String()), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  51. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.String()), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.String()), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  52. anchor1 := fmt.Sprintf( "<a href='%s'>%s</a>", p.URL(), template.HTMLEscapeString(p.String()), ) anchor2 :=

    fmt.Sprintf( "<a href='%s'>%s</a>", r.URL(), template.HTMLEscapeString(r.String()), ) anchor3 := fmt.Sprintf( "<a href='%s'>%s</a>", loc.URL(), template.HTMLEscapeString(loc.String()), )
  53. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p.Name, r.Name, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  54. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p.Name, r.Name, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  55. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p.Name, r.Name, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  56. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p.Name, r.Name, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  57. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p.Name, r.Name, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  58. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p, r, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  59. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p, r, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  60. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, p, r, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  61. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, text(p), text(r), text(loc), ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  62. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, text(p), text(r), text(loc), ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  63. msg := "%s discovered %s at %s." text := fmt.Sprintf(

    msg, text(p), text(r), text(loc), ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), )
  64. func text(l linker) string { return l.String() } func link(l

    linker) string { return fmt.Sprintf( "<a href='%s'>%s</a>", l.URL(), template.HTMLEscapeString(l.String()), ) }
  65. func format(msg string, fn func(linker) str... args := make([]interface{}, len(values))

    for i, v := range values { args[i] = fn(v) } return fmt.Sprintf(msg, args...) }
  66. func format(msg string, fn func(linker) str... args := make([]interface{}, len(values))

    for i, v := range values { args[i] = fn(v) } return fmt.Sprintf(msg, args...) }
  67. msg := "%s discovered %s at %s." text := fmt(msg,

    text, p, r, loc) html := fmt(msg, link, p, r, loc)
  68. type message struct { template string vars []linker } func

    newMessage(template string, vars ...linker) message { return message{ template: template, vars: vars, } }
  69. func (msg message) fmt(fn func(linker) string) string { args :=

    make([]interface{}, len(msg.vars)) for i, v := range msg.vars { args[i] = fn(v) } return fmt.Sprintf(msg.template, args...) }
  70. func (msg message) text() string { return msg.fmt(text) } func

    (msg message) html() string { return msg.fmt(link) }
  71. s := "%s discovered %s at %s." msg := newMessage(s,

    p, r, loc) // ... msg.text() // ... msg.html()
  72. text := fmt.Sprintf( "%s discovered %s at %s.", p.Name, r.Name,

    loc, ) html := fmt.Sprintf( "<a href='/people/%s'>%s</a> discovered "+ "<a href='/tribes/%s/resources/%s'>%s</a> "+ "at <a href='/maps?lat=%f&lon=%f'>%s</a>.", p.Handle, template.HTMLEscapeString(p.Name), r.TribeUUID(), r.UUID, template.HTMLEscapeString(r.Name), loc.Latitude, loc.Longitude, template.HTMLEscapeString(loc.String()), )
  73. type message struct { template string vars []linker } func

    newMessage(template string, vars ...linker) message { return message{ template: template, vars: vars, } }
  74. type message struct { template string vars []linker } func

    (msg message) text() string { return msg.fmt(text) } func (msg message) html() string { return msg.fmt(link) } func (msg message) fmt(fn func(linker) string) string { args := make([]interface{}, len(msg.vars)) for i, v := range msg.vars { args[i] = fn(v) } return fmt.Sprintf(msg.template, args...) }
  75. type linker interface { String() string URL() string } func

    link(l linker) string { return fmt.Sprintf( "<a href='%s'>%s</a>", l.URL(), template.HTMLEscapeString(l.String()), ) } func text(l linker) string { return l.String() }