- Notifications
You must be signed in to change notification settings - Fork110
kodecocodes/c-sharp-style-guide
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
- The Official kodeco.com C# Style Guide
This style guide is different from others you may find, because the focus iscentered on readability for print and the web. We created this style guide tokeep the code in our tutorials consistent.
Our overarching goals areconciseness,readability andsimplicity. Also, this guide is written to keepUnity in mind.
This style guide is based on C# and Unity conventions.
On the whole, naming should follow C# standards.
Namespaces are allPascalCase, multiple words concatenated together, without hyphens ( - ) or underscores ( _ ). The exception to this rule are acronyms like GUI or HUD, which can be uppercase:
AVOID:
com.kodeco.fpsgame.hud.healthbar
PREFER:
Kodeco.FPSGame.HUD.Healthbar
Classes and interfaces are written inPascalCase. For exampleRadialSlider.
Methods are written inPascalCase. For exampleDoSomething().
All non-static fields are writtencamelCase. Per Unity convention, this includespublic fields as well.
For example:
publicclassMyClass{publicintpublicField;intpackagePrivate;privateintmyPrivate;protectedintmyProtected;}
AVOID:
privateint_myPrivateVariable
PREFER:
privateintmyPrivateVariable
Static fields are the exception and should be written inPascalCase:
publicstaticintTheAnswer=42;
All properties are written inPascalCase. For example:
publicintPageNumber{get{returnpageNumber;}set{pageNumber=value;}}
Parameters are written incamelCase.
AVOID:
voidDoSomething(Vector3Location)
PREFER:
voidDoSomething(Vector3location)
Single character values are to be avoided except for temporary looping variables.
Actions are written inPascalCase. For example:
publiceventAction<int> ValueChanged;
In code, acronyms should be treated as words. For example:
AVOID:
XMLHTTPRequestString URLfindPostByID
PREFER:
XmlHttpRequestString urlfindPostById
Access level modifiers should be explicitly defined for classes, methods and member variables.
Prefer single declaration per line.
AVOID:
stringusername,twitterHandle;
PREFER:
stringusername;stringtwitterHandle;
Exactly one class per source file, although inner classes are encouraged where scoping appropriate.
All interfaces should be prefaced with the letterI.
AVOID:
RadialSlider
PREFER:
IRadialSlider
Spacing is especially important in kodeco.com code, as code needs to be easily readable as part of the tutorial.
Indentation should be done usingspaces — never tabs.
Indentation for blocks uses4 spaces for optimal readability:
AVOID:
for(inti=0;i<10;i++){Debug.Log("index="+i);}
PREFER:
for(inti=0;i<10;i++){Debug.Log("index="+i);}
Indentation for line wraps should use4 spaces (not the default 8):
AVOID:
CoolUiWidgetwidget=someIncrediblyLongExpression(that,reallyWouldNotFit,on,aSingle,line);
PREFER:
CoolUiWidgetwidget=someIncrediblyLongExpression(that,reallyWouldNotFit,on,aSingle,line);
Lines should be no longer than100 characters long.
There should be exactly one blank line between methods to aid in visual clarityand organization. Whitespace within methods should separate functionality, buthaving too many sections in a method often means you should refactor intoseveral methods.
All braces get their own line as it is a C# convention:
AVOID:
classMyClass{voidDoSomething(){if(someTest){// ...}else{// ...}}}
PREFER:
classMyClass{voidDoSomething(){if(someTest){// ...}else{// ...}}}
Conditional statements are always required to be enclosed with braces,irrespective of the number of lines required.
AVOID:
if(someTest)doSomething();if(someTest)doSomethingElse();
PREFER:
if(someTest){DoSomething();}if(someTest){DoSomethingElse();}
Switch-statements come withdefault case by default (heh). If thedefault case is never reached, be sure to remove it.
AVOID:
switch(variable){case1:break;case2:break;default:break;}
PREFER:
switch(variable){case1:break;case2:break;}
Use US English spelling.
AVOID:
stringcolour="red";
PREFER:
stringcolor="red";
The exception here isMonoBehaviour as that's what the class is actually called.
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 theScripTemplates folder into your own UnityAssets 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 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.
This style guide is a collaborative effort from the most stylishkodeco.com team members:
About
C# Style Guide for Game Tech tutorials
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.