HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Dividing Negative integer ranges

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rangesintegernegativedividing

Problem

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 xiTotalNumbe

Solution

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 < 0, not <= zero which seems to be what your code suggests. So again, I'd tend to be more explicit.

Context

StackExchange Code Review Q#142685, answer score: 2

Revisions (0)

No revisions yet.