Why are state variables private rather than public in OpenZeppelin Contracts?

I have seen in tokens that use the variables decimals and etc as private and create a function to access them, why?

1 Like

You can find some explanation of visibility in this documentation: https://solidity.readthedocs.io/en/v0.7.4/contracts.html#visibility-and-getters, as for why create a function to access private variables, maybe in order to avoid exposing variables directly, and I think we should ask @abcoathup for a real reason.

1 Like

Hi @franco_villa_santana,

(Thanks @Skyge as always for being awesome :pray:)

State variables being private is part of the OpenZeppelin Contracts style guidelines:

The explanation for this can be found in the Release notes:

From: https://github.com/OpenZeppelin/openzeppelin-contracts/releases/tag/v2.0.0

All state variables are now private , which means that derived contracts cannot access them directly, but have to use getters. This is to increase encapsulation, to be able to reason better about the code.

1 Like