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.
background concept
A program consists of a combination of simple (eg assignment) or compound (eg conditional, loop) statements .
The latter statements can be nested , which means they can be composed of blocks of code that can contain other statements .
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
…
The post Six Rules for Writing Clean Code first appeared on Lenix Blog .
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.