This document outlines the coding style guidelines for Go (Golang) projects in our organization. Adhering to consistent coding standards improves code readability and maintainability.
- Use
gofmt
to format code automatically. - Limit lines to 80 characters for better readability.
// Incorrect
func myFunction() { fmt.Println("Hello, World!") }
// Correct
func myFunction() {
fmt.Println("Hello, World!")
}
- Follow the camelCase convention for variable and function names.
- Use PascalCase for exported functions, types, and constants.
- Avoid abbreviations unless they are widely accepted (e.g., HTTP).
// Incorrect
var my_var int
func HTTPRequest() {}
// Correct
var myVar int
func HTTPRequest() {}
- Group standard library imports first, followed by third-party packages and then local packages.
- Keep imports organized and remove unused ones.
// Incorrect
import (
"fmt"
"net/http"
"project/packageA"
"project/packageB"
)
// Correct
import (
"fmt"
"net/http"
"github.com/thirdparty/packageA"
"github.com/thirdparty/packageB"
"local/project/packageC"
)
- Always check and handle errors explicitly.
- Favor returning errors over logging them within functions.
// Incorrect
func myFunction() {
result, err := someFunction()
// Ignoring the error
}
// Correct
func myFunction() error {
result, err := someFunction()
if err != nil {
return err
}
// Continue with the result
return nil
}
- Write clear and concise comments to explain non-obvious code.
- Document exported functions and types with comments.
// Incorrect
// Function to process data
func process() {}
// Correct
// processData is a function that performs data processing.
func processData() {}
- Use goroutines and channels for concurrent programming.
- Avoid using shared memory for communication; prefer communication through channels.
// Incorrect
var sharedData int
func worker() {
sharedData++
}
// Correct
func worker(ch chan int) {
ch <- 1
}
// In the calling function
func main() {
ch := make(chan int)
go worker(ch)
data := <-ch
// Process data
}
- Write comprehensive unit tests for your code.
- Use the
testing
package for tests. - Name test files with the
_test.go
suffix.
// File: my_function_test.go
package mypackage
import (
"testing"
)
func TestMyFunction(t *testing.T) {
result := myFunction()
if result != expectedValue {
t.Errorf("Expected %v, got %v", expectedValue, result)
}
}
This document outlines the coding style guidelines for Node.js projects in our organization. Consistent coding standards improve code maintainability and collaboration.
- Formatting
- Naming Conventions
- Modules and Imports
- Error Handling
- Comments
- Asynchronous Programming
- Testing
- Use 2 spaces for indentation.
- Limit lines to 80 characters for better readability.
- End files with a newline.
// Incorrect
function myFunction() { console.log("Hello, World!"); }
// Correct
function myFunction() {
console.log("Hello, World!");
}
- Follow the camelCase convention for variable and function names.
- Use PascalCase for class names.
- Use UPPERCASE_WITH_UNDERSCORES for constants.
// Incorrect
var my_var;
function httpRequest() {}
// Correct
var myVar;
function HttpRequest() {}
- Use
import
statements for module dependencies. - Group and order import statements consistently.
// Incorrect
const localModule = require('./localModule');
const fs = require('fs');
const express = require('express');
// Correct
const express = require('express');
const fs = require('fs');
const localModule = require('./localModule');
- Always check and handle errors explicitly.
- Use the
try-catch
block for synchronous code andasync/await
for asynchronous code.
// Incorrect
function myFunction() {
// Ignoring errors
}
// Correct
function myFunction() {
try {
// Code that might throw an error
} catch (error) {
// Handle the error
}
}
- Write clear and concise comments to explain non-obvious code.
- Document exported functions and modules with comments.
// Incorrect
// Function to process data
function process() {}
// Correct
// processData is a function that performs data processing.
function processData() {}
- Prefer using Promises or
async/await
for asynchronous code. - Avoid callback hell by using modular and named functions.
// Incorrect
fs.readFile('file.txt', (err, data) => {
// Callback code
});
// Correct
const readFilePromise = (filename) => {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
- Write comprehensive unit tests for your code.
- Use testing frameworks like Jest or Mocha.
- Name test files with the
.test.js
suffix.
// File: myFunction.test.js
const { myFunction } = require('./myModule');
test('myFunction should return the expected value', () => {
const result = myFunction();
expect(result).toBe(expectedValue);
});