Coding Standards
1 - Why create and document coding standards?
2 - What are the coding standards?
3 - Debating, updating, changing coding standards
4 - How to update the coding standards
Why create coding standards?
Informs new hires
New developers can learn how we want to do things as opposed to learn how we have done things.
Reading the codebase tells developers how we have once done something. Not how we hope it gets done in the future.
Why create coding standards?
Basis for Code Reviews
When performing a code review, use the coding standards to point out improvements and refer to links for reference.
Why create coding standards?
Basis for Code Reviews
When performing a code review, use the coding standards to point out improvements and refer to links for reference.
Why create coding standards?
Encourages "Team-Oriented" Code
The purpose of coding standards are to encourage team-oriented code, which is code that tends to be easy for anyone to maintain and update.
What makes "Team-Oriented" Code?
Intentions of Code are Understood
The purpose of coding standards are to encourage team-oriented code, which is code that tends to be easy for anyone to maintain and update.
Things that make testing more difficult
SpiesAsynchronous code Dependencies / Mocks Dates / Timezones JS Frameworks / 3rd Party Libraries The DOM
Async things
1 - Non-promises setTimeout(), $timeout, alert() 2 - Promises using .then() chaining 3 - Async/Await promises
4 - RxJs (Observables) 5 - $q.defer() (AngularJS's promise library before promises were main stream)
jasmine.clock()
using jasmine clock for setTimeout()
.tick() is part of jasmine.clock()??? Could waitForAsync() and fakeAsync() be a wrapper on jasmine.clock() ????
jasmine.clock().install();
let value = controller.myFunction();
jasmine.clock().tick(2000);
expect(value).toBe("not undefined");
jasmine.clock().uninstall();Testing Async
// Typescript
public async saveCarePlan(): Promise<void> {
try {
await this.handleModuleInfoSave(moduleInfo, patient);
await this.handleMedicationsSave();
} catch(error) {
// if any trouble saving module info or medications a failure message to display
this.NotifyService.danger(this.$translate.instant('careplan.metrics.error'));
}
}
it('should call show danger message if an error', (done) => {
spyOn(controller, "preSaveCheckForPCVPhone").and.returnValue(Promise.resolve());
spyOn(controller, "handleModuleInfoSave").and.returnValue(Promise.resolve());
spyOn(controller, "handleMedicationsSave").and.returnValue(Promise.reject());
// handleMedicationSave returning a rejected promise will result in an error, landing in the catch block, resulting in the danger message
let notifyDangerSpy = spyOn(NotifyService, "danger");
controller.saveCarePlan()
.then(() => {
expect(notifyDangerSpy).toHaveBeenCalled();
done();
});
});Promises with .then()
jasmine.any(Date)
// 1 - Date
expect(new Date()).toEqual(jasmine.any(Date)); // PASSES
let newMomentDate = moment().toDate();
expect(newMomentDate).toEqual(jasmine.any(Date)); // PASSESCoding Standards
By Rich Finelli
Coding Standards
- 81