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
6
Upvotes
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