Download - Writing Good Code

Transcript
Page 1: Writing Good Code

Slide titleIn CAPITALS

50 pt

Slide subtitle 32 pt

Writing Good Code<lecturer> Leo Liang </lecturer>

<date> 2009-7 </date>

Page 2: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

We are …

Programmer

Designer

Elegant Code

Page 3: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Principles

Your code should

– be simple and clean– be readable and maintainable– be testable– be secure– be extensible

Page 4: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Rule #1

get the job done

Page 5: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Readable

Beautiful code is like poem

– Good code style– Meaningful naming– Short method– Appropriate class size

Unreadable code is unmaintainable

How To Write Unmaintainable Code? http://freeworld.thc.org/root/phun/unmaintain.html

白色马驹浮出雾中转瞬不见回到雾里 秋日午后无花果树叶轻轻落下停在自己的影子上

--- 阿巴斯

Page 6: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Simple & Clean

Choose the simplest way that could possibly work

Don’t repeat yourself– Reuse

High cohesion, low coupling– Design with contracts– Orthogonal ( 正交 )

Page 7: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Robust

Robustness means two things:

– getting the job done

– continuing to get the job done as much as possible even when things go bad

Page 8: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Performance vs. Style

Code style is more important than code performance– Consider readability and maintainability

Performance is very important

How the design is affects performance much more than how to code

– Performance is depends on a lot of factors– Design correctly– Identify the performance bottleneck by test or tool

Clever Code

Page 9: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Don’t live with BAD code

Refactoring

Page 10: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Principles

Your code should– get the job done– be simple and clean– be readable and maintainable– be testable– be secure– be extensible

Page 11: Writing Good Code

Slide titleIn CAPITALS

50 pt

Slide subtitle 32 pt

Coding Guidelines

Page 12: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Why need code convention

Improve the readability Create maintainable code Eliminate potential bugs

– Bug pattern– Best practices

ClearCase merge

No absolutely good or bad Need unify convention in a team

Page 13: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Programming Style Guidelines

Every product line has its own guidelines defined– SIG / ISP / MSDP / MIEP / AOP

General guidelines– Java Guidelines from Geotechnical Software Services

(http://geosoft.no/development/javastyle.html)– Excepts: 8 / 20 / 34 / 61 / 63 / 64

Take 20 minutes to go throw them!

Page 14: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Naming Convention

Camel case– Types: nouns– Methods: verbs

TrafficProfile trafficProfile = createTrafficProfile();

Constants: all uppercase using underscore to separate words

setStatus( SERVICE_NOT_ACTIVE );

Page 15: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Comment

JavaDoc– Classes, public methods

Comment on – where the code isn’t easy to understand – where the code differs to conventions

Unfinished task– // TODO: description

Know issue– // FIXME: description

Delete the unused code instead of comment out

Page 16: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

No warning

Target: No warning– Everyone has the responsibility to fix the warnings– Should check at code review

Suppress warning (Be careful!)– Java:

Annotate with @SuppressWarning– CheckStyle:

Suppress audit events between a comment containing CHECKSTYLE:OFF and a comment containing CHECKSTYLE:ON

– FindBugs:Edit the findbugs_exclude.xml

Page 17: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Sample

Find code style issues from the sample code

sample.java

Page 18: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Eclipse Usage

Eclipse built-in code style formatter– Menu: Source – Format– Keyboard: Ctrl-Shift-F

Rename an identifier is very easy in Eclipse– Menu: Refactor - Rename – Keyboard: Alt-Shift-R

Page 19: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Code Check-in Rule

Before check-in your code, you must ensure– Code compiled successful– Basic test passed

Check-in frequency– As soon as possible– No longer than 1 day

Do not leave the file locked when you are not editing it

Write meaningful comment at check-in

Ensure the code in ClearCase is always valid

Page 20: Writing Good Code

Slide titleIn CAPITALS

50 pt

Slide subtitle 32 pt

Quick Reference

Page 21: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Quick Reference – Naming Example

AudioSystem audioSystem; // NOT: audio_system

openDvdPlayer(); // NOT: openDVDPlayer();

getServiceCapability(); // NOT: getSc();

MAX_ITERATIONS = 25; void setTopic(Topic topic) // NOT: void setTopic(Topic value)

// NOT: void setTopic(Topic aTopic) // NOT: void setTopic(Topic t)

Collection<Point> points; // NOT: point

line.getLength(); // NOT: line.getLineLength();

Page 22: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Quick Reference – Naming Example

void setFound(boolean isFound); // NOT: setIsFound(boolean isFound)// NOT: setFound(boolean found)

boolean hasLicense, canEvaluate, shouldAbort;

get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, old/new, open/close, show/hide, suspend/resume

xxxFactory, xxxProxy, xxxFacade, xxxCommandxxxExecption, xxxImpl, DefaultXxx, AbstractXxx

Page 23: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Quick Reference - Comment

Comment as reminder// TODO: ……// FIXME: ……

Suppress warning– Java / FindBugs / PMD

@SuppressWarning– checkstyle

// CHECKSTYLE:OFFSome codes …// CHECKSTYLE:ON

Page 24: Writing Good Code

Top right corner for field-mark, customer or partner logotypes. See Best practice for example.

Slide title 40 pt

Slide subtitle 24 pt

Text 24 pt

Bullets level 2-520 pt

Quick Reference - Eclipse

Format code style– Source - Format (Ctrl-Shift-F)

Organize imports– Source - Organize imports (Ctrl-Shift-O)

Rename identify– Refactor - Rename (Alt-Shift-R)

Code clean up– Source - Clean up…

Error correct suggestion– Edit - Quick Fix (Ctrl-1)

More functions in Source and Refactor menu …

Page 25: Writing Good Code