Hacker News new | ask | show | jobs
by revskill 1089 days ago
Algorithm to read scope:

- Step 1: Find open bracket

- Step 2: Find closed bracket

With indentation, the algorithm now becomes:

- Step 1: Find something which seems to open a scope.

- Step 2: Scan all indentation level until it's done.

The difference is huge here.

3 comments

Algin your method to the top of your screen. Now scroll one more line down. According to your algorithm, it's now impossible to know which line belongs to which scope.

I have a different algorithm:

- Find first indented line

- Find next unindented line

If that does not work for you, your code is bad.

finding open and closed brackets is a massive chore in mis-copied code, so indeed having scope open by spaces makes things very clear and non-ambiguous. Using whitespace for readability and not using it for scope control is one of the many bugs in the design of programming languages like Java. In my very humble opinion.
Step 1: Find an indent increase.

Step 2: Find the first place where the indent becomes lower.

It's not hard, human vision is naturally designed for this. What is hard is finding a matching bracket when the code is not indented properly.