Variable privatization and function privatization, and the benefits of privatization

variable privatization

Why are variables private?

In a well-structured program, data is exchanged between separate modules through function calls that pass parameters between modules. In most cases, especially when you're just getting into programming habits, it's a good idea to make sure that each global variable isn't referenced by more than one module. To avoid the possibility of two modules referencing the same global variable, the variable can be made private.

How are variables private?

Variable privatization is to use the keyword static before the declaration, such as:

static int cpos;

This declaration defines cpos as a global integer variable, visible everywhere in the defined module, but invalid for other modules, so it is private to the current module.

illustrate:

In most cases, it's best to think of static as a synonym for private (similar to C++), which more closely describes its purpose. Declaring variables with the keyword static makes them private to the function that applies them.

function privatization

Why should functions be private?

Some people may ask, whether a function or a procedure itself is to perform a certain function, it should be called, so why should it be privatized? Here is another concept to be clear: the interface.

For the description of the interface, please refer to the following information:

General principles of interface design

How to customize the C language .h header file?

One of the important functions of an interface is to open some interfaces for users to use, and hide the complex implementation inside the interface. So when an interface is defined, the functions exported by the interface for use by clients are not private. The point of an interface is to make these functions callable from other modules. In many cases, the interface will also have some functions that are not open to customers in order to implement the interface functions. However, the customer may call these functions, or the customer-defined function may have the same name as the function. In this case, a certain function is required to be restricted to a certain module before it can be used, so that the client cannot call these functions, thus making the abstract boundary between the interface and the user more stable and firm.

How are functions private?

Function privatization is to use the keyword static in front of the prototype and implementation, such as:

static string buffer;

illustrate:

Declaring a function as static is also beneficial in large programming environments where several programmers are involved in development. If functions or global variables are not declared static, other modules in the module set that make up the entire program cannot use these names. In this case, programmers may need more communication and increase development costs. Therefore, the static keyword ensures that the names they use are private to their own modules.

The following rules are excellent guidelines for modular development.

Static declaration principle: All functions must be declared static except for the main function and functions explicitly exported by the interface.


2018.05.09 12:35

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326454369&siteId=291194637