Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# call a method whenever another method ends?

Tags:

c#

Although this question might sound stupid at first glance, please hear me out.

c#'s get{} and set{} methods are increadibly usefull in situations when you do not know how you porgramming goals will evolve while you build your code. I enjoyed their freedom many a time and now I wonder if there is something similar for methods but in a bit different light.

As I am working in gamedev, it is a very common practice to extend/update/improve existing code day-in day-out. Therefore, one of the patterns I taught myself to follow is to never use "return" statement more than once in the most of my methods.

The reason why I do this is to always be able to write something at the bottom of the method and be sure that the line I have written is always called 100% of the time once my method ENDS.

Here is an example:

    public void Update()
    {
        UpdateMovement();


        if (IsIncapacitated)
            return;

        if (IsInventoryOpened)
        {
            UpdateInventory();
            return;
        }

        if (Input.HasAction(Actions.Fire))
        {
            Fire();
            return;
        }
        else if (Input.HasAction(Actions.Move))
        {
            Move(Input.Axis);
            return;
        }


    }

Now imagine that this method is called dozens of times in many places across the entirety of your project. And then the next day you decide that you need to call UpdatePhysics() method at the very end of your Update() method. In this case there are only 4 returns, it could be much worse in reality.

Then imagine that such decesions happen several times a day every day. Bad planning you may say? I might agree with you, but I do think that freedom of development is essential in modern coding. I don't think you should kill yourself trying to anticipate every turn your project might take before you start writing code.

One way to insure that problems like the one I described above never happen is to rewrite the method as follows:

    public void Update()
    {
        UpdateMovement();


        if (!IsIncapacitated)
        {
            if (IsInventoryOpened)
            {
                UpdateInventory();
            }
            else
            {
                if (Input.HasAction(Actions.Fire))
                {
                    Fire();
                }
                else if (Input.HasAction(Actions.Move))
                {
                    Move(Input.Axis);
                }
            }
        }

    }

In this case you can always add a line at the bottom and be sure it will always get called nomatter what.

So I wanted to ask if there is another approach that could allow for placing "return"-s wherever you wish while still being able to add extra code easily at the bottom of the method any time. Maybe there is any form of syntax in c# that does it for you? Or maybe there is a better coding practice that eliminates such problem?

UPDATE: As I started receiving answers, I realized that I need to clarify things a bit.

  1. 'try/catch/finally' is an overkill - I will never use them. They have severe performance penalty on catch(), they screw up 'Edit and continue' feature in Visual Studio and they just look ugly.

  2. Idealy I need to be able to access local variables in the Update() method from any code I decide to add at the end of the method,

  3. When I wrote the question, I already had an answer - nesting. My second code sample has no returns and, therefore I can add code to the very bottom of the method and it will work 100% of the time, while I will be able to use local variables. Nesting is bad though, and that is why I am here searching for a BETTER solution.

UPDATE 2: I was actually mistaken about try/catch because I did not know that you can skip catch alongside it's performance penalties and only have finally. However, this solution is still worse than the nesting solution provided in the question, because in your newly added finally block you no longer can use return statements. So basically you can do whatever you want when you write the method the first time, but once you extend it - you are back to nesting.

like image 420
cubrman Avatar asked Jun 14 '16 12:06

cubrman


1 Answers

One simple suggestion is to wrap your function. For example:

public void UpdateCall()
{
   Update();
   AfterUpdate code goes here.
}
like image 188
Joe C Avatar answered Sep 20 '22 00:09

Joe C