Coding Standards
Introduction
Coding conventions serve the following purposes:
- They create a consistent look to the code, so that readers can focus on content, not layout.
- They enable readers to understand the code more quickly by making assumptions based on previous experience.
- They facilitate copying, changing, and maintaining the code.
- They demonstrate best practices.
The principles below broadly apply to all Ed-Fi source code. For language-specific guidance, please see:
General Principles
- The standards below are a guide, but they are not absolute. Violations of the standard can be accepted when two developers on the responsible team agree with the justification.
- Readability is essential:
- Be consistent in style.
- Use empty lines and spaces appropriately.
- Use descriptive names and naming conventions at all times.
- Follow existing patterns unless there is a good reason not to do so (and justify that in the code review).
- Keep methods and classes short and to the point (i.e., adhere to the Single Responsibility Principle).
- When making changes to an existing file, consider several small commits. Small commits can help a reviewer more easily understand the changes in the diff compared to a single large commit.
- New language features can be very powerful, but can introduce confusion for both developers reading the code and systems that implement it. Be conservative about using new language features and consult with the development team before doing so.
Layout Conventions
Good layout uses formatting to expose the structure of your code and to make the code easier to read. Ed-Fi code conforms to the following conventions:
-
Set your Code Editor to use four-character indents and save tab characters as spaces.
-
Write only one statement per line.
-
Write only one declaration per line.
-
If continuation lines are not indented automatically, indent them one tab stop (i.e., four spaces).
-
Avoid more than one empty line at at time.
-
Limit the length of a line of code to about 100-110 characters to reduce unnecessary horizontal scrolling.
-
In source code repositories that are made available to the public under the terms of the Apache license, every file should begin with a license header. The example below uses C-style comments; substitute the correct commenting style for the language in use.
C# Example
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.
Commenting Conventions
Use comments liberally to provide useful context for another developer to be able to follow the intent and logic of the code. Explain why, not what. Prefer using proper English sentences.
-
Place the comment on a separate line, not at the end of a line of code.
-
Prefer use of proper English sentences.
-
Insert one space between the comment delimiter and the comment text, as shown in the following example.
C# Example
// The following declaration creates a query. It does not run
// the query.SQL Example
-- The following declaration creates a query. It does not run
-- the query. -
Do not create formatted blocks of asterisks around comments.
-
Comments should have a blank line before and, at the very least, after the segment of code to which the comment applies (thus the line above the
ForEach
).C# Example
var scores = _injectedService.GetScores();
// Calculate the average
int count = scores.Count;
double sum = scores.Sum();
double average = sum / count;
scores.ForEach(x => Console.WriteLine(x.Average)); -
TODO comments should not be kept in the final code merge. If a change is being deferred for some reason, add the ID of the Tracker ticket in which the issue will be resolved:
# ODS-9876: needs better null handling
Portions of this document are based on the Microsoft C# Coding Conventions, which have been reproduced and modified under the terms of the Creative Commons Attribution 4.0 International license.