Question

string noOfAccountsString = Console.ReadLine();
int noOfAccounts = int.Parse(noOfAccountsString);

Would you say this is bad practice? When you could just store one of these variables and perform the relevant operation on it when necessary?

So instead of the above would this be better?

int noOfAccounts = int.Parse(Console.ReadLine());
Was it helpful?

Solution

Not necessarily bad practice, just a matter of preference. The bad practice is assuming that the user will enter a numerical value.

OTHER TIPS

If you are certain that the parsing will occur then you can perform it right away and I wouldn't consider it bad practice. Also, I wouldn't worry about the application's speed at parsing the number unless you are doing this a very large number of times. I would use a TryParse prior to parsing, especially if it is being received from the user.

Your code would be more readable if you performed this on two lines instead of one though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top