HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Take input of delimited file of integers and output result

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
resultfiledelimitedtakeoutputinputandintegers

Problem

I wanted to learn some new language so I developed a really simple vb.net "console application" (was that the right type?) that takes text input, looks at it and multiplies it if it's even and then reads each result into a msgbox.

  • test.txt = 1 2 3 4 5 6 7 8 9



  • result should be an array {1 4 3 8 5 12 7 16 9}



This would be my first vb.net programming attempt

Module MyFirstModule
    Sub Main()
        HelloNumbers()
    End Sub

    Private Sub HelloNumbers()
        Dim inputFile As String
        inputFile = "c:\temp\test.txt"
        Dim i As Integer
        Dim readResult As String() = Split(System.IO.File.ReadAllText(inputFile))
        Dim multipliedResult As Integer()
        Dim lower As Integer = LBound(readResult)
        Dim upper As Integer = UBound(readResult)
        ReDim multipliedResult(0 To upper)
        For i = lower To upper
            multipliedResult(i) = MultiplyEvenNumbers(CInt(readResult(i)))
        Next

        For i = lower To upper
            If multipliedResult(i) Mod 2 = 0 Then
                MsgBox(readResult(i) & " was multiplied by two for " & multipliedResult(i))
            Else : MsgBox(readResult(i) & " is original")
            End If
        Next
    End Sub

    Private Function MultiplyEvenNumbers(ByVal inputNumber As Integer) As Integer
        If inputNumber Mod 2 = 0 Then inputNumber = inputNumber * 2
        MultiplyEvenNumbers = inputNumber
    End Function
End Module

Solution

This is a good start. You created two functions to separate parts of the logic but there is more that can be refactored. The HalloNumbers sub is too big and does too much. Let's try to improve.

First the path shouldn't be hardcoded inside it so we pass it as parameter:

Sub Main()
    HelloNumbers("c:\temp\test.txt")
End Sub


Now we take parts of the big HalloNumbers and move them into fuctions that do only one thing - we always strive for this because it's easier to maintain when you only have to care about one thing at a time (see SRP).

Private Sub HelloNumbers(ByVal inputFile As String)
    Dim numbers As Integer() = ReadNumbers(inputFile)
    Dim multipliedNumbers = numbers.Select(Function(number) MultiplyEvenNumbers(number))
    PrintNumbers(multipliedNumbers)
End Sub


So we can extract the following functions:

We need to read the numbers. They are in a file. So let's require a path and after reading them we convert each number-string into an Integer with linq:

Private Function ReadNumbers(ByVal inputFile As String) As IEnumerable(Of Integer)
    ReadNumbers = Split(File.ReadAllText(inputFile)).Select(Function(s, i) CInt(s))
End Function


Then we need to multiply them. This one was already there so we leave it as is.

Private Function MultiplyEvenNumbers(ByVal inputNumber As Integer) As Integer
    If inputNumber Mod 2 = 0 Then inputNumber = inputNumber * 2
    MultiplyEvenNumbers = inputNumber
End Function


Finaly you want to print the results. Let's do it in PrintNumbers. If you decide to write the output to the console you now just have to adjust this small function.

Private Sub PrintNumbers(ByVal numbers As IEnumerable(Of Integer))
    For Each number As Integer In numbers
        If number Mod 2 = 0 Then
            MsgBox(String.Format("{0} was multiplied by two for {1}", number, number / 2))
        Else : MsgBox(String.Format("{0} is original", number))
        End If
    Next
End Sub

Code Snippets

Sub Main()
    HelloNumbers("c:\temp\test.txt")
End Sub
Private Sub HelloNumbers(ByVal inputFile As String)
    Dim numbers As Integer() = ReadNumbers(inputFile)
    Dim multipliedNumbers = numbers.Select(Function(number) MultiplyEvenNumbers(number))
    PrintNumbers(multipliedNumbers)
End Sub
Private Function ReadNumbers(ByVal inputFile As String) As IEnumerable(Of Integer)
    ReadNumbers = Split(File.ReadAllText(inputFile)).Select(Function(s, i) CInt(s))
End Function
Private Function MultiplyEvenNumbers(ByVal inputNumber As Integer) As Integer
    If inputNumber Mod 2 = 0 Then inputNumber = inputNumber * 2
    MultiplyEvenNumbers = inputNumber
End Function
Private Sub PrintNumbers(ByVal numbers As IEnumerable(Of Integer))
    For Each number As Integer In numbers
        If number Mod 2 = 0 Then
            MsgBox(String.Format("{0} was multiplied by two for {1}", number, number / 2))
        Else : MsgBox(String.Format("{0} is original", number))
        End If
    Next
End Sub

Context

StackExchange Code Review Q#141363, answer score: 3

Revisions (0)

No revisions yet.