Hi Trixie here. It’s Code Review time! Yay. Time to clean up that code and start really looking at all the crap work you’ve done over the last months and clean that sh!t up!
I’ll post our in-house Style Guide (which of course goes out the window when you are coding in anger) at the bottom of the post. Hopefully someone else will follow it or find it useful.
This is how the code review works:
- Make a list of all your scripts and the game objects they are attached to.
- Go through all the variables/functions/iterators and make them follow the standard in the Style Guide (which includes making logical names sensible to humans).
- Check all your Public references and make Private if you are really not using them.
- Trawl the console output and clean up the extraneous debugging guff.
- Start grouping your code into functions that do a similar thing and try and make them standardised.
- If you are lucky you will find some optimisations in there as well to make your game run faster, better, more efficient, lighter.
Then at least it will be another few months before you muck it up again.
I know there are automated tools to do some of this work but I prefer to work though my own methods. I like to have complete control over the process. My favourite tool for analysis is the Unix Command line (I know weird right?). My workstation is Windows 10 but I have a Cygwin like utility called MobaXterm installed which allows me to interrogate the file system like a Unix machine and use grep and other commands on all the files that make up my scripts.
Basically I want to build a big spreadsheet of information that lists all my scripts and the Game Objects they attach to and the Public interfaces and variables etc.
This is one that I started for Endless Elevator:
Then I start extracting Game Objects and Code loops using the Unix Command Line so I can start to build a picture of what’s going on.
One of the outcomes of this process is that I want to be left with a map of how stuff works that I can connect the dots on in the future. It’s kind of like a Design Document in reverse.
Here are a few examples of the commands I’m using and how the information is extracted:
# grep bool *.cs # Getting out all my bools – I tend to use true or false tests a lot to explicitly define functions and events.
As you can see above the bool names are mostly descriptive and the variable name is mostly in camelCase. Those few like swingme will get updated to swingMe and the overly generic “yes” and “yesNow” (what was I thinking!) will be made more descriptive of what we are really agreeing to in that code.
This command looks at all the sort of functions I am using and where. It’s nice to know which one’s don’t have an Update() function and where all my Collisions and Triggers are.
# egrep “{|}” AI.cs # I use something like this to collect an idea of the complexity of my loops and functions.
# egrep “{|}(|)|if|for|else” AI.cs # This is my favourite type of command. Like the command above I use it for working out the structure of a script. This is way easier than scrolling through lines and lines of code and comment and makes it really easy to spot areas where you have gone loopy crazy pants.
# grep GameObject *.cs # This one makes a good list of all the scripts that I reference another Game Object in. It helps build a visual map of what dependencies there are between objects.
# grep script *.cs # This one does a similar thing in that it grabs all the times that I am calling something from within another script attached to another Game Object..
The command line is also a nice quick way to check how many lines of code you have written.
There are some good Style Guides out there.
I like this one for it’s organised folder structure and naming convention for files: https://github.com/stillwwater/UnityStyleGuide
I like the Microsoft C# one for layout and block style: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions
I like this for it’s use of camelCase and Capitals and it’s preferred block style: https://github.com/raywenderlich/c-sharp-style-guide
As a basic guide though I always try and mimic the Unity API manual. For example this page for OnCollisionEnter:
// A grenade
// - instantiates an explosion Prefab when hitting a surface
// - then destroys itself
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform explosionPrefab;
void OnCollisionEnter(Collision collision)
{
ContactPoint contact = collision.contacts[0];
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 position = contact.point;
Instantiate(explosionPrefab, position, rotation);
Destroy(gameObject);
}
}
The comments are appropriate. The Class name uses a Capital for each word. The curly brackets are on a new line and indented block space. Varables are in camelCase.
Here is our minimal Style Guide. This is the basics of how we roll things:
Thanks for reading. See you all in the New Year… Trixie.