©2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice
Jan 15, 2013
Bart [email protected] NG Java & WEB: HP Belgium
CODE QUALITY ASSURANCE
2 HP Confidential
• Why do we need clean code?
• What is clean code?
• How do we write clean code?
• How do we test clean code?
• How do we measure clean code?
• Disclaimer: clean code vs. great code
• Q & A
Jan 2013
Agenda
3 HP Confidential Jan 2013
Why do we need clean code?
4 HP Confidential
Why do we need clean code?
Jan 2013
Objectives–Easy to read and understand
–Unnecessary code and noise are removed
–Maintaining and extending code is easier• First step towards re-use!
–Project lifecycle cost will be reduced• Cost of owning a mess ..
“Writing clean code is what you must do in order to call yourself a professional. There is no reasonable excuse for doing anything less than your best.” Robert C. Martin
5 HP Confidential
What is clean code?
Jan 2013
• Simple and direct• Human readable• Efficient• No duplications: DRY• Made for the problem• Easy to enhance
6 HP Confidential
How do we write clean code?
Jan 2013
• SRP: Single responsibility principle– Do One Thing
– Stepdown Rule
– Restrict number of arguments (3 is a lot ..)
• Boy Scout Rule– Leave code better than you found it
– Don’t comment bad code, rewrite it
7 HP Confidential
How do we write clean code?
Jan 2013
• Meaningful, self-explaining names– Purpose of a variable, class, method, ..
– Avoid disinformation (eg. lblUserName)
• Error Handling– Use exceptions rather then return codes
– Don’t return null
– Don’t pass null
• Expresses intent
8 HP Confidential
How do we test clean code?
Jan 2013
• SRP: Single responsibility principle– Unit test enabler
• Boy Scout Rule– Add unit tests before refactoring
9 HP Confidential
How do we test clean code?
Jan 2013
• Meaningful names– Understand what to test
• Error Handling– @Test(expected=CleanCodeException.class)
Fix a bug only once. Write Unit tests when fixing bugs!
10 HP Confidential
How do we measure clean code?
Jan 2013
• Sonar integration– http://sonar.elabs.eds.com:9000/
• Maven:<properties>
<sonar.jdbc.url>jdbc:mysql://sonar.elabs.eds.com:3306/sonar?useUnicode=true&characterEncoding=utf8</sonar.jdbc.url><sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver><sonar.jdbc.username>x</sonar.jdbc.username><sonar.jdbc.password>x</sonar.jdbc.password><sonar.host.url>http://sonar.elabs.eds.com:9000</sonar.host.url>
</properties>
11 HP Confidential
How do we measure clean code?
Jan 2013
• Ant:<!-- Sonar properties --><property name="sonar.jdbc.url" value="jdbc:mysql://sonar.elabs.eds.com:3306/sonar?useUnicode=true&characterEncoding=utf8" /><property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" /><property name="sonar.jdbc.username" value="x" /><property name="sonar.jdbc.password" value="x" /><property name="sonar.host.url" value="http://sonar.elabs.eds.com:9000" />
<!-- Sonar target --> <target name="sonar" depends="compile">
<sonar:sonar workDir="." key="be.vlaanderen.vip:xfire" version="02.00" xmlns:sonar="antlib:org.sonar.ant">
<sources><path location="${project.dir.src}" />
</sources></sonar:sonar>
</target>
<!-- Sonar task --><taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath><fileset dir="${project.dir.lib}">
<include name="sonar-ant-task-1.0.jar" /></fileset>
</classpath></taskdef>
12 HP Confidential
How do we measure clean code?
Jan 2013
• Demo!
13 HP Confidential
Disclaimer: clean code vs. great code
Jan 2013
• Primary goal: making it work
• SRP– But: no methods with 1 line of code
– But: don’t write a huge amount of tiny classes
• Boy Scout Rule– Don’t go overboard
14 HP Confidential
Q& A
Jan 2013
http://www.osnews.com/comics
©2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice
THANKS FOR YOUR ATTENTION
Top Related