software engineering best practices @ nylas

24
Soware Engineering Best Practices Ben Gotow (@bengotow)

Upload: ben-gotow

Post on 14-Apr-2017

67 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Software Engineering Best Practices @ Nylas

Software Engineering Best Practices

Ben Gotow (@bengotow)

Page 2: Software Engineering Best Practices @ Nylas

Computer Science ≠ Software Engineering• Clean

• Predictable

• Maintainable

• Extensible

• Fast & Efficient

• Mathematically Optimal

• Uses latest language features

Page 3: Software Engineering Best Practices @ Nylas

Essentials• The Mafia Model

• Design Patterns

• Building a Shared Language

• Exceptions aren’t evil

• Anti-patterns

Page 4: Software Engineering Best Practices @ Nylas
Page 5: Software Engineering Best Practices @ Nylas
Page 6: Software Engineering Best Practices @ Nylas

“Design software like you’re running a mafia.”

Page 7: Software Engineering Best Practices @ Nylas

• Has one well defined job. Doesn’t ask questions.

• Has limited knowledge of the rest of the operation. Others don’t know or care how he gets the job done.

• Has no secret dealings with others, clean cut actor.

• Can be replaced—others could do this specific job if he was “removed.”

Page 8: Software Engineering Best Practices @ Nylas

• Single Responsibility Principle (Robert C. Martin)

• Separation of Concerns (Dijkstra)

• Avoid Side Effects

• “Program to an interface, not an implementation.” (Erich Gamma)

Page 9: Software Engineering Best Practices @ Nylas
Page 10: Software Engineering Best Practices @ Nylas

Design Patterns

Patterns make it easy to communicate abstract ideas about software.

Page 11: Software Engineering Best Practices @ Nylas

Design PatternsCommand EmitterSingleton

Page 12: Software Engineering Best Practices @ Nylas

Design PatternsCommand

Encapsulate a request as an object, thereby letting users parameterize clients with different requests, queue or log

requests, and support undoable operations.

(Tasks in N1 are an implementation of the Command pattern.)

Page 13: Software Engineering Best Practices @ Nylas

Anti-Patterns (“Code Smells”)- Copying code into more than three places

- Calling private methods on other classes

- Inspecting state which was not designed to be observed

- Waiting for something to happen using the wall clock

setTimeout I’m looking at you.😒

Page 14: Software Engineering Best Practices @ Nylas

Anti-Patterns (“Code Smells”)Ex: Adding optional parameters that change the behavior of existing code slightly. “Don’t do this, this one time.”

thread.save({tellUser: true}) thread.save().then(() => { this.tellUser();});

Take a break, think about refactoring, explain the problem to someone else.

Page 15: Software Engineering Best Practices @ Nylas

Naming Conventions

Naming things is hard, and really important when building software on a team.

Page 16: Software Engineering Best Practices @ Nylas

• Functions that return a value should indicate how costly that value is to retrieve:

thread() // easygetThread() // hmm, probably not O(1)fetchThread() // better cache the result of this!

Naming Conventions

Page 17: Software Engineering Best Practices @ Nylas

• Names should reflect intended use and give you an idea what is returned:

onClicked() // called in response to an eventnewWindow() // always returns a new objectisSending() // always returns a booleanensureReady() // may or may not do anything

• Long names are almost always worth it:finalizeAndPersistNewMessage() // shit is going down

Naming Conventions

Page 18: Software Engineering Best Practices @ Nylas

• Functions with many parameters should use named hashes: _onComposeReply: ({thread, message, popout, behavior}) =>

• Leading (or trailing) underscores denote private members:_doInternalFileWrite myInstanceVar_

this._onComposeReply({ thread: A, behavior: reply-all, popout: true })

Naming Conventions

Page 19: Software Engineering Best Practices @ Nylas

Exceptions aren’t EvilThrow exceptions aggressively to protect the code you write from things it wasn’t intended for.

• If your code makes assumptions, make assertions.

• If you ever have the option of crashing now, or /probably/ crashing later downstream, crash now.

Page 20: Software Engineering Best Practices @ Nylas

Exceptions aren’t Evil

😍

Page 21: Software Engineering Best Practices @ Nylas

Code Review

https://paper.dropbox.com/doc/Code-Reviews-using-Phabricator-LdupUMb1X9SWMyrShmhQB

Write better code, learn new tricks, avoid shipping mistakes, develop a shared understanding of the codebase.

Page 22: Software Engineering Best Practices @ Nylas

Code ReviewCode Level:

• Clarity • Function / variable naming • Test coverage

Architectural Level:• Does this fit performance requirements? • Does this follow conventions and patterns used elsewhere? • Are we comfortable with the limitations of this approach? • Are changes well contained?

Page 23: Software Engineering Best Practices @ Nylas

Further Reading- Design Patterns: Elements of Reusable Object-Oriented

Software (Gang of Four)

- Clean Code: A Handbook of Agile Software Craftmanship (Bob Martin)

- https://sourcemaking.com/design_patterns

- http://en.clouddesignpattern.org