What is the modulus operator (%)?

. 1 min read

Most beginner coding tutorials start with basic arithmetic operations. I think it's a good idea to play with addition and multiplication in the very first coding lesson.

We can do better. I think we could make one change to every beginner tutorial, effective immediately. Let's cut the modulus operator from the first lesson. It slows down the tutorial and makes programming seem disconnected from the real world.

When I learned to code for the first time, I was a corporate finance banker. When you're working on billion dollar transactions, "remainders" after integer division are meaningless. The modulus operator seemed strange to me. Why did such a trivial operation earn a one character name? If I had my way, I'd demote modulus to a non-standard math library.

Now I'm learning to code again, as a computer scientist/mathematician. I'm learning number theory. I am laboriously solving modulus puzzles with the Euclidean algorithm. Okay, I get it now. The modulus operator is very important for both computer science and discrete mathematics.

When we teach anyone to code, we have to focus on the student's background and what the student wants to achieve. If the student wants to build a web application, let's teach them variables, functions, and scope first. Basic JavaScript websites don't need modular arithmetic.

When we write and deliver tutorials, we have to acknowledge our biases. You might have a computer science background, but perhaps your student doesn't. The tutorial has to adapt for the student, not the tutor.