Share
Embed
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
Transcript
149 slides
-
The Scandalous Story of the Dreadful Code Written by the
Best of Us Katrina Owen ! GitHub -
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... } -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
no-op #2 package comms func Broadcast(s string, f game.Freq, c
Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) } -
no-op #2 package comms func Broadcast(s string, f game.Freq, c
Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) } -
no-op #2 package comms func Broadcast(s string, f game.Freq, c
Crypto) { // ... a mountain of complexity... Sig.Broadcast(s, f, c) } -
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) { } -
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) { } -
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... } -
no-op #3 package notify func ResourceDepleted(r game.Resource, p *game.Person) {
// ... comms.Broadcast(...) // ... } -
no-op #3 package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p
*game.Person ) { // ... comms.Broadcast(...) // ... } -
no-op #3 package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p
*game.Person ) { // ... comms.Broadcast(...) // ... } -
package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p *game.Person )
{ // ... com.Broadcast(...) // ... } no-op #3 -
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 -
package comms type Transmitter interface { Broadcast(string, game.Freq, Crypto) Dispatch(string,
*game.Person) Register(game.Event, *game.Person) } interface -
interface package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p *game.Person
) { // ... com.Broadcast(...) // ... } -
interface package notify func ResourceDepleted(com *comms.Signal, r game.Resource, p *game.Person
) { // ... com.Broadcast(...) // ... } -
interface package notify func ResourceDepleted(com comms.Transmitter, r game.Resource, p *game.Person
) { // ... com.Broadcast(...) // ... } -
interface package notify type fakeTransmitter struct { broadcasts []fakeBroadcast dispatches
[]fakeDispatch events []fakeEvent } -
package notify type fakeTransmitter struct { broadcasts []fakeBroadcast dispatches []fakeDispatch
events []fakeEvent } func (*fakeTransmitter) Broadcast(...) { } func (*fakeTransmitter) Dispatch(...) { } func (*fakeTransmitter) Register(...) { } interface -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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, ) -
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, ) -
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, ) -
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, ) -
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, ) -
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, ) -
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, ) -
text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,
v3, ) html := fmt.Sprintf( "%s discovered %s at %s.", anchor1, anchor2, anchor3, ) -
text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,
v3, ) html := fmt.Sprintf( "%s discovered %s at %s.", anchor1, anchor2, anchor3, ) -
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()), ) -
text := fmt.Sprintf( "%s discovered %s at %s.", v1, v2,
v3, ) html := fmt.Sprintf( "%s discovered %s at %s.", anchor1, anchor2, anchor3, ) -
msg := "%s discovered %s at %s." text := fmt.Sprintf(
msg, v1, v2, v3, ) html := fmt.Sprintf( msg, anchor1, anchor2, anchor3, ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
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()), ) -
func link(l linker) string { return fmt.Sprintf( "<a href='%s'>%s</a>", l.URL(),
template.HTMLEscapeString(l.String()), ) } -
func link(l linker) string { return fmt.Sprintf( "<a href='%s'>%s</a>", l.URL(),
template.HTMLEscapeString(l.String()), ) } -
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), ) -
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), ) -
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), ) -
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), ) -
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), ) -
msg := "%s discovered %s at %s." text := fmt.Sprintf(
msg, p, r, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), ) -
msg := "%s discovered %s at %s." text := fmt.Sprintf(
msg, p, r, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), ) -
msg := "%s discovered %s at %s." text := fmt.Sprintf(
msg, p, r, loc, ) html := fmt.Sprintf( msg, link(p), link(r), link(loc), ) -
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), ) -
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), ) -
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), ) -
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()), ) } -
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...) } -
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...) } -
msg := "%s discovered %s at %s." text := fmt(msg,
text, p, r, loc) html := fmt(msg, link, p, r, loc) -
type message struct { template string vars []linker } func
newMessage(template string, vars ...linker) message { return message{ template: template, vars: vars, } } -
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...) } -
func (msg message) text() string { return msg.fmt(text) } func
(msg message) html() string { return msg.fmt(link) } -
s := "%s discovered %s at %s." msg := newMessage(s,
p, r, loc) // ... msg.text() // ... msg.html() -
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()), ) -
type message struct { template string vars []linker } func
newMessage(template string, vars ...linker) message { return message{ template: template, vars: vars, } } -
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...) } -
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() }