Code Quality - LearningRbcRegistry/Wiki GitHub Wiki
#Code Quality -good code is like good coffee-
Antoine Bour / Oct 16*
1. Principle
2. Why it's important
3. Code coverage
4. SQALE (Software Quality Assessment based on Lifecycle Expectations)
5. List of tools
6. TDD
7. Agile test pyramid
8. Sonar as a product
9. Maven jacoco plugin
10. JsHint for SublimeText
11. SonarLint for Eclipse
12. References
###1. Principle ###
-
Bad code :
- Making good code not the priority
- Good code began to be old, unsynchronized, with deprecated dependences..
-
How to make good code… Respect this order: (Steward Digital’s 4 core quality principles):
- TDD / Test Driven Development
- Complies with requirements
- Test only a unit (not a class, the smaller part of code)
- TrustWorthiness, maintainability, readability, extensibility, documented AAA (Arrange, act, assert)
- TDD / Test Driven Development
-
DRY / Don’t Repeat yourself
-
YAGNI / You Aint Gonna Need It (do not add not required code)
- Bad code proposes too much. Respect the patterns.
- KISS: Keep it simple, stupid. Your code has to be easy to read and support for others:
- Not too much dependencies
- Small
- Expressive
-
Single Responsibility principle
- In facts:
- Descriptive names
- Documents and comments
- DRY
- Check errors and respond to them
- Splitted and short code.
- Do not overdesign
- Consistence
- Efficient
- … and with unit tests.
- Portable and buildable
- Versionning control
- In facts:
### 2. Why it's important ###
- Client satisfaction
- Release date respect
- Support, maintainability
- Readability and extensibility
- Software quality
- No need of top developpers for extensions
- Reduce costs
- Audits
- Competitive advantage
- Collaboration in the team
### 3. Code coverage### How many percent of code is executed while automated tests are running. Helps on unit testing coverage…but depends on quality of written tests: Are each parameter values covered by the junits?
### 4. SQALE (Software Quality Assessment based on Lifecycle Expectations) ###
-
Evaluation of software quality and technical debt.
-
Applicable to all representations of a software (from UML to code)
-
Quality requirement for a software:
- Atomic
- Unambiguous
- Non redundant
- Justifiable
- Acceptable
- Implementable
- Principles:
- Non remediation function (fix immediately,not later)
- Index density:
- Conformity rules in order to meet the SQALE method
- Example for java language:
### 5. List of tools ###
- Collaborator
- Visual Studio TFS
- Crucible
- Gerrit
- CodeReviewer
### 6. TDD Test Driven Development ###
- Traditional method: Code then test.
- TDD: Test code then code.
- RGR:
- Red: Write a test code that fails
- Green: Write the code that validate the test
- Refractor: rework the code to level-up the quality
- 5 steps:
- Write a test case
- Execute it, should fail because the code has not been written yet
- Write the implementation based on the junits.
- Execute the test. They should be successed.
- Rework the code to enhance the quality
- Pros:
- Test units are really written
- Code is more efficient, done in more little steps… and other code quality requirements.
- Clarify the way to produce the code
- Test reporting and automation
- No regression testing • Cons:
- Don’t think that it helps to create a 100% covered test suite (and regression suite)
- This will not replace business testing
- Not all developers are test-fans!
- Test suite can be long to execute
### 7. Agile test pyramid ###
### 8. Sonar as a product ###
Non exhaustive list of plugins:
| Tool | Link |
|---|---|
| Sonar Squid | http://www.sonarsource.org/ |
| Checkstyle | http://checkstyle.sourceforge.net |
| PMD | http://pmd.sourceforge.net |
| Findbugs | http://findbugs.sourceforge.net/ |
| Cobertura | http://cobertura.sourceforge.net |
| JaCoCo | http://www.eclemma.org/jacoco/index.html |
| Clover | http://www.atlassian.com/software/clover |
| Surefire | http://maven.apache.org/plugins/maven-surefire-plugin |
| In the lab, jenkins is on xl15srv0008 and sonar is configured in the settings.xml from Jenkins |
Install in local
- Update of wrapper.conf of sonar folder in order to point to 1.8 jdk version.
- The server is launched locally with sonar\bin\windows-x86-64\StartSonar.bat
- In the maven settings, add the following in the maven settings:
- Add the following in the wished pom:
- Mvn sonar:sonar: results are builds and published to sonar
- Another option is to configure the pom to do it automatically during the verification step:
- Another option is to configure the pom to do it automatically during the verification step:
### 9. Maven jacoco plugin ###
- It’s a code coverage plugin.
- How to use : Pom Exemple
- In the attached pom files we say to generate reports in the jacoco output folder (destFile, destData).
- The reports are available in the site folder
- CoveredRatio indicates the mimimum of covering requested in order to make the build in success. (0.15 = 15%)
- The "exclude" tag indicates the javaclasses that are excluded from the test coverage (and also from the coveredratio)
- List of maven goals that helps:
mvn help:describe -Dplugin=org.jacoco:jacoco-maven-plugin -Ddetail
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=true
- The generated report is like the following:
### 10. JsHint for SublimeText ###
- It can be used as a npm module (via gulp ??) in order to control the code quality.
- I have decided to use it as a sublimeText plugin. I can see directly my coding issues!
- How to install it in sublimetext: - Ctrl shift P: package control install package - Set node path. Ctrl shift P: set node path (in the file update the path)
### 11. SonarLint for Eclipse ###
- It’s a codecoverage tool based on jacoco.
- Via The Eclipse Marketplace, search and install EclEmma Java Code Coverage.
- As a result, you will be able to check if your code coverage of unit tests is respecting your standards.. And you can still (coverage configurations) exclude wished files from the coverage run):
### 12. References ###
- http://dorightdigital.com/principles/core-code-quality-principles
- https://blog.goyello.com/2013/01/21/top-9-principles-clean-code/
- https://blog.goyello.com/2011/10/06/three-pillars-of-unit-tests/
- http://www.informit.com/articles/article.aspx?p=2223710
- http://docs.sonarqube.org/display/PLUG/SQALE+Plugin
- http://www.sqale.org/
- https://fr.wikipedia.org/wiki/SQALE
- http://www-igm.univ-mlv.fr/~dr/XPOSE2009/TDD/pagesHTML/PresentationTDD.html
- http://agiledata.org/essays/tdd.html
- http://bruno-orsier.developpez.com/tutoriels/TDD/pentaminos/ (for an example)
- http://www.eclemma.org/jacoco/trunk/doc/