Handling Errors Without Exceptions: Part 2

Tags: , , , ,

Last week’s article introduced the Either class as an alternative to exceptions that makes it easy for functions to declare their error results in addition to their success results and for callers of those functions to handle both results. Today we’ll go further by linking together multiple functions to handle all the error cases almost transparently. Read on to learn how to make the most out of Either!

Read the rest of this article »

No Comments

Handling Errors Without Exceptions: Part 1

Tags: , , ,

Exceptions are the de facto way to handle errors in C#, but they have problems. Callers don’t know if the function they’re calling will throw an exception at all or which types of exceptions it’ll throw. Exceptions also introduce an alternative control flow that’s often hard for programmers to follow. They make our code slower too, even when never thrown! Today’s article introduces an alternative to exceptions to help solve all of these issues. Read on to learn a new way to handle errors!

Read the rest of this article »

2 Comments

Event Performance: C# vs. UnityEvent

Tags: , ,

Unity programmers have their choice of two kinds of events. We could use the built-in C# event keyword or Unity’s UnityEvent classes. Which is faster? Which one creates more garbage? Today’s article finds out!

Read the rest of this article »

9 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

Adding Unions to C#

Tags: ,

C and C++ have a great feature call the “union”. It’s like a struct except it only has one of the fields at a time. C# lacks this feature, but with some trickery it can be added in. Today’s article shows how to do that!

Read the rest of this article »

No Comments

File I/O Performance Tips

Tags: , , , , , , ,

At some point, every project ends up reading or writing to the file system. If you do anything more than storing a single blob of bytes (e.g. JSON text) then you’ll need to be very careful about performance. It’s easy to accidentally write code that takes way longer to read and write than it should and you won’t get any help from the compiler or from Unity. Today’s article reveals some of these traps so you won’t fall into them!

Read the rest of this article »

No Comments

When Structs Create Garbage

C# has a huge advantage over languages like Java and AS3: the struct. It allows us to create a value type with multiple members and avoid creating garbage every time we create one with new. At least that’s the simple version of the story. In reality, it’s really easy to accidentally create garbage with struct. Today’s article shows how to use struct so that you don’t create any garbage and reveals some of the traps so you won’t fall into them.

Read the rest of this article »

No Comments

More JSON Performance Benchmarks

Tags: , , , ,

Last week’s article benchmarked Unity 5.3’s new JsonUtility class against third-party alternatives LitJSON and Json.NET. JsonUtility came out the clear winner, but the question arose about how JsonUtility would fare with bigger or more complex JSON structures. Today’s article answers that question by benchmarking with more types of JSON documents to find out if JsonUtility can maintain its lead.

Read the rest of this article »

3 Comments

JSON Performance Benchmarks

Tags: , , ,

Unity 5.3 came out this week and introduced a new, built-in JSON serializer library. Today’s article compares its performance against some popular third-party JSON serializer libraries to see if Unity’s version is any faster. Read on for the results!

Read the rest of this article »

3 Comments

Making Enums More Flexible and Extensible

Tags: , ,

Enums are great at what they do: creating a simple integer type with specific values. Their main purpose is to choose one value out of many like enum Color { Red, Green, Blue }. But what if you have data attached to those choices? What if the data is one type for one choice and another type for another choice? What if there are two pieces of data to attach to one choice and only one for another? Today’s article shows a simple pattern you can use instead of enum in these cases to get a lot more flexibility and extensibility. Read on to see how!

Read the rest of this article »

4 Comments