Why and when to use static classes and methods

A friend asked me a question, a while ago, stating:

Hey Jan, One stupid question around which I have thought a lot and often get stuck while deciding. When to make a function static/non-static specifically the helpers or utility ones.

I read 2 arguments

  1. All the functions that don’t need to use a state of the object, (don’t update any variable value of the object ) should be static.
  2. All the functions that should/can be test independently and have some logic. Not only transformation or mapping. Should be non-static.

These 2 arguments are against each other and are often confusing, and mostly in the context of our repo. We often use multiple resources and their clients to implement a logic. Now these mostly don’t update any state of an object, mostly the value of the attributes of the class are read, never updated. And it does make sense to test them independently

One example Reading from the storage account, filter data based on some logic and then updating that in, let’s say, a Function App.

Now, how do you decide?

Let me start by: There’s no such thing as a stupid question in engineering! These are the type of questions everyone is wondering about, but only few dare to ask. Iris Classon has done an entire blog-series on these ‘not so stupid questions’.

Read more →