Coding Standards And Practices - Niilo007/RimWorld-NQoL GitHub Wiki
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NQualityOfLife;
using NQualityOfLife.MiscFixes;
using NQualityOfLife.Settings;
using NQualityOfLife.Utils;
using UnityEngine;
#nullable enable
namespace NQualityOfLife
{
}
[DevTool("QA", true)]
private static bool ToListTests()
{
int t = 0, s = 0, f = 0;
Stopwatch stopwatch = Stopwatch.StartNew();
//...
stopwatch.Stop();
return QA.Utils_.PrintTestResults("ToListTests", t, s, f, stopwatch);
}
- If the patch is applied with a
ApplyPatchesIfEnabled
method then the patch class must inherit fromHarmonyLib.HarmonyPatch
due to the wayApplyPatchesIfEnabled
is implemented. - All the input parameters that can be null should be marked nullable.
- Non static patch classes should be sealed.
[PatchIfEnabled(alwaysEnabled: true, message: "debug always enabled")]
[HarmonyPatch(typeof(Verse.ThingDef), nameof(Verse.ThingDef.HasComp), typeof(Type))]
internal sealed class GetCompValidator_Patch : HarmonyPatch
{
[HarmonyPrefix]
internal static void Prefix(Type? compType, Verse.ThingDef? __instance)
{
}
}
- Patch classes can be static if they are applied conditionally with the PatchIfEnabled attribute
[PatchIfEnabled(alwaysEnabled: true, message: "debug always enabled")]
[HarmonyPatch(typeof(Verse.ThingDef), nameof(Verse.ThingDef.HasComp), typeof(Type))]
internal static class GetCompValidator_Patch
{
[HarmonyPrefix]
internal static void Prefix(Type? compType, Verse.ThingDef? __instance)
{
}
}
Here will be important definitions for general coding and for NQoL
A shallow copy creates a new object with the same values as the original, but it does not create copies of the objects referenced by the original. Instead, both the original and the copy share references to the same instances of any reference-type fields. This means changes to shared referenced objects will affect both the original and the copy.
A deep copy is a type of copy where all objects and their references within a class are duplicated, creating an entirely new instance with its own distinct copies of the referenced objects. This ensures that changes made to the copy do not affect the original, and vice versa, as they are completely independent instances.
A pure method is a function whose output depends only on its input parameters and has no observable side effects. It does not read from or modify any external state, global variables, I/O systems, or mutable data structures. This makes it predictable, testable, and safe to call multiple times with the same arguments without changing behavior or causing unintended consequences.
int Add(int a, int b) => a + b;
An impure method performs operations that go beyond returning a result based solely on its inputs. It may modify external state, perform I/O (e.g., logging, file access), use randomness, depend on time, or mutate input parameters. Impure functions are often necessary in real-world applications, but they are less predictable and harder to test.
int GetRandomValue() => new Random().Next();
An urgent/eager operation is executed immediately, regardless of whether its result is needed right away. It eagerly computes values or triggers side effects as soon as it is called. This is typical for methods that interact with game state, perform logging, I/O, or update systems in real time.
Calling .ToList()
on an IEnumerable<T>
forces immediate evaluation.
A lazy operation is deferred—it does not execute until its result is actually needed. This allows for performance optimizations by avoiding unnecessary work, especially in scenarios involving expensive computations, chained transformations, or conditional logic. Lazy evaluation is commonly used in things like IEnumerable, Lazy, and deferred LINQ queries.
IEnumerable<T>
sequences are not evaluated until iterated.
Evaluation is the process of executing an expression or function to produce a result. In programming, this means resolving the value of variables, computing expressions, or performing operations that yield output. Evaluation can be eager (immediate) or lazy (deferred), and may or may not involve side effects depending on whether the logic is pure or impure.
2 + 2 evaluates to 4; calling a function evaluates its body.
Iteration refers to the process of repeatedly executing a block of code—typically over elements in a collection (like a list or array). Each repetition is called an iteration, and the term iterated describes something that has been processed during one of those repetitions.
In programming, iteration is most commonly done using constructs like for, foreach, while, or with LINQ in C#.
In foreach (var item in list), each item is one iterated element during an iteration over list.
Mutation refers to the act of changing the state or contents of an existing object or data structure after it has been created. This can involve modifying values in a collection, updating the fields of an object, or changing the contents of a variable that holds a reference to mutable data.
In contrast to immutable operations (which produce new values), mutation has side effects, meaning it changes something that exists outside the local scope of the function or method.
list.Add(5); is a mutation because it modifies the original list by adding an element.
x = x + 1; is not a mutation if x is a primitive type, but it is a mutation if x is a field in an object being changed.
Recursion is a programming technique in which a function calls itself in order to solve a problem by breaking it down into smaller or simpler subproblems of the same type.
A recursive function typically includes:
- A base case, which ends the recursion.
- A recursive case, where the function calls itself with modified arguments.
Calculating factorial: factorial(n) = n * factorial(n - 1) with the base case factorial(0) = 1.
Recursive describes a function, process, or definition that involves or uses recursion. It refers to the self-referential nature of the structure or behavior.
A directory search algorithm that looks inside subdirectories is recursive.