Top Banner
Thinking with a Clean Mind Mitchell Matthews 1
45

Thinking with a Clean Mind

Aug 06, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Thinking with a Clean Mind

1

Thinking with a Clean MindMitchell Matthews

Page 2: Thinking with a Clean Mind

2

Wait Until the End For Photos

There will be a QR code and links to download this presentation

SuprTEK Advanced Technology Group

Page 3: Thinking with a Clean Mind

3

Contents of This Presentation

Why you should care about clean code

What clean code is

How to write clean code

Clean development

Integrated development environments and source control

Misconceptions and mistakes

Becoming proficient at writing clean code

SuprTEK Advanced Technology Group

Page 4: Thinking with a Clean Mind

4

Why You Should Care

SuprTEK Advanced Technology Group

Page 5: Thinking with a Clean Mind

5

Maintainability

If you can’t understand your own code, you can’t maintain it

This includes... Adding new features

Fixing bugs

Reuse

SuprTEK Advanced Technology Group

Page 6: Thinking with a Clean Mind

6

Productivity

Find bugs faster

Fix bugs faster

Write fewer bugs

Add features faster

SuprTEK Advanced Technology Group

Page 7: Thinking with a Clean Mind

7

Teamwork

If you can’t understand your own code others certainly won’t

As a software developer, you will work in teams

Your team needs to be able to work with your code

SuprTEK Advanced Technology Group

Page 8: Thinking with a Clean Mind

8

What Clean Code Is

SuprTEK Advanced Technology Group

Page 9: Thinking with a Clean Mind

9

Clean Code...

Is easy to understand

Is easy to modify

Is easy to test

Works correctly

SuprTEK Advanced Technology Group

Page 10: Thinking with a Clean Mind

10

Writing Clean Code

SuprTEK Advanced Technology Group

Page 11: Thinking with a Clean Mind

11

Code Style and Formatting

White space is important; use it carefully and deliberately

Follow your language’s conventions

Stay consistent

Be verbose

SuprTEK Advanced Technology Group

Page 12: Thinking with a Clean Mind

12

Avoid:

a = 'acgatagc'

b = len(a) - 2

d = ""

for e in range(0,f,3):

g = a[e:e+3]

h = i.get(g.upper(), 'X')

d = d + hCode courtesy of http://pythonforbiologists.com/index.php/bad-variable-and-function-names/SuprTEK Advanced Technology Group

Page 13: Thinking with a Clean Mind

13

Do:

dna = 'acgatagc'

last_codon_start = len(dna) - 2

protein = ""

for codon_start in range(0,last_codon_start,3):

codon = dna[codon_start:codon_start+3]

amino_acid = genetic_code.get(codon.upper(), 'X')

protein = protein + amino_acidCode courtesy of http://pythonforbiologists.com/index.php/bad-variable-and-function-names/SuprTEK Advanced Technology Group

Page 14: Thinking with a Clean Mind

14

Avoid:

o.get("rows").forEach(r -> { String rs = r.asText();

for (int i = 0; i < rs.length(); i++) {

m.s.t[i][y.v] = tm.get(rs.charAt(i));

if(m.s.t[i][y.value]==null){m.s.t[i][y.value]=ft.value;}

}y.value++;});

SuprTEK Advanced Technology Group

Page 15: Thinking with a Clean Mind

15

Do:

rootNode.get("rows").forEach(row -> {

String rowString = row.asText();

for (int x = 0; x < rowString.length(); x++) {

map.currentState.terrain[x][y.value] = terrainMap.get(rowString.charAt(x));

if (map.currentState.terrain[x][y.value] == null) {

map.currentState.terrain[x][y.value] = fillTerrain.value;

}

}

y.value++;

});SuprTEK Advanced Technology Group

Page 16: Thinking with a Clean Mind

16

Utilize Patterns

Patterns are reusable solutions to common problems

As with whitespace, use them only as needed and when it makes sense

Patterns are easy to over-use—be careful

Commonly used patterns include:

Command

Observer

Singleton

SuprTEK Advanced Technology Group

Page 17: Thinking with a Clean Mind

17

Refactor

Refactoring is rewriting code without changing functionality

Refactor early and as often as necessary

SuprTEK Advanced Technology Group

Page 18: Thinking with a Clean Mind

18

Documentation (Part 1)

Documentation provides a starting point for understanding code

Document everything as you go

Use built-in language features, such as JavaDocs

SuprTEK Advanced Technology Group

Page 19: Thinking with a Clean Mind

19

Clean Development

SuprTEK Advanced Technology Group

Page 20: Thinking with a Clean Mind

20

Make a Plan

Before writing any code, plan out your project

Planning presents a clear picture of what must be done

High level planning identifies potential problems early

Helps stay focused

SuprTEK Advanced Technology Group

Page 21: Thinking with a Clean Mind

21

Define Goals and Requirements

Create a list of goals for your code to accomplish

Detail the functionality of these goals

Prioritize

Keep it readable

Keep it reasonable

SuprTEK Advanced Technology Group

Page 22: Thinking with a Clean Mind

22

Pick Languages and Frameworks

Do your research; pick the best tools for the job

Consider:

Ease of use

Simplicity

Scalability

Extensibility

Sustainability

The best tools may be the ones you’re most familiar with

SuprTEK Advanced Technology Group

Page 23: Thinking with a Clean Mind

23

Prototype

Identify the bare minimum feature set

Implement an example or two of each core feature

Make changes as necessary; your design may need to change

Keep prototypes focused and simple

SuprTEK Advanced Technology Group

Page 24: Thinking with a Clean Mind

24

Test Your Development Environment

Use a clean machine; virtual machines are good for this

Set up a new environment and test building and releasing

Use multiple operating systems

Consider automating setup of development environments

SuprTEK Advanced Technology Group

Page 25: Thinking with a Clean Mind

25

Document (Part 2)

Record all the details of building and releasing in a readme or similar, including:Software and library versions

Compatible operating systems

Troubleshooting

Code style

Development environment setup

SuprTEK Advanced Technology Group

Page 26: Thinking with a Clean Mind

26

Integrated Development Environments

and Source Control

SuprTEK Advanced Technology Group

Page 27: Thinking with a Clean Mind

27

IDEs

Catch common mistakes

Refactoring tools

Code completion and auto-generation

Automated builds

SuprTEK Advanced Technology Group

Page 28: Thinking with a Clean Mind

28

Source Control

Software that:Tracks changes

Enables short and long-term undos

Creates backups

Enables synchronization

Provides branching/merging

SuprTEK Advanced Technology Group

Page 29: Thinking with a Clean Mind

29

Common Source Control Software

Git

Subversion

Mercurial

SuprTEK Advanced Technology Group

Page 30: Thinking with a Clean Mind

30

Share Your Work

Upload your work to sites such as GitHub or BitBucket

Free backups

Work from any machine

Familiarization with real-world tools

Creates a portfolio

SuprTEK Advanced Technology Group

Page 31: Thinking with a Clean Mind

31

Misconceptions and Mistakes

SuprTEK Advanced Technology Group

Page 32: Thinking with a Clean Mind

32

Over-Engineering

Save yourself time and effort by utilizing existing solutions Don’t reinvent the wheel

Don’t let ‘perfect’ get in the way of ‘good enough’

SuprTEK Advanced Technology Group

Page 33: Thinking with a Clean Mind

33

Showing Off

Don’t do it

Simplicity is key

Less efficient code that is readable is often preferred

Code can be optimized later if need be

SuprTEK Advanced Technology Group

Page 34: Thinking with a Clean Mind

34

Superfluous Commenting

Don’t say how or what; say why

Use comments sparingly

SuprTEK Advanced Technology Group

Page 35: Thinking with a Clean Mind

35

Avoid:

if (wantToDragZoom) {

// Just a plain old left-down initiates panning mode.

mouseHandlingMode = MouseHandlingMode.Zooming;

wantToDragZoom = false;

} else if (mouseButtonDown == MouseButton.Left && (Keyboard.Modifiers & ModifierKeys.Shift) == 0) {

// Just a plain old left-down initiates panning mode.

mouseHandlingMode = MouseHandlingMode.Panning;

} else if (mouseHandlingMode != MouseHandlingMode.None) {

// Capture the mouse so that we eventually receive the mouse up event.

//zoomWindow.CaptureMouse();

}

SuprTEK Advanced Technology Group

Page 36: Thinking with a Clean Mind

36

Avoid:

/* Check if Web Workers are supported */

function getWebWorkerSupport() {

return (typeof(Worker) !== "undefined") ? true:false;

}

SuprTEK Advanced Technology Group

Page 37: Thinking with a Clean Mind

37

Do:

// Suppresses default constructor, ensuring non-instantiability.

private Collections() {

}

SuprTEK Advanced Technology Group

Page 38: Thinking with a Clean Mind

38

Do:

/* We need the window to resize based on the size of its contents automatically.

JavaFX doesn't let children of Region/Pane/Control nodes dictate their preferred size. A Group uses slightly different layout calculations that enable us to get the preferred size of its children reliably.

See: https://blogs.oracle.com/jfxprg/entry/the_peculiarities_of_javafx_layout */

Group group = new Group();

group.getChildren().add(loader.getRoot());SuprTEK Advanced Technology Group

Page 39: Thinking with a Clean Mind

39

Becoming Proficient at Writing Clean Code

SuprTEK Advanced Technology Group

Page 40: Thinking with a Clean Mind

40

Have Patience

You won’t get better overnight

Projects will take longer than you anticipate

You may not be able to finish projects

SuprTEK Advanced Technology Group

Page 41: Thinking with a Clean Mind

41

Pay Off Technical Debt

Find old code in your current project and refactor it

Go back to old projects and examine how you would write them better

SuprTEK Advanced Technology Group

Page 42: Thinking with a Clean Mind

42

Code More!

Start small

Be ambitious

Find something you like to do and do it

Contribute to open source projects

SuprTEK Advanced Technology Group

Page 43: Thinking with a Clean Mind

43

FINISH STUFF

Really, do it!

SuprTEK Advanced Technology Group

Page 45: Thinking with a Clean Mind

• Explore, evaluate, and develop new technologies that can be applied to our clients’ missions

Research & Product

Development

• Apply capabilities from R&PD to develop solutions that solve client-specific problems

Solutions Architectures

• Provide tactical consulting services to address technically-challenging requirements on client programs

Consulting Services

• Optimize and maintain IT infrastructure and security to support and enhance business operations

IT Infrastructure

Building Stuff Our Clients Wished Existed …