There is no such thing as "perfect" code — there is only better code!
Instead of seeking perfection, we should seek for is "continuous improvement"...
We rarely write tests for our tests — a human must ensure that tests are valid.
Code Quality Is Everyone’s Responsibility including:
Developer
Tester
Code quality aspects
Aspect
Comments
Reliability
Probability that a system will run without failure over a specific period of operation
Maintainability
How easily software can be maintained
Testability
how well the software supports testing efforts
Portability
how usable the same software is in different environments
Reusability
whether existing assets — such as code — can be used again
Code quality Metrics
Metric
Comments
Defect Metrics
Number of defects — and severity of those defects — are important metrics of overall quality.
Complexity Metrics
Cyclomatic complexity for example
Testability
how well the software supports testing efforts
Portability
how usable the same software is in different environments
Ways to Improve Code Quality
Way
Comments
Use a coding standard
Makes everyone to use the right style
Analyze code — before code reviews
Analyze code as soon as it’s written
Follow code review best practices
Manual code reviews are still important for verifying the intent of the code
Refactor legacy code (when necessary)
help you clean up your codebase and lower its complexity
Run Static Code Analysis tools like PMD that attempt to highlight possible violations and vulnerabilities within ‘static’ (non-running) source code by using techniques such as Taint Analysis and Data Flow Analysis
Individual programmers are less than 50% efficient at finding bugs in their own software.
Code Walk-thru
Goals
The overall code health of code base is improving over time
The codebase stays consistent, maintainable
What to look for in a Code Review?
The developers should test code well-enough that they work correctly by the time they get to code review.
Item
Comments
Design
Interactions in the code makes sense?
Functionality
Code is the manifest of what the developer intended?
Complexity
Is the code is more complex than it should be?. “can’t be understood quickly by code readers.” Over-engineering, where developers have made the code more generic than it needs to be
Tests
Unit, integration, or end-to-end tests
Naming
Developer pick good names for everything?
Comments
Developer has written clear comments in understandable Language. Comment should explain why some code exists, and should not be explaining what some code is doing. Regular expressions and complex algorithms often benefit greatly from comments that explain what they’re doing, for example. Note that comments are different from documentation of classes, modules, or functions, which should instead express the purpose of a piece of code, how it should be used, and how it behaves when used
Style
Developer follows the published style for a given programming language
Documentation
READMEs and generated reference docs
Code Review Summary
Item
The code is well-designed.
The functionality is good for the users of the code.
Any UI changes are sensible and look good.
Any parallel programming is done safely.
The code isn’t more complex than it needs to be.
The developer isn’t implementing things they might need in the future but don’t know they need now.
Code has appropriate unit tests.
Tests are well-designed.
The developer used clear names for everything.
Comments are clear and useful, and mostly explain why instead of what.
The goal of review is to maintain the quality of the codebase and the products
Fix the Code
1. Clarify the code 2.code comment that explains why the code is there (if required) 3. Modify the code
Think Collaboratively
If you disagree with the reviewer, find ways to collaborate: ask for clarifications, discuss pros/cons, and provide explanations of why your method of doing things is better for the codebase
Should be unique, begin with an Uppercase letter. Do not contain underscores or spaces (except from the prefix and suffix). Should be nouns in mixed cases, with first letter of each interval word capitalized
AccountController
Variable Name
should be in mixed case (camelCase) with a lowercase first letter. Internal words start with capital letters
geocodingUrl
Method Name
Should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized - camelCase
getPagedPropertyList(...)
Constants
Class constants should be all UPPERCASE with words separated by underscores
private static final String BASE_URL = 'https://nominatim.openstreetmap.org/search?format=json'
Trigger
Trigger, should follow Salesforce Trigger – One trigger per object
// app.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
// app.test.js
const sum = require('./app');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
yarn test
yarn test
yarn run v1.22.17
$ jest
PASS ./app.test.js
✓ adds 1 + 2 to equal 3 (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.519 s, estimated 1 s
Ran all test suites.
✨ Done in 1.76s.