W2D3: Coding masterclass (watch even if you are not a gamedev)
Lucian's five-hour C# masterclass — variables to design patterns — covering loops, classes, code smells, SOLID, and a hand-built event bus in Unity.
Lucian Lazar · 17 Jul 2024
This is the long one. Week 2 Day 3 is a standalone coding session that happens to use Unity as its playground, and Lucian says up front that any programmer from any language can sit in and come out ahead. The first third is beginner C# — variables, functions, classes, loops. The rest is the part you actually come back to in five years: best practices, code smells, SOLID, and a walk through fourteen design patterns with live code for most of them.
What’s covered
C# fundamentals, fast
- Variables and types:
int,float,double,decimal,string,bool. Why floats lose precision as numbers get bigger, why finance code usesdecimal, and whyuintgets double the positive range. - The
=sign as an instruction, not an assertion. Compute the right, store it in the left. public/private/protected, and whyvarexists (“what people do with all the time they save by using var instead of uint”).- Functions vs. methods, parameter lists, Pascal case vs. camel case, static vs. instance, and what
throwactually does to execution.
Classes and Unity plumbing
- Classes as blueprints. Constructors, fields vs. local variables, and the
_underscoreconvention for private fields (Lucian disagrees with the “Rider colours it for you” crowd). - Why a script needs to inherit
MonoBehaviourbefore you can drag it onto a GameObject, demonstrated live with a button’s OnClick event. - Loops:
for,while,do-while,foreach, plus a detour intoIEnumerator,yield return nulland how coroutines stop Unity from freezing.
Practices that outlive the language
- YAGNI, KISS and DRY — including the caveat nobody teaches: two identical blocks that will change for different reasons tomorrow should stay duplicated.
- Composition over inheritance, worked through a coffee machine that takes a money grabber, sugar adder and water source instead of inheriting from all three.
- Encapsulation by default, premature optimization, feature creep, and not betting two years of development on the newest Unity version.
- Hard size limits: under 500 lines per class, under 50 per method, five parameters max.
SOLID, code smells, design patterns
- All five SOLID principles with the square/rectangle example for Liskov.
- Code smells: large class, long method, primitive obsession, data clumps, god object, shotgun surgery, feature envy, divergent change, speculative generality, inappropriate intimacy, refused bequest, message chains.
- Patterns coded live: singleton, factory method, strategy, observer, decorator, adapter, command, state, template method, proxy, chain of responsibility, facade, builder, and a full generic event bus.
Timestamps
- 00:04:30 — What this session is and why non-gamedevs should stay; beginner and advanced material in the same stream
- 00:09:00 — Why Unity uses C#, the “C# is Microsoft Java” take, and a recommended 4-hour deep-dive course link
- 00:14:00 — Top-down thinking: start from the result and work backwards; writing code should feel like translating, not inventing
- 00:18:00 — Variables,
int/float/double/decimal, precision limits and value ranges - 00:30:00 — Strings, concatenation, and what
=really means at runtime - 00:36:00 —
public,private,protected, instances, and when to usevar - 00:45:00 — Unity types in practice: GameObject, Transform,
Vector3.Distanceand static methods explained - 00:55:00 — Functions: parameters, return types, pure vs. stateful,
throw, and Pascal vs. camel case - 01:15:00 — Classes as blueprints; inheriting MonoBehaviour and wiring a method to a UI button’s OnClick
- 01:30:00 — Constructors, fields vs. variables, and the underscore naming convention
- 01:45:00 — Loops end to end:
for,while,do-while,foreach, plus coroutines andyield - 02:15:00 — Lists, zero-based indexing, and spawning prefabs at random positions with
Instantiate - 02:25:00 — Writing a sprite animator by hand:
Time.deltaTime, modulo wrapping, assigning frames in the Inspector - 02:45:00 — YAGNI, KISS, DRY — and the case where duplicating code is the right call
- 03:05:00 — Composition over inheritance with the coffee machine;
virtual/overrideand why encapsulation matters - 03:25:00 — Premature optimization, feature creep, and staying two years behind the latest Unity
- 03:35:00 — Size limits and the full SOLID walkthrough
- 04:00:00 — Code smells, one by one, with examples
- 04:25:00 — Design patterns begin: singleton, factory, strategy, observer
- 04:50:00 — Decorator, adapter, command, state, template method, proxy, chain of responsibility, facade, builder
- 05:15:00 — Building a generic event bus from scratch, plus dependency injection and the wrap-up
What you build
Nothing ships at the end of this one — that’s the point. What you do write, live, alongside Lucian:
- A frame-by-frame sprite animator driven by
Time.deltaTimeand a modulo-wrapped index, with the frame list assigned through the Inspector lock trick. - A coroutine that prints while a public bool stays true, toggled from the Inspector mid-play so you can watch the loop exit.
- A coffee machine refactored from three inherited base classes into constructor-injected components.
- A
Builderwith chainedWithIcon()/WithAttackAlgo()calls returningthis. - A generic event bus using a
Dictionary<Type, List<Action<object>>>, including the lambda cast that lets a typed subscriber sit in an untyped list.
void Update()
{
_timer += Time.deltaTime;
if (_timer >= 1f / frameRate)
{
_index = (_index + 1) % frames.Length;
spriteRenderer.sprite = frames[_index];
_timer = 0f;
}
}
```csharp
public void Subscribe<T>(Action<T> subscriber)
{
if (!_subs.TryGetValue(typeof(T), out var subs))
{
subs = new List<Action<object>>();
_subs[typeof(T)] = subs;
}
subs.Add(o => subscriber((T)o));
}
## Who should watch this
Anyone who can already write a loop but has never had someone explain why their 900-line manager class is a problem. It's equally useful if you've been shipping C# for three years and have never heard "refused bequest" or "shotgun surgery" named out loud — Lucian bets you won't know at least one thing on the list. Absolute beginners will find the second half heavy going, but that's what the timestamps are for; save it and come back when you're moving from junior to mid.
Part of the full curriculum. Course overview.