code quality assurance

15
©2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice Jan 15, 2013 Bart Blommaerts [email protected] EMEA NG Java & WEB: HP Belgium CODE QUALITY ASSURANCE

Upload: bart-blommaerts

Post on 08-Jun-2015

133 views

Category:

Technology


0 download

DESCRIPTION

Do's and don'ts regarding Code Quality Assurance. Presentation given to a technical Java audience.

TRANSCRIPT

Page 1: Code Quality Assurance

©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

Page 2: 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

Page 3: Code Quality Assurance

3 HP Confidential Jan 2013

Why do we need clean code?

Page 4: Code Quality Assurance

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

Page 5: Code Quality Assurance

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

Page 6: Code Quality Assurance

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

Page 7: Code Quality Assurance

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

Page 8: Code Quality Assurance

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

Page 9: Code Quality Assurance

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!

Page 10: Code Quality Assurance

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&amp;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>

Page 11: Code Quality Assurance

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&amp;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>

Page 12: Code Quality Assurance

12 HP Confidential

How do we measure clean code?

Jan 2013

• Demo!

Page 13: Code Quality Assurance

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

Page 14: Code Quality Assurance

14 HP Confidential

Q& A

Jan 2013

http://www.osnews.com/comics

Page 15: Code Quality Assurance

©2010 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice

THANKS FOR YOUR ATTENTION