thinking with a clean mind

45
Thinking with a Clean Mind Mitchell Matthews 1

Upload: mitchell-matthews

Post on 06-Aug-2015

63 views

Category:

Technology


4 download

TRANSCRIPT

1

Thinking with a Clean MindMitchell Matthews

2

Wait Until the End For Photos

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

SuprTEK Advanced Technology Group

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

4

Why You Should Care

SuprTEK Advanced Technology Group

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

6

Productivity

Find bugs faster

Fix bugs faster

Write fewer bugs

Add features faster

SuprTEK Advanced Technology Group

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

8

What Clean Code Is

SuprTEK Advanced Technology Group

9

Clean Code...

Is easy to understand

Is easy to modify

Is easy to test

Works correctly

SuprTEK Advanced Technology Group

10

Writing Clean Code

SuprTEK Advanced Technology Group

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

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

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

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

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

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

17

Refactor

Refactoring is rewriting code without changing functionality

Refactor early and as often as necessary

SuprTEK Advanced Technology Group

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

19

Clean Development

SuprTEK Advanced Technology Group

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

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

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

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

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

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

26

Integrated Development Environments

and Source Control

SuprTEK Advanced Technology Group

27

IDEs

Catch common mistakes

Refactoring tools

Code completion and auto-generation

Automated builds

SuprTEK Advanced Technology Group

28

Source Control

Software that:Tracks changes

Enables short and long-term undos

Creates backups

Enables synchronization

Provides branching/merging

SuprTEK Advanced Technology Group

29

Common Source Control Software

Git

Subversion

Mercurial

SuprTEK Advanced Technology Group

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

31

Misconceptions and Mistakes

SuprTEK Advanced Technology Group

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

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

34

Superfluous Commenting

Don’t say how or what; say why

Use comments sparingly

SuprTEK Advanced Technology Group

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

36

Avoid:

/* Check if Web Workers are supported */

function getWebWorkerSupport() {

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

}

SuprTEK Advanced Technology Group

37

Do:

// Suppresses default constructor, ensuring non-instantiability.

private Collections() {

}

SuprTEK Advanced Technology Group

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

39

Becoming Proficient at Writing Clean Code

SuprTEK Advanced Technology Group

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

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

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

43

FINISH STUFF

Really, do it!

SuprTEK Advanced Technology Group

• 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 …