Dividing Negative integer ranges Context:

My project has a hand-rolled ORM (... yeah ... I know) ...

built around the MS DataSet objects that represent wrapped collections of rows of DBtables (... yeah ... I know) ...

and which use negative ints to represent identity columns on new Rows which haven't yet been synched with the DB (... yeah ... I know) ...

the upshot of which is we objects that generally expect an identity to start at -1 and be decremented until they reach int.MinValue

Furthermore, we sometimes need to tell these objects that they can only use a certain sub-set of the negative integer range, so that multiple threads can create rows, which can then be combined without risk of ID conflicts. (... yeah ... I know) ...

and occasionally this subdivision has to happen repeatedly :(

All of this is to explain why it is that I need a method that is going to take a range of negative numbers, and sub-divide it. But that these ranges want to be thought of as running from, say, -100 to -999 (i.e. they run the 'wrong way').

Alas, all of this set-up is completely non-negotiable. so the plethora of comments along the lines of "the best way to fix this code is to not need it, or to change what it's trying to do" are (whilst completely correct) not at all useful.

We need to split an arbitrary negative range into n sub-ranges, all of which MUST be strictly dis-joint. It's NOT necessary that we use all the integers (though it's preferable not to waste too many).
It's not necessary that we always use the boundary values, though again preferable.

We're guaranteed to be given the range (-1, int.MinValue), so have to support that.

Primary goal of review is primarily to spot bugs and edge cases, and gather views on readibility/understandability of code.
Other aesthetic/design comments welcome too.
Unit Test suite will be written, given the ease of testing.

So, the code:

public static void IdentifyIndexRangeForDataSetUse(
  int xiExistingRangeStart,
  int xiExistingRangeEnd,
  int xiTotalNumberOfDivisionsBeingMade,
  int xiIndexOfTargetDivision,
  out int xoTargetRangeStart,
  out int xoTargetRangeEnd)
{
  if (xiExistingRangeStart &lt; xiExistingRangeEnd)
  { throw new System.InvalidOperationException("The SMALLER value MUST be provided first. Note that since values are negative, this first value will have the larger ABSOLUTE value!"); }

  if (xiExistingRangeStart &gt; 0 || xiExistingRangeEnd &gt; 0)
  { throw new System.InvalidOperationException("The existing range should lie entirely within the Negative Integers."); }

  // REVERSE THE NUMBERS!!
  // We get given a range like (-1 --&gt; -99), but it's WAY easier to think and reason about the range (1 --&gt; 99)
  // so simply reflect the range into the positive domain and work there.
  int lPosExistingRangeStart, lPosExistingRangeEnd;

  lPosExistingRangeStart = -1 * xiExistingRangeStart;
  if (xiExistingRangeEnd == int.MinValue)
  {
    lPosExistingRangeEnd = int.MaxValue;
  }
  else
  {
    lPosExistingRangeEnd = -1*xiExistingRangeEnd;
  }


  var lTotalLengthOfExistingRange = lPosExistingRangeEnd - lPosExistingRangeStart + 1; //check overflows
  var lTotalLengthOfNewRanges = lTotalLengthOfExistingRange / xiTotalNumberOfDivisionsBeingMade;
  int lPosTargetRangeStart = lPosExistingRangeStart + (xiIndexOfTargetDivision * lTotalLengthOfNewRanges);
  int lPosTargetRangeEnd = lPosTargetRangeStart + (lTotalLengthOfNewRanges - 1); //the '-1' prevents overlaps.


  // REVERSE THE NUMBERS!!
  // To take it back into the negative domain.
  xoTargetRangeStart = -1 * lPosTargetRangeStart;
  xoTargetRangeEnd = -1 * lPosTargetRangeEnd;

}
Redundant check

If xiExistingRangeStart is greater than xiExistingRangeEnd, then you only have to verify that xiExistingRangeStart is not greater than zero to know that xiExistingRangeEnd can't be.

Parameters + Error messages

I would prefer your exception messages to be more explicit.   Rather than:


  "The SMALLER value MUST be provided first. Note that since values are negative, this first value will have the larger ABSOLUTE value!"); }


I think it would be better to refer to 'Start' rather than 'first' value.  This may seem pedantic but consistency between the error name and the parameter name make it easier to resolve issues.

Similarly, when I think of negative numbers, I tend to think of numbers &lt; 0, not &lt;= zero which seems to be what your code suggests.  So again, I'd tend to be more explicit.