Coding mode C #

A few days ago I encountered some code that looked like this during the code review.

public void DoSomeTasks( )
{
if (CheckSomeState()==true) return;
DoTaskOne();
if (CheckSomeState()==true) return;
DoTaskTwo( );
if (CheckSomeState()==true) return;
DoTaskThree();
if (CheckSomeState()==true) return;
DoTaskFour();
)

As the number of tasks increases, the code will eventually have higher cyclomatic complexity, and it feels wrong to me.

I proposed to solve this problem. The solution is.

private void DoTasksWhile(Func condition, Action[] tasks)
{
foreach (var task in tasks)< br /> {
if (condition.Invoke()==false) break;
task.Invoke();
}
}

Like this Use

public void DoSomeTasks()
{
var tasks = new Action[] {
{()=DoTaskOne()},
{()=DoTaskTwo()},
{()=DoTaskThree()},
{()=DoTaskFour()}

}

DoTasksWhile(()=>CheckSomeState(), tasks);
}

Does anyone have any suggestions to make the code more readable?

I refactored your implementation

private void DoTasksWhile(Func predicate, IEnumerable tasks)
{
foreach (var task in tasks)
{
if ( !predicate())
return;
task();
}
}

>You don’t need to call delegates. Just execute them
> Don’t compare the boolean value with true/false (it’s useless, you can specify the boolean value incorrectly)
>So you only enumerate tasks, IEnumerable applies to parameters

You can also create Extension method

public static void DoWhile(this IEnumerable actions,Func predicate)
{
foreach (var action in actions)
{
if (!predicate())
return;
actions();
}
}

The usage is very simple :

tasks.DoWhile(() => CheckSomeState());

I was reviewing the code a few days ago I encountered some code that looks like this.

public void DoSomeTasks()
{
if (CheckSomeState()==true ) return;
DoTaskOne();
if (CheckSomeState()==true) return;
DoTaskTwo();
if (CheckSomeState()==true) return;
DoTaskThree();
if (CheckSomeState()==true) return;
DoTaskFour();
}

As the number of tasks increases, the code will eventually have higher cyclomatic complexity, and it feels wrong to me.

I propose to solve this The solution to the problem is.

private void DoTasksWhile(Func condition, Action[] tasks)
{
foreach (var task in tasks )
{
if (condition.Invoke()==false) break;
task.Invoke();
}
}

Use like this

public void DoSomeTasks()
{
var tasks = new Action[] {
{()=DoTaskOne() },
{()=DoTaskTwo()},
{()=DoTaskThree()},
{()=DoTaskFour()}

}< br />
DoTasksWhile(()=>CheckSomeState(), tasks);
}

Does anyone have any suggestions to make the code more readable?

I have refactored your implementation

private void DoTasksWhile(Func  predicate, IEnumerable tasks)
{
foreach (var task in tasks)
{
if (!predicate())
return;< br /> task();
}
}

>You don’t need to call delegates. Just execute them
>Don’t compare booleans to true/false ( It’s useless, you can specify a boolean value incorrectly)
>So you only enumerate tasks, IEnumerable is suitable for parameters

You can also create extension methods

public static void DoWhile(this IEnumerable actions,Func predicate)
{
foreach (var action in actions)
{
if (! predicate())
return;
actions();
}
}

The usage is very simple:

tasks.DoWhile(() => CheckSomeState());

Leave a Comment

Your email address will not be published.