Posts Tagged interface

Closures Without the GC

Tags: , , ,

Closures allow you to save the local variables of a function and access them later in a callback. Think of how lambdas can access the local variables of the function they’re declared in, even though the lambda itself is another function. Unfortunately, creating a lambda like this creates garbage for the GC to collect and you have no control over that process. Today’s article introduces an alternative that allows you to take control over the GC and still use nice, type-safe closures. Read on to learn how!

Read the rest of this article »

1 Comment

Unit Testing Code That Uses the Unity Engine: Part 2

Tags: , , , ,

Last week’s article showed a technique that you can use to abstract the Unity engine so that you can test code that uses it. Today’s article presents another technique that allows you to remove this abstraction layer so your game code is faster and more natural. Read on to learn how!

Read the rest of this article »

No Comments

Unit Testing Code That Uses the Unity Engine: Part 1

Tags: , , ,

How do you write unit tests for code that uses the Unity engine to play sounds, make web calls, or render graphics? Today’s article shows one solution!

Read the rest of this article »

2 Comments

Replacing Events in MV-C

Tags: , , , ,

This article shows you how to remove events from the MV-C design pattern to make it simpler to understand, safer to use, and faster to execute. It’s a step-by-step lesson in refactoring. Start by reading the MV-C article, then read on!

Read the rest of this article »

22 Comments

The Problems with Events

Tags: , , ,

There’s no question that the for loop is a good idea, but events are much more complex. They’re enshrined into C# by the event keyword, but not everything about them is good. Today’s article shows some considerations you should take into account when deciding whether or not to use an event. Bonus: it includes some little extension methods to make using events and delegates easier!

Read the rest of this article »

No Comments

Introducing MV-C: A Unity-Specific Design Pattern

Tags: , , , , , , ,

Last year I introduced a Unity-based model-view-controller (MVC) design pattern and in the many comments on that article, a theme arose. The “model” part of MVC is arguably not necessary since Unity stores so much of the data itself. Today’s article takes that theme and elaborates on it to create and introduce a new Unity-specific design pattern. Read on to see how this adaptation of MVC works!

Read the rest of this article »

17 Comments

Iterators vs. Callbacks: Performance and Garbage

Tags: , , , , , , ,

Iterator functions and their ability to yield return values then continue on really come in handy for a variety of situations. Unfortunately, they come with some pretty serious performance and garbage creation drawbacks. So today’s article explores alternatives in various forms of callbacks: delegates, interfaces, and classes. Can they perform better than iterator functions? Can they avoid garbage creation? Read on to find out!

Read the rest of this article »

2 Comments

Hiding Unity Library Internals

Tags: , , ,

When writing code for a library, there is invariably some of it you want to hide from the users of the library. You want to keep the public API clean, but Unity makes this tough. Today’s article discusses a strategy for laying out your code so that users of the library aren’t burdened by classes, functions, and properties that they don’t need to know about. Read on to see how!

Read the rest of this article »

No Comments

Adding the const Keyword to C#

Tags: ,

Today’s article is not about the const keyword that C# already has. It’s about the const keyword that C++ has and how we can approximate it in C# to make our code safer. It’s a really powerful tool that’s often the default for C++ programmers, but we can take advantage of a similar strategy in C#. Read on to learn how!

Read the rest of this article »

3 Comments

Moving Methods out of Classes

Tags: , ,

In today’s article I’ll share a technique that can help you reason about your classes (and structs). The core idea is to move some methods out of the class into helper functions. Doing this can really simplify the class and simplify the functions so they’re much more easily understood by readers (including yourself!), more easily written, and more easily extended. Read on to learn more about this technique!

Read the rest of this article »

No Comments