Demystifying Methods and Functions in C# - When should I use them?
Tags: csharp, beginners, codenew
Published: 2020-02-18 03:15:48

When shoul<h3><img class="wp-image-1024 aligncenter" src="https://www.thedevelopingdeveloper.net/wp-content/uploads/2020/02/images.jpeg" alt="" width="402" height="226" /></h3>
<h3><span id="When_should_I_create_a_new_method" class="ez-toc-section">When should I create a new method when programming in C# (or other languages)?</span></h3>
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."

&nbsp;
<h5>What's that? A Verb? I hated English class!</h5>
Don't worry! I hated it English class too!

<a href="https://www.lexico.com/en/definition/verb">To break this down let's start with a definition of a verb.</a>

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

OK so what do verbs have to do with programming?
<h2>Let’s break that down a bit!</h2>
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.

&nbsp;
<h2>Let’s take this one step farther now.</h2>
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.
<div class="EnlighterJSWrapper enlighterEnlighterJSWrapper">
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">void MoveCharacter()
{


player.MovePostion(currentPostion +change)


}</pre>
&nbsp;

</div>
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... <a href="https://www.thedevelopingdeveloper.net/">head on over to my blog</a>.. Follow me on social media.. there is plenty more where this came from!