As a programmer I faced the following problem many times and never was able to solve it, suppose I have:
bool is_succeeded;
while ((is_succeeded=my_fun()))
{
if (!is_succeeded)
{
//really long code
}
}
This will bypass the initial check (if my_func failed at first time and won't call the //really long code
part).
A possible solution would be to add another condition before or after the while loop for the initial check, but that will cause a lot of duplicated code, how can I solve this problem?
For example I don't want the following code:
bool is_succeeded=my_fun();
if (!is_succeeded)
{
//really long code
}
while (is_succeeded)
{
is_succeeded=my_fun();
if (!is_succeeded)
{
//really long code
}
}
Why?
Because I am writing //really long code
part twice which is a duplication of code.
maybe do while
can help here ?
Please Note: I know I can move //really long code
to a function and call it but for my own reasons (hard to explain) I can't always do that.
Aucun commentaire:
Enregistrer un commentaire