Anda di halaman 1dari 53

Static Code Analysis

Ill be learning
something new
on my deathbed.

@brwngrldev
+AnnyceDavis

What is it?
Checking your program for errors
without executing it

@brwngrldev

What its not

Testing
@brwngrldev

1 / 7 - 10
@brwngrldev

1 / 10
1000
@brwngrldev

The tools

Checkstyle

@brwngrldev

FindBugs

PMD

Lint

Checkstyle
Lint

FindBugs

PMD

@brwngrldev

Checkstyle
a development tool to help
programmers write Java code that
adheres to a coding standard.

Modules

Source
Files

Design

Checkstyle Tool

config.xml

Formatting

Code Complexity

apply plugin: checkstyle


task checkstyle(type: Checkstyle) {
description 'Checks if the code passes quality standards'
group 'verication'

congFile le(checkstyle.xml')

<module name=MethodLength">
<property name="max" value=60"/>
</module>

playerControlCong.setShowClosedCaptionsButton(a.getBo
<module name=LineLength">
<property name="max" value=120"/>
</module>

<module name=CyclomaticComplexity">
<property name="max" value=8"/>
</module>


<module name=CyclomaticComplexity">
<property name="max" value=8"/>
</module>

Example

public void overlyComplexMethod(Video video) {


if (video != null && video.getStreamUrl() != null) {
switch (video.getCategory()) {
case "CAT1" :
playVideo(video);
if (video.getLargeImageUrl() == null) {
video.setLargeImageUrl("http://www.largeImage.png");
}
updateMetadata(video);
break;
case "CAT2" :
if (video.getLargeImageUrl() == null) {
video.setLargeImageUrl("http://www.smallImage.png");

warning: Cyclomatic Complexity is 9

public void overlyComplexMethod(Video video) {


if (video != null && video.getStreamUrl() != null) {
updateVideoBasedOnCategory(video);
}
}
private void updateVideoBasedOnCategory(Video video) {
switch (video.getCategory()) {
case "CAT1" :
playVideo(video);
if (video.getLargeImageUrl() == null) {
video.setLargeImageUrl("http://www.largeImage.png");
}
updateMetadata(video);
break;

switch (video.getCategory()) {

case "CAT1" :
playVideo(video);
updateMetaDataAndUrl(video, "http://www.largeImage.png");
break;

@brwngrldev

Review

Code
Complexity

Refactor
Gradually

Formatting

Checkstyle
@brwngrldev

FindBugs

But this
inspect Java bytecode
for occurrences of bug
patterns

apply plugin: ndbugs


task ndbugs(type: FindBugs) {
description 'Run ndbugs'
group 'verication'
eort 'max'
excludeFilter le('ndbugs-exclude.xml')

<FindBugsFilter>
<Match>
<Class name="~.*R\$.*"/>
</Match>
<Match>
<Bug pattern="HE_EQUALS_NO_HASHCODE"/>
</Match>

</FindBugsFilter>

Example

gradle findbugs

Review

Not
Always
Right

Use
the
Filters

Bug Patterns

FindBugs

@brwngrldev

PMD
nds common programming aws like
unused variables, empty catch blocks

apply plugin: pmd


task pmd(type: Pmd) {
description 'Run pmd'
group 'verication'
ruleSetFiles = les("./qa-checks/pmd-ruleset.xml")

<ruleset>
<rule ref="rulesets/java/braces.xml" />
<rule ref="rulesets/java/strings.xml" />
<rule ref="rulesets/java/basic.xml" />

</ruleset>

Braces Ruleset

Example

gradle pmd

Find out why

Fix it

Review

Wasteful
Usage

Duplicate
Code

Possible Bugs

PMD

@brwngrldev

Ewww!!!

Lint
checks for structural code problems that
could aect the quality and performance of
your application.

cpplint
Lintian
PyLint
PC-Lint
Android Lint Splint JSLint

Example

Android Lint
<lint>
<issue id="IconColors" severity="ignore" />

<issue id="IconMissingDensityFolder" severity="ignore" />

<issue id="UnusedResources">
<ignore path="**/cong.xml" />
</issue>

</lint>

Continous Integration

Review

Exclude
Checks

Continuous
Integration

Structural Issues

Lint
@brwngrldev

Summary
Checkstyle
FindBugs
PMD
Lint
@brwngrldev

Resources

Clean Code - http://amzn.to/1DJybxH


Eective Java - http://amzn.to/1Ku8Xel
Google Code Style - http://goo.gl/8Pf6J3
QA Checks - http://git.io/vCMwc
Conquering Cyclomatic Complexity - http://goo.gl/lRoPXN
Using Android Lint - http://goo.gl/Zl2BPx
Static Code Analysis Tools - https://goo.gl/0Hczxn

@brwngrldev

Photo Credits

Slide 7 - https://www.ickr.com/photos/orinrobertjohn/13068719
Slide 20 - https://www.ickr.com/photos/oakleyoriginals/2750185692
Slide 41 - https://commons.wikimedia.org/wiki/File:Navel_lint_ball.jpg
Slide 50 - https://pixabay.com/en/thumb-success-successful-fanfaust-328420/

@brwngrldev

Thanks!
@brwngrldev
+AnnyceDavis
www.adavis.info

Anda mungkin juga menyukai