Home > Blockchain >  Refactoring if statements for a telegram bot
Refactoring if statements for a telegram bot

Time:10-22

I have this method DetermineInline() which sends us an inline keyboard, I want to add some more functionality to it so it would check for different properties and send an inline keyboard with group numbers, I have a queryhandler object that stores the data, how would I refactor it in a way so that I wouldn't have to use if statements. Example code bellow.

private InlineKeyboardMarkup DetermineInline()
    {
        if (_queryHandler.Course == "1" && _queryHandler.Specialization == "Marketing")
        {
            InlineKeyboardMarkup inlineKeyboardMarkup = new(
            new[]
            {
                new[]
                {
                InlineKeyboardButton.WithCallbackData("001","001"),
                InlineKeyboardButton.WithCallbackData("002","002"),
                InlineKeyboardButton.WithCallbackData("003","003")
                },
            });
            return inlineKeyboardMarkup;
        }
        if(_queryHandler.Course == "1" && _queryHandler.Specialization == "IRM")
        {
            InlineKeyboardMarkup inlineKeyboardMarkup = new(
            new[]
            {
                new[]
                {
                InlineKeyboardButton.WithCallbackData("004","004"),
                InlineKeyboardButton.WithCallbackData("005","005"),
                InlineKeyboardButton.WithCallbackData("006","006")
                },
            });
            return inlineKeyboardMarkup;
        }
        if(_queryHandler.Course == "1" && _queryHandler.Specialization == "Logistics")
        {
            InlineKeyboardMarkup inlineKeyboardMarkup = new(
            new[]
            {
                new[]
                {
                InlineKeyboardButton.WithCallbackData("007","007"),
                InlineKeyboardButton.WithCallbackData("008","008"),
                InlineKeyboardButton.WithCallbackData("009","009")
                },
            });
            return inlineKeyboardMarkup;
        }
        return null;
    }

CodePudding user response:

You can use Dictionary collection where the keys will be the specilizations such as "Marketing" and the values will be the "001", "002" and so on.

This way you can minimize the if statements. However we still need one if statement for the _queryHandler.Course query as it is not same as the other specilization queries.

private InlineKeyboardMarkup DetermineInline()
{
    var dict = new Dictionary<string, (string, string, string)>();
    dict.Add("Marketing", ("001","002","003"));
    dict.Add("IRM", ("004","005","006"));
    dict.Add("Logistics", ("007","008","009"));
    
    var q = _queryHandler.Specialization;
            
    if (_queryHandler.Course == "1")
    {
        InlineKeyboardMarkup inlineKeyboardMarkup = new(
        new[]
        {
            new[]
            {
            InlineKeyboardButton.WithCallbackData(dict[q].Item1,dict[q].Item1),
            InlineKeyboardButton.WithCallbackData(dict[q].Item2,dict[q].Item2),
            InlineKeyboardButton.WithCallbackData(dict[q].Item3,dict[q].Item3),
            },
        });
        return inlineKeyboardMarkup;
    }
    return null;
}

Dictionary Output:

Console.WriteLine(dict["Marketing"].Item1); // 001
Console.WriteLine(dict["IRM"].Item3);       // 006
Console.WriteLine(dict["Logistics"].Item2); // 008
  • Related