Niraj Zade   Home  Blog  Notes  Tools 

Quick tip- Clearly communicate ranges in your functions' documentation using mathematical notations

Date: 2023/11/18

The notation

The mathematicians have a very clear way of telling ranges.

wikipedia.org/wiki/Interval_(mathematics)

The notation is simple -

  • Square brackets are inclusive
    • [a,b] means all x where a<=x<=b. {x∈R:a≤x≤b}
  • Round brackets are non-inclusive
    • (a,b) means all x where a<x<b. {x∈R:a<x<b}

You can also mix and match ranges.

EXAMPLES

  • Numbers between 0 and 3, including both 0 and 3
    • Notation: [0,3]
    • Means - {x∈R:0<=x<=3}
    • Values are : {0,1,2,3}
  • Numbers between 0 and 3, including 0 and excluding 3
    • Notation: [0,3)
    • Means - {x∈R:0<=x<3}
    • Values are : {0,1,2}
  • Numbers between 0 and 3, excluding 0 and including 3
    • Notation: (0,3]
    • Means - {x∈R:0<x<=3}
    • Values are : {1,2,3}
  • Numbers between 0 and 3, excluding both 0 and 3
    • Notation: (0,3)
    • Means - {x∈R:0<x<3}
    • Values are : {1,2}

Using notation in functions' documentation

When you create range functions in your libraries - in the documentation tell the range notation of your function's arguments.

Examples using python functions

The python range function has the syntax - range(start, stop, step)

We can document the range notation as:

  • [start, stop)
  • Range is : start <= val < stop

The python list slicing syntax is - list[start:stop:step]

We can document the range notation as:

  • [start,stop)
  • Range is : start <= val < stop

Bonus: You'll quickly notice that most standard libraries of most languages follow the [start,stop) convention.

Range notation in error messages

This is an error you get when you try to build spark with an older version of Java.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.4.1:enforce (enforce-versions) on project spark-parent_2.13: 
[ERROR] Rule 1: org.apache.maven.enforcer.rules.version.RequireJavaVersion failed with message:

[ERROR] Detected JDK version 11.0.22 (JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64) is not in the allowed range [17,).

Look at how it tells the range of acceptable JDK

[ERROR] Detected JDK version 11.0.22 (JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64) is not in the allowed range [17,).

17,). This means it needs a JDK version >= 17

This is such a concise and accurate way of telling the range of JDK needed.