3

Pretty annoying when a programming language's constructs do things that are unintuitive, for example, Java substring:

str.substring(startIndex, endIndex); // oops, character at endIndex not included.

Or some other Python data science Google library (I used this at work) that decided their way is the best way, where they have a simple method but in the facade behind it is a nightmare monstrosity of complexity. How fun to debug or customize that!

Weird design decisions. They don't help because it makes them harder to immediately understand them.

Comments
  • 5
    To be fair, the end index is always exclusive, in any language.
  • 3
    @Lensflare I aim to find out why.
  • 3
    It's maybe more clear if you read it as length. "ABC"[0:1]>"a". Second parameter is just length and it starts from zero so. But also making the mistake still often. Can happen.
  • 2
    @whimsical that actually makes sense.
    length = endIndex - startIndex
  • 5
    Top-exclusive ranges are the standard because they make more sense than bottom-exclusive ranges, and because making exactly one side of the range inclusive makes all sorts of math a lot easier. If both ends were included, an empty slice would have to be either unrepresentable or it would be encoded as end + 1 = start where neither end nor start are actually included.
  • 0
    @lorentz I knew it would be math. lol. It reminded me of (,] and [,) notations.
  • 0
    @jestdotty or simply do rand() % numberOfThings
    ๐Ÿคจ
  • 0
    @jestdotty Yes indeed.. learning by practice and experience always works well.
  • 0
    @jestdotty no, what you said was
    (int)(randomFloat() * numberOfThings)

    where randomFloat() gives you a number in the range of 0.0 to 1.0
  • 1
    Now thinking about it, I wonder though if there's a broad category of problems where it's useful to represent ranges with A..B containing X where (B < X) == (X < A), which is to say, double-inclusive if A<B and double-exclusive if B<A.

    On an additional tangent from my tangent, I wonder if it's feasible to conduct niche elimination via algebraic invariants this way, i.e. we know that a standard range has low<high, so you can tag a union of a range and anything that fits into two fewer bits than it by setting low.msb=1 and high.msb=0 and then populating the rest of the range with the alternatve value.
  • 0
    @lorentz I‘m not gonna pretend that I understand (I never do), but I like your thoughts.
Add Comment