5
\$\begingroup\$

I would like some tips on how I could simplify my code. Basically what I am trying to do is to input 10 numbers in an array but this looks messy so I am trying to change it to look less messy.

Imports System

Public Module Module1

Public Sub Main()
    Dim score(10) as decimal
    Console.WriteLine("Enter the score")
    score(0) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(1) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(2) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(3) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(4) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(5) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(6) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(7) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(8) = Console.ReadLine
    Console.WriteLine("Enter the score")
    score(9) = Console.ReadLine
    Console.WriteLine(score(0) + score(1) + score(2) + score(3) + score(4) + score(5) + score(6) + score(7) + score(8) + score(9))

End Sub
\$\endgroup\$
4
\$\begingroup\$

Repetition. Always a sign that something could be improved. DRY (Don't repeat yourself) is the mantra here.

Magic Numbers. Passable for one-off code, but can get in the way if you wish to maintain or extend the code.

I will deal with the magic numbers first (and you'll see why when I address the repetition)

You have Dim score(10) as decimal where there is no real reason for 10 except that it is a nice number. We can fix this (may seem superfluous at first, but it is a good habit) by the following:

Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal

Now, if you wanted to work with 12 numbers you only have to change the assignment of numOfInputs. You have two areas of repetition. Addressing the main block:

For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
    Console.WriteLine("Enter the score (input number " & iterator & ")")
    score(iterator) = Console.ReadLine
Next iterator

See how I was also able to "personalise" the prompt string! Now if you want to do something else in here, you only have to fix the code once, and not ten times.

Addressing the output. There are a couple of ways to do this. One of those is to set up another loop, loop through the array and then use the output. But we already have the exact same loop that we would use to add them all up.

Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Dim finalCount as decimal = 0 'OK, the default value is 0 but I like to be explicit
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
    Console.WriteLine("Enter the score (input number " & iterator & ")")
    score(iterator) = Console.ReadLine
    finalCount += score(iterator) ' simply add up the items as we go along.
Next iterator

As you can see, I have now put in place finalCount so that we can add up as we go along - which is what I would have done if I had it in a separate loop anyway.

Oh, and now you can print it out through Console.WriteLine(finalCount).

Key lessons

  1. Loop rather than repeat. Much easier to maintain
  2. Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.
  3. Replace magic numbers with variables (constants) that are defined somewhere logical. Much easier to replace a well-named constant only once than trying to find that 10 and then working out if that needs to change to 12 or if it really is 10.
\$\endgroup\$
1
\$\begingroup\$

When you declare an array in VB.NET, the number you provide is the last index you can access.

So the line

Dim score(10) as decimal

means that score really has 11 numbers in it. It's one of the many differences between C# and VB.NET.

The wasted space isn't huge, but writing a loop to score.Length will give unexpected results when you get to the extra element you didn't know you had.

Here is some more information about arrays in VB.NET:

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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