The Felewin Coding Style is a Unity C# coding style that optimizes code legibility by encouraging programmers to explicitly display the context of each line of code, according to a consistent format. The aspects to follow are defined in this guide, and two script templates are also included which can be added to Unity.
This guide provides a reference for one or multiple programmers working together on a project. Whenever a programmer writes a script file, it should adhere to this style guide, in order to follow the Felewin Coding Style. By doing so, one programmer can interpret another programmer's script in a formulaic manner, because all scripts share a unified style. In this way, the structure of each script allows a programmer to learn its functionality through the context of each of its pieces.
To make class names easier to read and type, use single-word nouns whenever possible. For example, try 'Gun' instead of 'ProjectileShooter'.
This requires a bit of creativity sometimes, to create new words or even new meanings for words that already exist. For example, using 'Doodad' instead of 'InteractiveButInertToy'. The point is to come up with a one-worder that encompasses the nuances of the type of thing you are coding, so you can pack as much meaning in one word as possible.
This way, scripts for specific characteristics of such nouns will be much more straightforward to read and type, in turn. For example, 'DoodadSpawner' instead of 'InteractiveButInertToySpawner'.
In the 'InteractiveButInertToy' example, simply using 'Toy' won't cut it, because there are other Toys in the game, such as Units. Coming up with your one-worder is well worth it, once you get everyone else on board with your new definition.
Whenever possible, the name should describe a characteristic of the object it will be applied to, instead of an arbitrary happening which could be interpreted out of context. For instance, say a book is used to "imagine" different scenes in your game. You could give the book object a script called 'BookImagining', as opposed to 'SceneManager'. This tells developers that the script is used by the book. We know where to find it and where to put it. It also doesn't imply that there is some additional 'Scene Manager' or similarly named object, which would be unnecessary clutter. Certainly there is no reason to create a 'Scene Manager' object which tracks its position, rotation, and scale in the world space yet does not tangibly exist.
For example, use the name 'GoblinGoonBehavior' for a script that extends a script called 'UnitBehavior', but don't use the name 'GoblinGoonBrain' to extend from 'UnitBehavior'. In this way, you know intuitively that the 'GoblinGoonBehavior' script extends the 'UnitBehavior' script, because of the common word 'Behavior'.
Furthermore, if a 'Goblin Goon' is a particular kind of 'Unit', according to the design of the project, then that fact requires you to use the name 'GoblinGoonBehavior' instead of 'GoblinGoonUnitBehavior' (which would be redundant).
If the parent class is ever really long, then it may be impractical to include its name in the name of the extending class; there is a point at which brief naming must take precedence over this rule, which is up to the programmer's discretion.
Keep in mind that one doesn't need to understand the granular workings of the class, just the general idea of what purposes it achieves. Let the code commenting within speak for itself to explain the details of the operations involved to achieve the purposes of the class.
The purpose of this style aspect is to separate purposefully-distinct sections of code, as well as their corresponding comments, in order to discretize concepts for optimized legibility.
A variable is notated as having a certain type in order to easily recognize the use case of the variable.
For starters, a public variable is considered a "setting" because in Unity, public variables can be "set" in the inspector, by default. Here are some settings shown in the inspector:
A setting can be either local or global.
Local settings are those that show in the inspector local to a particular instance of a script.
Global settings are another category of setting, which apply for all instances of the class. They must be set within the code where they are declared. As such, they cannot be shown locally in the inspector. They can even be private, instead of public, as a result. When they are public, however, they are considered to be provided because they are intentionally being "provided" to other classes.
Another category of variable types is trackings. Trackings are simply variables keeping track of something. Unlike settings, they are intended to change over time as methods update them, instead of being set before the game starts. Trackings can either be private, settable, or provided, much like global settings, depending on whether they should be made available to other classes or if they are allowed to be set initially in the inspector like one would set a setting.
Connections are the final category of variable types. They are references made to other classes within the Unity environment. Manual connections are shown in the inspector, much like settings, whereas automatic connections are hidden, to be "automatically" connected to in the code, often in either the Awake or Start events.
These events are quite self-explanatory; however, it can help to comment as shown in the script made for example.
All of the stylizations listed above are guidelines for each programmer on the team to follow, such that the more they are followed, the easier time another member will have interpreting their code, since it will be more explicitly explained and also more uniform with the other scripts.
However, it is often unnecessary to include all stylizations to get the point across in a clean-enough way.
*At the very least, each script should follow the stylizations of:
Most of the unnecessary stylizations are further commenting stylizations. This is because elegant code can be self-explanatory enough to not need further comments. However, all scripts need to at least follow the patterns above which are those important for knowing how to read the script in the first place.
Here is an example script demonstrating the minimal stylization required in each script, inspired by an apple tree having and growing apples:
The remaining style aspects listed below are the ones that are only necessary as further clarification becomes needed:
If you add these two new C# script templates, you can easily create scripts that follow the Felewin Coding Style from the start:
To add the templates, put them here in your Unity installation directory: Editor\Data\Resources\ScriptTemplates