DEV Community

Cover image for Demystifying Methods and Functions in C# - When should I use them?
nineismine
nineismine

Posted on

Demystifying Methods and Functions in C# - When should I use them?

When shoul

When should I create a new method when programming in C# (or other languages)?

Often as a software engineer you will be faced with the question of… “When should I create a new method?”

The simple answer is: "When you encounter a verb."

 

What's that? A Verb? I hated English class!

Don't worry! I hated it English class too!

To break this down let's start with a definition of a verb.

VERB
a word used to describe an action, state, or occurrence, and forming the main part of the predicate of a sentence, such as hear, become, happen.

OK so what do verbs have to do with programming?

Let’s break that down a bit!

Let's say that we wanted to create a program with a player character, and of course when we have a player, we probably want to have that player do stuff right?
One of the things that we want to do might be to have the player move around. We might have a system where we trap the screen refresh or maybe we have a timer ticking.

Whatever the case might be we have created an update method that runs at some interval and named it Update().

In our program we are going to create a method called MoveCharacter(). We could just add the movement code to the Update() method but then later in our program, we might need to write the exact same code again (if we need to move again).

This is a somewhat simple example but this type of situation comes up a lot when you are programming.

Over my years of doing computer programming I have often asked myself if whatever I was working on should be contained in a more general method. (like Update)

This question gets easier when you start to think of the programming terms like sentences. When we start to think of examples of things we do as programmers in these kinds of terms it removes the guess work.

When it comes to deciding on when to create a new method you can can think of the methods as verbs. “Brad wants to run”, “Brad attacks”…. In both of these sentences, if we are writing code to make our player do some action, it makes sense to create a method for both verbs.

 

Let’s take this one step farther now.

Let’s say that we want to write this code to handle PlayerMovement or “Move” (Player) if we are keeping with the Verb reference.

Each time we get a frameUpdate, we want to evaluate if the user pressed a button to tell the computer to Move() (our verb!).
What might happen in the Move() method then?
Well as we discussed earlier, when we move, we need change our players X and Y position.

void MoveCharacter()
{


player.MovePostion(currentPostion +change)


}

 

So when we Move(), we are actually telling the computer to move our player, some amount of degrees X, and move our player some amount of degrees Y. (There are those dang verbs again!)
We can boil that down to MoveX and MoveY. (TWO Verbs!)

If we look back at the psuedocode above, what do we see ?

We have a method that Moves the character and inside of it we have ANOTHER method call, which handles our MoveX and MoveY.

Why is there another method there? Because we encountered another verb. (MoveX and MoveY)

I can’t say that this is a hard and fast rule because when it comes to programming, there will always be exceptions to every rule. But if you can take that simple statement with you then you have a good baseline to answer the question.

“When should I create a new method?”

Hope that helps this next part make a little more sense for you!

If you like this content , or it helped you to develop your skills... head on over to my blog.. Follow me on social media.. there is plenty more where this came from!

Top comments (0)