Writing Good Code
-
Author
aleung -
Category
Technology
-
view
1.618 -
download
1
Embed Size (px)
description
Transcript of Writing Good Code

Slide titleIn CAPITALS
50 pt
Slide subtitle 32 pt
Writing Good Code<lecturer> Leo Liang </lecturer>
<date> 2009-7 </date>

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

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

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

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
白色马驹浮出雾中转瞬不见回到雾里 秋日午后无花果树叶轻轻落下停在自己的影子上
--- 阿巴斯

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 ( 正交 )

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

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

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

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

Slide titleIn CAPITALS
50 pt
Slide subtitle 32 pt
Coding Guidelines

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

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!

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 );

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

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

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

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

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

Slide titleIn CAPITALS
50 pt
Slide subtitle 32 pt
Quick Reference

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();

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

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

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 …
