Home > Enterprise >  Is there a way to make this more clean looking?
Is there a way to make this more clean looking?

Time:09-30

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 appriciated.

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 frontend

CodePudding user response:

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",
    _ => ""
};

CodePudding user response:

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();
        }
  • Related