Clean code

Clean Code.
image by dashu83 - www.freepik.com

Clean code is about writing understandable, concise code. Therefore, code should follow conventions that apply project-wide. Sticking to clean code is mandatory.

Table of contents


Scope of clean code rules

Clean code rules are mandatory for a big software project. However, the exact rules are a personal choice. The project team must agree on them. The rules presented here are applied to all MMO-blueprint code examples.


Case Conventions

Many programming languages are case-sensitive. Case conventions improve readability.

PascalCase

namespace Vortex

class Timestamp

void IsReady()

interface ISpellSlot

camelCase

int hitCount;

UPPER_CASE

const BYTE_SIZE_OF_FLOAT = 4;

Conditionals

When to use brackets.

if (value = true)
{
    value = false;
    i++;
}

if (value = true)
    i++;

Special variables

Naming rules for special variables.

//variables with a dimension
float height_Meters;
double time_Milliseconds;

//unique identifier
Int64 UID;

//session identifier
Int32 SID;

//bool
bool isReady;
bool isIdentified;

//Collections
Dictionary<Int32, Creature> allCreatures;
List<Spell> allSpells;

Special Classes

Some classes serve a similar structure or purpose.

Processor classes


Enumerations

In normal use, you would not assign values to enumerations. However, in an MMORPG enumerations are often sent via network or stored in databases.

Network sending requires knowing the exact size of data. Database storage requires keeping consistent data values between program versions. Therefore:

  • the first enum value is always UNDEFINED = 0
  • assign a value to every enum
  • normal enums are Int16
  • BitFlag enums are Int64

The name of an enum is in singular.

public enum AccessMode : Int16
{
    UNDEFINED = 0,
    READONLY = 100,
    WRITEONLY = 200,
    READ_AND_WRITE = 300
}

public enum TargetTypeFlags : Int64
{
    EMPTY                   = 0x00000000,
    SELF                    = 0x00000001,
    SINGLE_FRIENDLY_TARGET  = 0x00000002,
    SINGLE_HOSTILE_TARGET   = 0x00000004,
    AOE_AROUND_TARGET       = 0x00000008,
    AOE_AROUND_CASTER       = 0x00000010,
    AOE_IN_AREA             = 0x00000020,
    CONE_90                 = 0x00000040,
    CONE_180                = 0x00000080,
    GROUP                   = 0x00000100,
    RAID                    = 0x00000200,
}

Instance variable versus method variable

class MessageClass
{
    public string message;
    
    void LogToConsole()
    {
        Console.WriteLine(message);
    }   
    
    void LogToConsole(string message)
    {
        Console.WriteLine(message);
    }   

    void LogToConsole(string message)
    {
        Console.WriteLine(this.message + message);
    }    
}

Comments

Avoid comments in your code. The code itself should be your comment. DonĀ“t strive for the shortest code. Rather, strive for the most readable and understandable code.

Not so:

//if the spell is ready
if (SpellCD() < 750)
{
    //set player spell ready
    player.SpRdy();
}

Rather so:

if (SpellIsReady())
{
    player.SetSpellReady();
}

double standardSpellCooldown_Milliseconds = 750;

bool SpellIsReady()
{
    return SpellCD() < standardSpellCooldown_Milliseconds;
}

Checklist

These rules are specific to the examples of MMO-Blueprint. You may have other rules. The important thing is to have clean code rules at all, and have all team members agree to them.

Clean code rules ideally are supported by your IDE and applied to your project from the first code line.

When rules change and you want to apply new rules to old code parts, use this as an opportunity to refactor.


Server Sid
Server Sid

The person that reads your code most is you!

(Last Updated on May 2, 2021)

Leave a comment

Your email address will not be published.