From 24945da51737d40f5a6b081bbefbf7df1dd8813c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 10 Nov 2017 16:12:24 +0100 Subject: [PATCH] Added code style --- CODE_STYLE.md | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 CODE_STYLE.md diff --git a/CODE_STYLE.md b/CODE_STYLE.md new file mode 100644 index 00000000..b2266cc8 --- /dev/null +++ b/CODE_STYLE.md @@ -0,0 +1,108 @@ +# Code Style + +This document is only meant to teach you the code style used in this project and will not explain *why* this coding style is used. + +## Tabs vs Spaces + +Use **tabs to indent** and **spaces for alignment** only: + +```go +type AnimeTitle struct { + Romaji string + English string + Japanese string + Canonical string + Synonyms []string +} +``` + +## Add an empty line between blocks and other statements + +Bad: + +```go +func() { + if true { + // Block 1 + } + if true { + // Block 2 + } + doSomething() + doSomething() + if true { + // Block 3 + } +} +``` + +Good: + +```go +func() { + if true { + // Block 1 + } + + if true { + // Block 2 + } + + doSomething() + doSomething() + + if true { + // Block 3 + } +} +``` + +## Variable names + +Variables are written in `camelCase` and should clearly state what they contain, preferably with no abbreviations. Common short names like `id` and `url` are allowed. + +Iterator variables in loops are an exception to this rule and can be 1-letter, non-significant variable names, usually `i` and `j`. + +## Early returns + +Do not wrap a whole function in 1 if-block to check parameters. Use early returns. + +Bad: + +```go +func DoSomething(a string, b string) { + if a != "" && b != "" { + doIt(a, b) + } +} +``` + +Good: + +```go +func DoSomething(a string, b string) { + if a == "" || b == "" { + return + } + + doIt(a, b) +} +``` + +## Private fields at the end + +This is not an absolute rule but try to keep private fields at the end of a struct. + +```go +type MyType struct { + PublicA string + PublicB string + PublicC string + + privateA int +} +``` + +## Package names + +Package names should be short lowercase identifiers and tests should be written using the black box pattern. Black box testing can be enabled by adding the suffix `_test` to the package names in `*_test.go` files. It will enable you to test your library like it would be used by another developer, without internal access to private variables. \ No newline at end of file