0

How do I improve on this code? I can't seem to find any better looking code or more readable.

If anyone knows how to make this more clean please let me know.

Any tips on how to do it in the future are well appreciated.

public IActionResult Opdracht4_5(int score)
{
    var result = "";

    if (score == 100 && score >= 97)
    {
        result = "A+";
    } else if (score <= 96 && score >= 93)
    {
        result = "A";
    } else if (score <= 92 && score >= 90)
    {
        result = "A-";
    } else if (score <= 89 && score >= 87)
    {
        result = "B+";
    } else if (score <= 86 && score >= 83)
    {
        result = "B";
    } else if (score <= 82 && score >= 80)
    {
        result = "B-";
    } else if (score <= 79 && score >= 77)
    {
        result = "C+";
    } else if (score <= 76 && score >= 73)
    {
        result = "C";
    } else if (score <= 72 && score >= 70)
    {
        result = "C-";
    } else if (score <= 69 && score >= 67)
    {
        result = "D+";
    } else if (score <= 66 && score >= 63)
    {
        result = "D";
    } else if (score <= 62 && score >= 60)
    {
        result = "D-";
    } else if (score < 60)
    {
        result = "F";
    }
    
    ViewBag.Output = result;

    
    return View();
}

ViewBag.Output = result : Returns the result message to the front-end

3 Answers 3

6

Yes, you can use switch expressions for example.

var result = score switch
{
    100 and >= 97 => "A+",
    <= 96 and >= 93 => "A",
    <= 92 and >= 90 => "A-",
    <= 89 and >= 87 => "B+",
    <= 86 and >= 83 => "B",
    <= 82 and >= 80 => "B-",
    <= 79 and >= 77 => "C+",
    <= 76 and >= 73 => "C",
    <= 72 and >= 70 => "C-",
    <= 69 and >= 67 => "D+",
    <= 66 and >= 63 => "D",
    <= 62 and >= 60 => "D-",
    < 60 => "F",
    _ => ""
};
4
  • This code gives an error at the switch and result statement. But it looks very good
    – Qukz
    Commented Sep 30, 2022 at 12:59
  • It is a C# 8 feature, what dotnet version you are using?
    – IamK
    Commented Sep 30, 2022 at 13:02
  • I have to use the current version of dotnet I can't change the version.
    – Qukz
    Commented Sep 30, 2022 at 13:05
  • 1
    Thank you for bringing me on the right path.
    – Qukz
    Commented Sep 30, 2022 at 13:06
0

This has fixed my issue.

public IActionResult Opdracht4_5(int score)
        {
            
            var result = "";
            result = score is 100 and >= 97 ? "A+" :
                score is <= 96 and >= 93 ? "A" :
                score is <= 92 and >= 90 ? "A-" :
                score is <= 89 and >= 87 ? "B+" :
                score is <= 86 and >= 83 ? "B" :
                score is <= 82 and >= 80 ? "B-" :
                score is <= 79 and >= 77 ? "C+" :
                score is <= 76 and >= 73 ? "C" :
                score is <= 72 and >= 70 ? "C-" :
                score is <= 69 and >= 67 ? "D+" :
                score is <= 66 and >= 63 ? "D" :
                score is <= 62 and >= 60 ? "D-" :
                score < 60 ? "F" : result;

            ViewBag.Output = result;
            
            return View();
        }
0

This task can be accomplished much much more effective. Just check this code:

public class GradeCalculator
{
    private static readonly int[] _scores = new int[] { 59, 62, 66, 69, 72, 76, 79, 82, 86, 89, 92, 96 };
    private static readonly string[] _grades = new string[] { "F", "D-", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-", "A", "A+" };

    public string GetGrade(int score)
    {
        int pos = Array.BinarySearch(_scores, score);
        return _grades[pos < 0 ? ~pos : pos];
    }

    public IActionResult Opdracht4_5(int score)
    {
        ViewBag.Output = GetGrade(score);

        return View();
    }
}

The _scores array contains the maximum score to get specific grade. The _grades array contains grades and have one element more than _scores array. Its crucial for the _scores array to be sorted ascending, because of how binary search algorithm works. And finally, this approach only makes at most four comparisons to find the right grade.

Not the answer you're looking for? Browse other questions tagged or ask your own question.