skip to Main Content

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


  1. Chosen as BEST ANSWER

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

  2. 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",
        _ => ""
    };
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search