title: Need help reducing cyclomatic complexity
body: <p>I've got a method called <code>UpdateUserDevices (UserModel)</code>. The <code>UserModel</code> contains a <code>List&lt;DeviceModel&gt;</code> which associates a list of devices with a specific user. (One-to-many).</p>
  
  <p>When I call the method, everything is working as excpected, however it's quite complex with nested loops and if statements.</p>
  
  <p>What would be a good pattern to reduce the cyclomatic complexity on this? I thought about "CoR" but that might be overkill.</p>
  
  <pre><code>private void UpdateUserDevices( UserModel model )
  {
      // Get users current devices from database
      var currentDevicesFromDatabase = _deviceRepository.FindByUserId( model.Id ).ToList();
  
      // if both the model devices and the datbase devices have records
      // compare them and run creates, deletes, and updates
      if( model.Devices.Any() &amp;&amp; currentDevicesFromDatabase.Any() )
      {
          var devicesToAdd = model.Devices.Exclude( currentDevicesFromDatabase, d =&gt; d.Id ).ToList();
          var devicesToDelete = currentDevicesFromDatabase.Exclude( model.Devices, d =&gt; d.Id ).ToList();
          var workingDevices = model.Devices.Union( currentDevicesFromDatabase );
  
          foreach( var device in workingDevices )
          {
              // Add devices
              if( devicesToAdd.Contains( device ) )
              {
                  _deviceRepository.Create( device );
                  continue;
              }
  
              // delete devices
              if( devicesToDelete.Contains( device ) )
              {
                  _deviceRepository.Delete( device );
                  continue;
              }
  
              // update the rest
              _deviceRepository.Update( device );
          }
          return;
      }
  
      // model.Devices doesn't have any records in it.
      // delete all records from the database
      if( !model.Devices.Any() )
      {
          foreach( var device in currentDevicesFromDatabase )
          {
              _deviceRepository.Delete( device );
          }
      }
  
      // database doesn't have any records in it
      // create all new records
      if( !currentDevicesFromDatabase.Any() )
      {
          foreach( var device in currentDevicesFromDatabase )
          {
              _deviceRepository.Create( device );
          }
      }
  }
  </code></pre>