Let’s look at some suggestions on how to prevent the spaghetti code phenomenon by minimizing the visibility of variables .
Our goal is to minimize the part of the code where the variable is visible in the source code, i.e. reduce the scope of the variable .
Scope minimization is a process of structuring code, it’s easy:
- declares the variable with the smallest scope, and
- Assign the variable the data with the smallest scope.
In fact, it is the code structure that defines the visibility of variables.
In particular, let’s consider two blocks A and B :
- If A contains B , we say that A is an outer block of B , while
- if B is contained in A
This article is reprinted from https://blog.p2hp.com/archives/9732
This site is for inclusion only, and the copyright belongs to the original author.