In This Article:
🤔 Notice something is missing or not working? → Please contact Zoel (zoelbastianbach@agate.id) on Ms. Teams!
https://embed.notionlytics.com/s/TTBVNGRreGhSV00zYm5sRE9IWkJObWRRV2pjPQ==
Coding standards are collections of coding rules, guidelines, and best practices. It gives a uniform appearance to the codes written by different engineers and developers, and improves readability and maintainability of the code while reducing the complexity. It also helps in code reuse and helps to detect error.
Coding style causes the most inconsistency and controversy between developers. Each developer has a preference, and rarely are two the same. However, consistent layout, format, and organization are key to creating maintainable code. The following sections describe the preferred way to implement C# source code in order to create readable, clear, and consistent code that is easy to understand and maintain
❌ NEVER declare more than 1 namespace per file.
❌ AVOID putting multiple classes in a single file.
Try to place curly braces ({ and }) on a new line. But try to maintain code readability
Try to keep on using curly braces ({ and }) in optional conditional statements.
✅ ALWAYS use a Tab & Indention size of 4.
✅ ALWAYS declare each variable independently (not in the same statement)
// **❌** DO NOT do this:
public int HitCount, DeathCount;
// **✅** DO this instead:
public int HitCount;
public int DeathCount;
Place namespace “using” statements together at the top of file. Group .NET namespaces above custom namespaces.
Group internal class implementation by type in the following order:
Sequence declarations within type groups based upon access modifier and visibility:
Segregate interface Implementation by using #region statements.
Recursively indent all code blocks contained within braces.
Only declare related attribute declarations on a single line, otherwise stack each attribute as a separate declaration.
// **❌** DO NOT do this:
[Attrbute1, Attrbute2, Attrbute3]
public class MyClass {…}
// **✅** DO this instead:
[Attrbute1, RelatedAttribute2]
[Attrbute3]
[Attrbute4]
public class MyClass {…}
Place the following attribute declarations on a separate line:
All comments should be written in the same language (English preferred), be grammatically correct, and contain appropriate punctuation.
✅ ALWAYS use // or /// but ❌ NEVER / … /
❌ DO NOT “flowerbox” comment blocks.
// ***************************************
// Comment block
// ***************************************
✅ USE inline-comments to explain assumptions and known issues, and also algorithm insights if the algorithm is too complex or it is being optimized (so it is difficult to understand).
Only use comments for bad code to say “fix this code” – otherwise remove, or rewrite the code!
Include comments using Task-List keyword flags to allow comment-filtering.
// TODO: Place Database Code Here
// UNDONE: Removed P\Invoke Call due to errors
// HACK: Temporary fix until able to refactor
Try to apply C# comment-blocks (///) to public, protected, and internal. But don’t rely just from comment to explain your code.
Only use C# comment-blocks for documenting the API.
✅ ALWAYS include <summary> Include <param>, <return>, and <exception> comment sections where applicable.
/// <summary>
/// The loaded map.
/// </summary>
public MapData LoadedMap;
/// <summary>
/// Switchs the scene to specified scene’s name
/// </summary>
/// <returns><c>true</c>, if scene was switched, <c>false</c> otherwise.</returns>
/// <param name="sceneName">Scene name</param>
public bool SwitchScene(string sceneName)
{…}
Try to include <see cref=""/> and <seeAlso cref=""/> where possible.
❌ DO NOT omit access modifiers. Explicitly declare all identifiers with the appropriate access modifier instead of allowing the default.
// **❌** DO NOT do this:
void WriteEvent(string message) {…}
// **✅** DO this instead:
private void WriteEvent(string message) {…}
❌ AVOID typing long code in one line (max 120 characters per line).
Try to wrap repeated code block that occurred on more than one part into a function
//check all tile except obstacle’s tile to be manipulated
for each (tile:Tile in tileList)
{
if (tile.id != TILE_TREE && tile.id != TILE_ROCK && tile.id != TILE_RIVER)
{…}
}
//in some other place
if (tile.id != TILE_TREE && tile.id != TILE_ROCK && tile.id != TILE_RIVER)
{…}
//check all tile except obstacle’s tile to be manipulated
for each (tile:Tile in tileList)
{
if (!IsObstacleID (tile.id)
{…}
}
...
//di suatu tempat lain
if (!IsObstacleID (tile.id)
{…}
bool IsObstacleID(int id)
{
return (id != TILE_TREE && id != TILE_ROCK && id != TILE_RIVER);
}
Try to initialize variables where you declare them.
// **❌** DO NOT do this:
//OtherFunction () not called if SomeFunction () return false
if(SomeFunction() && OtherFunction())
{…}
// **✅** DO this instead:
bool someResult = SomeFunction();
bool otherResult = OtherFunction();
if(someResult && otherResult)
{…}
✅ ALWAYS use the built-in C# data type aliases, not the .NET common type system (CTS).
Try to use int for any non-fractional numeric values that will fit the int datatype - even variables for nonnegative numbers. The use of int is common throughout C#, and it is easier to interact with other libraries when you use int. Also, uint, short and long are not shown on Unity Editor
Only use long for variables potentially containing values too large for an int.