Simple programming patterns and conventions

I thought it might be helpful to some developers to see some simple patterns and conventions in a variety of languages. Many of us know about more complex and abstract patterns, like the Abstract Factory Pattern or the Flyweight Pattern, but smaller patterns are rarely discussed outside of answers to questions.

These are some patterns and conventions that I tend to use in any code I write. They range from one-liners to a couple of methods, and are mostly understandable at first glance. I've avoided going too much into why someone might choose any given pattern, and leave it as an exercise to the reader to determine where these patterns might be useful for them.

As of now, I've only listed a single convention because I use it in a number of the patterns below. I'm sure the convention is controversial, so if you don't agree with it, please keep in mind that the meat of this post is in the patterns.

I will add other conventions and patterns in the future, and may add others from comments here or on reddit.

A final note on the code below. I did not enclose all code in the required class or method blocks in order to keep the focus on the patterns alone. Additionally, I've specified the language that each example is written for; many of them work just fine in other languages. If you have any questions on why a pattern doesn't make sense, or is not executing, don't hesitate to ask.

Enjoy!


Conventions


Branching flow-control statements

For branching flow-control statements, do not use braces and keep the code on a single line.

Return statements

Language: C#, most C-like languages with some contingencies

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Continuing in a loop

Language: C#, most C-like languages

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Breaking out of a loop

Language: C#, most C-like languages

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Patterns


Prevent a method from executing more than once

This is an easy way to prevent a method from executing multiple times. By checking a condition at the beginning, and then ensuring that condition is true immediately after, any subsequent calls will return immediately.

Language: C#, Java with some contingencies

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Using a switch statement as a compact alternative to if statements

Language: JavaScript

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Execute multiple using blocks without braces

Like any other block statement, in C#, using can be used without braces as well. This is especially useful to avoid excessive visual nesting.

Language: C#

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Avoid unnecessary awaits by passing the Task around

At least for me, using async/await in C# seemed to require making your methods that called asynchronous methods async themselves, and making those methods await. Instead, if your method does not need to await, you can make it return a Task and then pass the task returned from an asynchronous method wherever you need it. This is especially helpful if you need to use out or ref in a method that calls an asynchronous method.

Language: C#

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Prevent a method call with a specific type during compile time

I happened across this solution when clicking around Stack Overflow. Apologies for not crediting the solution; I don't recall where I found it.

You may have a method that accepts an abstract class, an interface, or some form of "base" class, but you want to restrict a certain subclass of that type from being used (perhaps the subclass is obsolete). Rather than checking the type during runtime and throwing an exception, you use this pattern to gain a compile time check in addition to a runtime check. This example uses constructors, but it works for methods as well.

By default, the Obsolete annotation will display a warning. You can configure msbuild or Visual Studio to treat warnings as errors, which will prevent the following code from compiling.

Language: C#

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Define and declare the type of arrays and objects in TypeScript

I think this is a problem and solution unique to TypeScript. You need to define a variable as an array or an object, but either you've disallowed implicity any, or you need to declare the type of the variable. There are, of course, other ways to accomplish this, but I believe this is the most compact way.

Language: TypeScript

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Execute multiple async tasks in parallel

You may need to execute a series of asynchronous tasks in parallel, and then await their result. There are a couple different ways I accomplish this, depending on how the tasks are created. You can use the pattern Avoid unnecessary awaits by passing the Task around here as well.

Language: C#

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX