Download - Static Code Analysis
![Page 1: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/1.jpg)
Static Code Analysis
![Page 2: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/2.jpg)
![Page 3: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/3.jpg)
@brwngrldev +AnnyceDavis
“I’ll be learning something new on my deathbed.”
![Page 4: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/4.jpg)
Checking your program for errors without executing it
What is it?
@brwngrldev
![Page 5: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/5.jpg)
Testing
What it’s not
@brwngrldev
![Page 6: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/6.jpg)
1 / 7 - 10@brwngrldev
![Page 7: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/7.jpg)
![Page 8: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/8.jpg)
1 / 10@brwngrldev
1000
![Page 9: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/9.jpg)
The tools…
Checkstyle
1
FindBugs
2
PMD
3
Lint
4
@brwngrldev
![Page 10: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/10.jpg)
@brwngrldev
Lint
Checkstyle
FindBugs
PMD
![Page 11: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/11.jpg)
Checkstyle
“… a development tool to help programmers write Java code that
adheres to a coding standard.”
![Page 12: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/12.jpg)
Source
Files
config.xml
Checkstyle Tool
Modules
Design
Formatting
Code Complexity
![Page 13: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/13.jpg)
apply plugin: ‘checkstyle’
task checkstyle(type: Checkstyle) { description 'Checks if the code passes quality standards' group 'verification' configFile file(‘checkstyle.xml') …}
![Page 14: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/14.jpg)
<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …
playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false)); playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false));
![Page 15: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/15.jpg)
<module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module>
![Page 16: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/16.jpg)
Example
![Page 17: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/17.jpg)
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"); }
![Page 18: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/18.jpg)
… warning: Cyclomatic Complexity is 9
![Page 19: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/19.jpg)
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;
7
![Page 20: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/20.jpg)
switch (video.getCategory()) { case "CAT1" : playVideo(video); updateMetaDataAndUrl(video, "http://www.largeImage.png"); break; …
4
@brwngrldev
![Page 21: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/21.jpg)
Review…
• Formatting
• Code Complexity
• Refactor Gradually
@brwngrldev
Checkstyle
1
![Page 22: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/22.jpg)
FindBugs
![Page 23: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/23.jpg)
But this…
“…inspect Java bytecode for occurrences of bug
patterns”
![Page 24: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/24.jpg)
apply plugin: ‘findbugs’
task findbugs(type: FindBugs) { description 'Run findbugs' group 'verification'
effort 'max' excludeFilter file('findbugs-exclude.xml') …}
![Page 25: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/25.jpg)
<FindBugsFilter> <Match> <Class name="~.*R\$.*"/> </Match> <Match> <Bug pattern="HE_EQUALS_NO_HASHCODE"/> </Match>
…</FindBugsFilter>
![Page 26: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/26.jpg)
Example
![Page 27: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/27.jpg)
gradle findbugs
![Page 28: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/28.jpg)
![Page 29: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/29.jpg)
![Page 30: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/30.jpg)
Review…
• Bug Patterns
• Not Always Right
• Use the Filters
FindBugs
2
@brwngrldev
![Page 31: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/31.jpg)
![Page 32: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/32.jpg)
PMD
“…finds common programming flaws like unused variables, empty catch blocks…”
![Page 33: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/33.jpg)
apply plugin: ‘pmd’
task pmd(type: Pmd) { description 'Run pmd' group 'verification'
ruleSetFiles = files("./qa-checks/pmd-ruleset.xml") …}
![Page 34: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/34.jpg)
<ruleset> <rule ref="rulesets/java/braces.xml" />
<rule ref="rulesets/java/strings.xml" />
<rule ref="rulesets/java/basic.xml" /> …</ruleset>
![Page 35: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/35.jpg)
Braces Ruleset
![Page 36: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/36.jpg)
Example
![Page 37: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/37.jpg)
gradle pmd
![Page 38: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/38.jpg)
Find out why
![Page 39: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/39.jpg)
Fix it…
![Page 40: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/40.jpg)
Review…
• Possible Bugs
• Wasteful Usage
• Duplicate Code
@brwngrldev
PMD
3
![Page 41: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/41.jpg)
Ewww!!!
![Page 42: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/42.jpg)
“…checks for structural code problems that could affect the quality and performance of
your application.”
Lint
![Page 43: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/43.jpg)
Lintian
JSLintAndroid Lint SplintPC-Lint
PyLint
cpplint
![Page 44: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/44.jpg)
Example
![Page 45: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/45.jpg)
Android Lint<lint> <issue id="IconColors" severity="ignore" /> <issue id="IconMissingDensityFolder" severity="ignore" /> <issue id="UnusedResources"> <ignore path="**/config.xml" /> </issue> …</lint>
![Page 46: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/46.jpg)
![Page 47: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/47.jpg)
![Page 48: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/48.jpg)
Continous Integration
![Page 49: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/49.jpg)
Review
• Structural Issues
• Exclude Checks
• Continuous Integration
Lint
4
@brwngrldev
![Page 50: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/50.jpg)
Summary
PMD
Checkstyle
FindBugs
Lint
@brwngrldev
![Page 51: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/51.jpg)
Resources
• Clean Code - http://amzn.to/1DJybxH
• Effective 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
![Page 52: Static Code Analysis](https://reader031.vdocuments.us/reader031/viewer/2022030316/587802741a28ab91178b7089/html5/thumbnails/52.jpg)
Photo Credits• Slide 7 - https://www.flickr.com/photos/orinrobertjohn/13068719
• Slide 20 - https://www.flickr.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-fan-faust-328420/
@brwngrldev