Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid while-loop code duplication?

string s = Console.ReadLine();
while(s != null)
{
    // do something 
    //   ....
    s = Console.ReadLine(); 
}

The code above is to get the input, verify it, process it and then input again, but obviously, s = Console.ReadLine(); is code duplication.

What tricks are there to avoid the duplication?

like image 686
user3129535 Avatar asked Sep 16 '26 14:09

user3129535


1 Answers

depending on language, you can often do something like this:

while (s = Console.ReadLine())
{
    ...
}
like image 187
acushner Avatar answered Sep 21 '26 00:09

acushner