Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kodecocodes/c-sharp-style-guide
C# Style Guide for Game Tech tutorials
https://github.com/kodecocodes/c-sharp-style-guide
Last synced: 3 months ago
JSON representation
C# Style Guide for Game Tech tutorials
- Host: GitHub
- URL: https://github.com/kodecocodes/c-sharp-style-guide
- Owner: kodecocodes
- Created: 2015-06-23T14:48:16.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-02-03T06:55:34.000Z (about 2 years ago)
- Last Synced: 2024-04-13T18:56:47.165Z (10 months ago)
- Homepage:
- Size: 37.1 KB
- Stars: 487
- Watchers: 32
- Forks: 114
- Open Issues: 3
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
- awesome-guidelines - C# Style Guide
- fucking-awesome-guidelines - C# Style Guide
- awesome-guidelines - C# Style Guide
README
## Table of Contents
- [The Official kodeco.com C# Style Guide](#the-official-kodecocom-c-style-guide)
- [Inspiration](#inspiration)
- [Nomenclature](#nomenclature)
- [Namespaces](#namespaces)
- [Classes & Interfaces](#classes--interfaces)
- [Methods](#methods)
- [Fields](#fields)
- [Properties](#properties)
- [Parameters](#parameters)
- [Actions](#actions)
- [Misc](#misc)
- [Declarations](#declarations)
- [Access Level Modifiers](#access-level-modifiers)
- [Fields & Variables](#fields--variables)
- [Classes](#classes)
- [Interfaces](#interfaces)
- [Spacing](#spacing)
- [Indentation](#indentation)
- [Blocks](#blocks)
- [Line Wraps](#line-wraps)
- [Line Length](#line-length)
- [Vertical Spacing](#vertical-spacing)
- [Brace Style](#brace-style)
- [Switch Statements](#switch-statements)
- [Language](#language)
- [Copyright Statement](#copyright-statement)
- [Smiley Face](#smiley-face)
- [Credits](#credits)# The Official Kodeco C# Style Guide
This style guide is different from others you may find, because the focus is
centered on readability for print and the web. We created this style guide to
keep the code in our tutorials consistent.Our overarching goals are **conciseness**, **readability** and **simplicity**. Also, this guide is written to keep **Unity** in mind.
## Inspiration
This style guide is based on C# and Unity conventions.
## Nomenclature
On the whole, naming should follow C# standards.
### Namespaces
Namespaces are all **PascalCase**, multiple words concatenated together, without hyphens ( - ) or underscores ( \_ ). The exception to this rule are acronyms like GUI or HUD, which can be uppercase:
**AVOID**:
```csharp
com.kodeco.fpsgame.hud.healthbar
```**PREFER**:
```csharp
Kodeco.FPSGame.HUD.Healthbar
```### Classes & Interfaces
Classes and interfaces are written in **PascalCase**. For example `RadialSlider`.
### Methods
Methods are written in **PascalCase**. For example `DoSomething()`.
### Fields
All non-static fields are written **camelCase**. Per Unity convention, this includes **public fields** as well.
For example:
```csharp
public class MyClass
{
public int publicField;
int packagePrivate;
private int myPrivate;
protected int myProtected;
}
```**AVOID:**
```csharp
private int _myPrivateVariable
```**PREFER:**
```csharp
private int myPrivateVariable
```Static fields are the exception and should be written in **PascalCase**:
```csharp
public static int TheAnswer = 42;
```
### PropertiesAll properties are written in **PascalCase**. For example:
```csharp
public int PageNumber
{
get { return pageNumber; }
set { pageNumber = value; }
}
```### Parameters
Parameters are written in **camelCase**.
**AVOID:**
```csharp
void DoSomething(Vector3 Location)
```**PREFER:**
```csharp
void DoSomething(Vector3 location)
```Single character values are to be avoided except for temporary looping variables.
### Actions
Actions are written in **PascalCase**. For example:
```csharp
public event Action ValueChanged;
```### Misc
In code, acronyms should be treated as words. For example:
**AVOID:**
```csharp
XMLHTTPRequest
String URL
findPostByID
```**PREFER:**
```csharp
XmlHttpRequest
String url
findPostById
```## Declarations
### Access Level Modifiers
Access level modifiers should be explicitly defined for classes, methods and member variables.
### Fields & Variables
Prefer single declaration per line.
**AVOID:**
```csharp
string username, twitterHandle;
```**PREFER:**
```csharp
string username;
string twitterHandle;
```### Classes
Exactly one class per source file, although inner classes are encouraged where scoping appropriate.
### Interfaces
All interfaces should be prefaced with the letter **I**.
**AVOID:**
```csharp
RadialSlider
```**PREFER:**
```csharp
IRadialSlider
```## Spacing
Spacing is especially important in kodeco.com code, as code needs to be easily readable as part of the tutorial.
### Indentation
Indentation should be done using **spaces** — never tabs.
#### Blocks
Indentation for blocks uses **4 spaces** for optimal readability:
**AVOID:**
```csharp
for (int i = 0; i < 10; i++)
{
Debug.Log("index=" + i);
}
```**PREFER:**
```csharp
for (int i = 0; i < 10; i++)
{
Debug.Log("index=" + i);
}
```#### Line Wraps
Indentation for line wraps should use **4 spaces** (not the default 8):
**AVOID:**
```csharp
CoolUiWidget widget =
someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line);
```**PREFER:**
```csharp
CoolUiWidget widget =
someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line);
```### Line Length
Lines should be no longer than **100** characters long.
### Vertical Spacing
There should be exactly one blank line between methods to aid in visual clarity
and organization. Whitespace within methods should separate functionality, but
having too many sections in a method often means you should refactor into
several methods.## Brace Style
All braces get their own line as it is a C# convention:
**AVOID:**
```csharp
class MyClass {
void DoSomething() {
if (someTest) {
// ...
} else {
// ...
}
}
}
```**PREFER:**
```csharp
class MyClass
{
void DoSomething()
{
if (someTest)
{
// ...
}
else
{
// ...
}
}
}
```Conditional statements are always required to be enclosed with braces,
irrespective of the number of lines required.**AVOID:**
```csharp
if (someTest)
doSomething();if (someTest) doSomethingElse();
```**PREFER:**
```csharp
if (someTest)
{
DoSomething();
}if (someTest)
{
DoSomethingElse();
}
```
## Switch StatementsSwitch-statements come with `default` case by default (heh). If the `default` case is never reached, be sure to remove it.
**AVOID:**
```csharp
switch (variable)
{
case 1:
break;
case 2:
break;
default:
break;
}
```**PREFER:**
```csharp
switch (variable)
{
case 1:
break;
case 2:
break;
}
```## Language
Use US English spelling.
**AVOID:**
```csharp
string colour = "red";
```**PREFER:**
```csharp
string color = "red";
```The exception here is `MonoBehaviour` as that's what the class is actually called.
## Copyright Statement
The following copyright statement should be included at the top of every source file:
/*
* Copyright (c) 2023 Kodeco Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
* distribute, sublicense, create a derivative work, and/or sell copies of the
* Software in any work that is designed, intended, or marketed for pedagogical or
* instructional purposes related to programming, coding, application development,
* or information technology. Permission for such use, copying, modification,
* merger, publication, distribution, sublicensing, creation of derivative works,
* or sale is expressly withheld.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/In this repository, copy the **ScripTemplates** folder into your own Unity **Assets** folder. This way the header above will be included in new scripts.
> **NOTE**: You may need to close and reopen Unity in order for it to start picking up the template.
## Smiley Face
Smiley faces are prominent style feature of the kodeco.com site!
It's important to have the correct smile signifying the immense amount of happiness and excitement for the coding topic. The closing square bracket ] is used because it represents the largest smile able to be captured using ASCII art. A closing parenthesis ("**:)**") creates a half-hearted smile, and thus is not preferred.**AVOID**:
:)
**PREFER**:
:]
> **NOTE**: Do not use smileys in your scripts.## Credits
This style guide is a collaborative effort from the most stylish
kodeco.com team members:- [Darryl Bayliss](https://github.com/DarrylBayliss)
- [Sam Davies](https://github.com/sammyd)
- [Mic Pringle](https://github.com/micpringle)
- [Brian Moakley](https://github.com/VegetarianZombie)
- [Ray Wenderlich](https://github.com/rwenderlich)
- [Eric Van de Kerckhove](https://github.com/BlackDragonBE)