DeusTheTom
DeusTheTom

Reputation: 81

Is there a way to make something like a function template using C#?

I have this code here:

        private void brandAnalytics_Click(object sender, EventArgs e)
        {
            try
            {
                string fileTARGET = System.IO.Path.GetFullPath(openFileDialogTARGET.FileName);
                if (fileTARGET == null)
                {
                    MessageBox.Show("Please select a valid document.");
                }
                else if (driver == null)
                {
                    MessageBox.Show("Please start the Edge Driver.");
                }
                else
                {
                  doStuff();
                }
            }
            catch (Exception ex)
            {
                brandAnalytics.BackColor = Color.White;
                button3.Text = "Error occured - check message box";
                MessageBox.Show(ex.Message);
            }
        }

This is how I start pretty much every function, using try and catch. How do I save the repeatance, so that if I modify something, it changes in all functions? I can't think of anything, maybe someone has an idea? - is there such a thing as function templates?

Upvotes: 1

Views: 84

Answers (1)

Undiscouraged
Undiscouraged

Reputation: 1125

I would apply the Template Method pattern here:

https://refactoring.guru/design-patterns/template-method

Basically your class

class Test
{
    private void brandAnalytics_Click(object sender, EventArgs e)
    {
        try
        {
            string fileTARGET = System.IO.Path.GetFullPath(openFileDialogTARGET.FileName);
            if (fileTARGET == null)
            {
                MessageBox.Show("Please select a valid document.");
            }
            else if (driver == null)
            {
                MessageBox.Show("Please start the Edge Driver.");
            }
            else
            {
                doStuff();
            }
        }
        catch (Exception ex)
        {
            brandAnalytics.BackColor = Color.White;
            button3.Text = "Error occured - check message box";
            MessageBox.Show(ex.Message);
        }
    }
}

will be split into 2 classes.

An abstract class:

// defines the template
abstract class Template
{
    private void brandAnalytics_Click(object sender, EventArgs e)
    {        
        try
        {
            if (EnsurePreconditions())
            {
                DoStuff();
            }
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }

    private abstract bool EnsurePreconditions();
    private abstract void DoStuff();
    private abstract void HandleException(Exception ex);
}

And a concrete class:

// concrete test based on the template
class TestV2 : Template
{
    private override bool EnsurePreconditions()
    {
        string fileTARGET = System.IO.Path.GetFullPath(openFileDialogTARGET.FileName);
        if (fileTARGET == null)
        {
            MessageBox.Show("Please select a valid document.");
            return false;
        }
        else if (driver == null)
        {
            MessageBox.Show("Please start the Edge Driver.");
            return false;
        }

        return true;
    }

    private override void DoStuff()
    {
        // ommitted
    }

    private override void HandleException(Exception)
    {
        brandAnalytics.BackColor = Color.White;
        button3.Text = "Error occured - check message box";
        MessageBox.Show(ex.Message);
    }
}

and you can have as many concrete classes which are based on the template as you wish.

(I did not compile the code so there might be some syntax errors.)

Upvotes: 1

Related Questions