Java Child / State And Change

A Value That Changes Is Where Confusion Begins

Most genuine bewilderment in programming comes from something that meant one thing earlier and means another now.

In ordinary writing, a name stands for a thing and keeps standing for it. In a program, a name can stand for one thing at the start and a different thing three steps later, and the instructions look identical in both moments. This is state: the collection of values that describe how things currently stand. State is what makes programs powerful, because it lets them accumulate, remember and respond. It is also what makes them confusing, because reading the instructions no longer tells you the whole story. You must also know when you are reading them.

The practical consequence is that a step cannot be understood in isolation. Asking what this step does is not quite a well formed question. The answerable question is what this step does given that these things are currently true. Beginners often stare at a single step, convinced the fault is inside it, when the fault was established twenty steps earlier by something that set a value they never thought about again. The step is doing exactly what it should, with the wrong material.

The strategy that helps most is to reduce the amount of changing you rely on. The fewer things that can be different at a given moment, the fewer combinations you need to hold in mind. Prefer working out a fresh answer over quietly altering an old one. Keep the reach of any changing value as narrow as you can manage. None of this is about elegance. It is about lowering the number of things that can surprise you at three o'clock in the afternoon.