0

I'm trying to do a connection between a local dataset and a remote dataset . When I run my program, the method below is called.

 public void CheckCanDo()
 {
    if (LocalAcess.HasNonSyncedItems())
     {
       if (RemoteAcess.TryToSync())
       {
          LocalAcess.RefreshDateLastConnection();
          CanDoThing = true;
       } 
       else
       {
           if(LocalAcess.HasExpired)
           {
               CanDoThing = false;
               return;
           }
       }                
     }
     CanDoThing = true;
 }

Is it appropriate to have a method executing actions and returning a boolean as a way to control the application's flow? Does it violate some good practice of C#? Is there a better way?

Vinicius Seganfredo
  • 1,704
  • 3
  • 15
  • 21

3 Answers3

0

Replace "void" with "bool" before "CheckCanDo()" and instead of setting variables etc, to return a result; just type return a bool, e.g. "return condition;". That's really a better way to do it.

0
public bool CheckCanDo()
{
    if (LocalAcess.HasNonSyncedItems())
    {
        if (RemoteAcess.TryToSync())
        {
            LocalAcess.RefreshDateLastConnection();
            return  true;
        }
        else
        {
            if (LocalAcess.HasExpired)
            {
                return false;
            }
        }
    }
    return true;
}
Sajid
  • 13
  • 1
  • 5
0

You could try this ... I will advice you to keep business logic strait and to remove to much if- else it could convert business logic into a mess!!!

public bool CheckCanDo()
{
        if (!LocalAcess.HasNonSyncedItems())
        {
            return false;
        }

        // It might be !RemoteAcess.TryToSync() || LocalAcess.HasExpired
        // I did not really understand that part
        if (!RemoteAcess.TryToSync() && LocalAcess.HasExpired)
        {
            return false;            
        }

        LocalAcess.RefreshDateLastConnection();
        return true;             
}

This blog might help you on this more... How to avoid "if" chains?

Community
  • 1
  • 1
Moumit
  • 8,314
  • 9
  • 55
  • 59