Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:04:43+00:00 2026-06-13T14:04:43+00:00

I came across some code that looked like this in a code review the

  • 0

I came across some code that looked like this in a code review the other day.

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 ends up with an ever higher cyclomatic complexity and it also just doesn’t feel right to me.

A solution that I have come up with to resolve this is.

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

Used like this

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

  }

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

Anyone got any suggestions to make the code more readable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:04:44+00:00Added an answer on June 13, 2026 at 2:04 pm

    I made a little refactoring of your implementation

    private void DoTasksWhile(Func<bool> predicate, IEnumerable<Action> tasks)
    {
        foreach (var task in tasks)
        {
            if (!predicate())
                return;
            task();
        }
    }
    
    • you don’t need to Invoke delegates. Just execute them
    • do not compare boolean values with true/false (it’s useless and you can assign boolean value by mistake)
    • thus you only enumerating tasks, IEnumerable is good for parameter

    Also you can create extension method

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

    Usage will be very simple:

    tasks.DoWhile(() => CheckSomeState());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across some C++ code that looked like this: class SomeObject {
A colleague came across some code that looked like this and couldn't understand how
So I came across some code this morning that looked like this: try {
I came across some legacy code that contains a function like this: LPCTSTR returnString()
The other day, I came across this construct: static_cast<size_type>(-1) in some example C++ code,
I recently came across some code that looks something like this: <head> <?php /*
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...
I came across some code written in C that looks like this: if (file
Context I came across some code, like this: if( Some_Condition ) throw 0; I
Some background: I came across something the other day that got me thinking about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.