r/visualbasic • u/Dennis13631 • Jan 12 '20
Need a bit help
So ive done a program and my teacher just casually said its spagheti code and i need to somehow fix it but im totally out of idea how to do that so i would appreciate all help i can get from here!
Here is the code:
https://pastebin.com/qzPbMXh8
2
u/Songg45 Jan 12 '20 edited Jan 12 '20
So I took a look at your code, and had made a few enhancements (note: I did not add LondonPilot's suggestions) :
Changed your if text = 1 or 2 or.... to a Regex function.
Made it into a Do While loop exiting by typing "exit"
The Regex function forces text to have a length of 1.
Changed your 25 if then index statements to a select case
Added a closing statement and error message
1
u/Dennis13631 Jan 12 '20
Can i just ask what is this? ("[0-9]|[bcdhnot]|[AEFGHLPUY]")
Never seen something like this whats it supposed to do?1
u/Songg45 Jan 12 '20
Thats my Regex (or REGular EXpression). It allows me to quickly determine patterns in text. I move text to the pattern search, and tell it to match anything thats 0-9, bcdhnot, or AEFGHLPUY. If you had the full alphabet, it would be ("[0-9][A-Z][a-z]")
If it gets a match, then the function returns true and the program continues. If it doesn't get a match, or text is longer than 1 character, the function returns false and fails.
1
u/Dennis13631 Jan 12 '20
so its not possible to print more than 1 character with this function as i understand? Anyway thats awesome but i cant use it like that i think
1
u/Songg45 Jan 12 '20
Like printing both 1 and Y for example?
With some work, it can, but your original program doesn't either.
1
u/Dennis13631 Jan 12 '20
On the same row yes . Thats the last thing i need to finish this program
1
u/Songg45 Jan 12 '20
Big oof if true
I have to leave so I will be back later... but I'm thinking not on the same line without a major rework.
1
u/Dennis13631 Jan 12 '20
Im sorry u wasted your time like that i should have explained it in the post not the comments :/ feels bad
1
u/Songg45 Jan 12 '20
No what I'm saying is I'm pretty certain what your trying to do is not possible using console.writeline... but I may be wrong.
Do you absolutely HAVE TO use console.writeline?
1
u/Dennis13631 Jan 12 '20 edited Jan 12 '20
Well it has to be similar to that ( its my teacher code) but i started mine before he uploaded his so i was told to somehow fix the spagheti code and upgrade it however i can
→ More replies (0)
1
u/Thunor_SixHammers Jan 12 '20
Could you give some context as to what this is supposed to do?
1
u/Dennis13631 Jan 12 '20
You put one of the available values and it should print them like a digital clock but with stars and i have to somehow fix this spagheti code and also make it so its able to print more than 1 on the same row
Here is how it looks right now when its started : https://imgur.com/a/FpaRR6R
1
u/fuzzfeatures Jan 16 '20 edited Jan 16 '20
Hi,
I thought I'd add my ideas to the mix.
Looking at the code my immediate thought there was a lot of work went into defining each letter, so I set about reducing the amount of work and the chance of making mistakes .. so i pre-defined the strings. This way, intellisense helps you out when it comes to defining each character. Also, if you wanted to change the asterisks to some other character, you only have to make a few changes with less chance of mistakes.
Also the arrays are structured slightly differently so you don't have to mess around with array subscripts .. this makes the code a little simpler and less prone to errors and easier to read. AND it removes the need for nested loops
Finally, your multiple If..Then statements can be reduced to a couple of lines.
All the replacement code does it to create a string of all the valid characters, then the first if statement checks that the user entered character is contained in the string of valid characters. Then, the next line just finds the character's position in that string and uses that position as the index in the array where the appropriate element is. :-)
Have a look and see if it makes sense
Option Explicit On
Module Module1
Sub Main(args As String())
Dim numbers(25)() As Integer
'numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
numbers(0) = {1, 0, 0, 0, 0, 0, 0}
numbers(1) = {1, 0, 0, 1, 1, 1, 1}
numbers(2) = {0, 1, 0, 0, 1, 0, 0}
numbers(3) = {0, 1, 1, 0, 0, 0, 0}
numbers(4) = {0, 0, 1, 1, 0, 0, 1}
numbers(5) = {0, 0, 1, 0, 0, 1, 0}
numbers(6) = {0, 0, 0, 0, 0, 1, 0}
numbers(7) = {1, 1, 1, 1, 0, 0, 0}
numbers(8) = {0, 0, 0, 0, 0, 0, 0}
numbers(9) = {0, 0, 1, 0, 0, 0, 0}
'Letters A, b, c, d, E, F, G, H, h, L, n, o, P, t, U, Y
numbers(10) = {0, 0, 0, 1, 0, 0, 0}
numbers(11) = {0, 0, 0, 0, 0, 1, 1}
numbers(12) = {0, 1, 0, 0, 1, 1, 1}
numbers(13) = {0, 1, 0, 0, 0, 0, 1}
numbers(14) = {0, 0, 0, 0, 1, 1, 0}
numbers(15) = {0, 0, 0, 1, 1, 1, 0}
numbers(16) = {1, 0, 0, 0, 0, 1, 0}
numbers(17) = {0, 0, 0, 1, 0, 0, 1}
numbers(18) = {0, 0, 0, 1, 0, 1, 1}
numbers(19) = {1, 0, 0, 0, 1, 1, 1}
numbers(20) = {0, 1, 0, 1, 0, 1, 1}
numbers(21) = {0, 1, 0, 0, 0, 1, 1}
numbers(22) = {0, 0, 0, 1, 1, 0, 0}
numbers(23) = {0, 0, 0, 0, 1, 1, 1}
numbers(24) = {1, 0, 0, 0, 0, 0, 1}
numbers(25) = {0, 0, 1, 0, 0, 0, 1}
Dim segH As String = "*******"
Dim segL As String = "* "
Dim segR As String = " *"
Dim segLR As String = "* *"
Dim segEmpty As String = " "
Dim letters(25)() As String
'numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
letters(0) = {segH, segLR, segLR, segLR, segLR, segLR, segLR, segLR, segH}
letters(1) = {segR, segR, segR, segR, segR, segR, segR, segR, segR}
letters(2) = {segH, segR, segR, segR, segH, segL, segL, segL, segH}
letters(3) = {segH, segR, segR, segR, segH, segR, segR, segR, segH}
letters(4) = {segLR, segLR, segLR, segLR, segH, segR, segR, segR, segR}
letters(5) = {segH, segL, segL, segL, segH, segR, segR, segR, segH}
letters(6) = {segH, segL, segL, segL, segH, segLR, segLR, segLR, segH}
letters(7) = {segH, segR, segR, segR, segR, segR, segR, segR, segR}
letters(8) = {segH, segLR, segLR, segLR, segH, segLR, segLR, segLR, segH}
letters(9) = {segH, segLR, segLR, segLR, segH, segR, segR, segR, segH}
'Letters A(10), b(11), c(12), d(13), E(14), F(15), G(16), H(17), h(18), L(19), n(20), o(21), P(22), t(23), U(24), Y(25)
letters(10) = {segH, segLR, segLR, segLR, segH, segLR, segLR, segLR, segLR}
letters(11) = {segL, segL, segL, segL, segH, segLR, segLR, segLR, segH}
letters(12) = {segEmpty, segEmpty, segEmpty, segEmpty, segH, segL, segL, segL, segH}
letters(13) = {segR, segR, segR, segR, segH, segLR, segLR, segLR, segH}
letters(14) = {segH, segL, segL, segL, segH, segL, segL, segL, segH}
letters(15) = {segH, segL, segL, segL, segH, segL, segL, segL, segL}
letters(16) = {segH, segL, segL, segL, segLR, segLR, segLR, segLR, segH}
letters(17) = {segLR, segLR, segLR, segLR, segH, segLR, segLR, segLR, segLR}
letters(18) = {segL, segL, segL, segL, segH, segLR, segLR, segLR, segLR}
letters(19) = {segL, segL, segL, segL, segL, segL, segL, segL, segH}
letters(20) = {segEmpty, segEmpty, segEmpty, segEmpty, segH, segLR, segLR, segLR, segLR}
letters(21) = {segEmpty, segEmpty, segEmpty, segEmpty, segH, segLR, segLR, segLR, segH}
letters(22) = {segH, segLR, segLR, segLR, segH, segL, segL, segL, segL}
letters(23) = {segL, segL, segL, segL, segH, segL, segL, segL, segH}
letters(24) = {segLR, segLR, segLR, segLR, segLR, segLR, segLR, segLR, segH}
letters(25) = {segLR, segLR, segLR, segLR, segH, segR, segR, segR, segH}
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("Values : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, b, c, d, E, F, G, H, h, L, n, o, P, t, U, Y")
Console.Write("Write a value: ")
Dim text As String = Console.ReadLine()
Console.ResetColor()
Dim validCharacters As String = "0123456789AbcdEFGHhLnoPtUY"
If validCharacters.Contains(text) Then
'Replacement for all the If..Then statements
Dim index As Integer = validCharacters.IndexOf(text)
'Letters A(10), b(11), c(12), d(13), E(14), F(15), G(16), H(17), h(18), L(19), n(20), o(21), P(22), t(23), U(24), Y(25)
For i As Integer = 0 To numbers(index).GetUpperBound(0)
Console.Write(" ")
Console.Write(numbers(index)(i))
Next
Console.WriteLine()
For j As Integer = 0 To letters(index).GetUpperBound(0)
Console.WriteLine(letters(index)(j))
Next
Else
Console.WriteLine("wrong value!")
End If
Console.ReadKey()
End Sub
End Module1
u/fuzzfeatures Jan 16 '20 edited Jan 16 '20
Hi again,
Have a look at my previous post first .. this explains some simplifications to your original code. The code below adds the ability to type in several characters and display them.
Rather than include the whole code here, I've pasted everything after the user input prompt
Dim validCharacters As String = "0123456789AbcdEFGHhLnoPtUY"
'Check that the entered text characters are all valid
Dim allCharactersValid As Boolean = True
For Each character As String In text
If Not validCharacters.Contains(character) Then
allCharactersValid = False
Exit For
End If
Next
Dim index As Integer
If allCharactersValid Then
For Each character As String In text
index = validCharacters.IndexOf(character)
For i As Integer = 0 To numbers(index).GetUpperBound(0)
Console.Write(numbers(index)(i))
Next
Console.Write(" ")
Next
Console.WriteLine()
For j As Integer = 0 To letters(index).GetUpperBound(0)
For Each character As String In text
Console.Write(letters(validCharacters.IndexOf(character))(j) & " ")
Next
Console.WriteLine()
Next
Else
Console.WriteLine("wrong value!")
End If
Console.ReadKey()
End Sub
3
u/LondonPilot Jan 12 '20 edited Jan 12 '20
It’s not really “spaghetti code” as such - that would suggest the flow of the program is hard to follow.
The flow of this program is simple - but it’s hard to read.
So:
Move the “numbers” and “letters” variables into a different module (or class, if you know about classes yet)
For the “letters” variable, put each string onto a different line, and indent them so that it’s immediately clear that they’re showing the letters (I think Visual Studio will let you do this, I can’t remember and I’m on my phone so I can’t test it)
Take that massive list of if statements, and at the very least change it to a select/case. Even better, if you can figure out how to turn this pseudo-code into code:
If between 0-9, Subtract ASCII code for 0
Else if between A-Z, Subtract ASCII code for A then Add 10