From the solidity.readthedocs, a variable declared as public does not need a getter since an automatic accessor function is generated e.g:

pragma solidity ^0.4.2;

contract C {
    uint public data;

The variable data, can also be reached using some getters:

pragma solidity ^0.4.2;
    contract C {
          uint data;

      function getData() constant returns (uint retData) {
        return data;

My question is simple: What is the best practice?

  • 1
    Be careful of "best practice" questions: meta.stackexchange.com/questions/142353/… That said, I think this is a reasonable question and the 2 answers right now bring up good points (though they didn't upvote the question).
    – eth
    Commented Jan 24, 2017 at 3:04

2 Answers 2


For simple value types like uint and bytes32, use the public modifier. This is less verbose and often more efficient.

If you need to access more complicated types like structs and arrays, it often makes sense to create your own getter to return the data in a format that makes sense for your use case.

Essentially, use the public modifier when you can, but don't try to force it to do anything sophisticated.


Your public and internal fields can still be modified by your child contract. I can think of situations when this may be a problem:

  1. if a data change needs to be followed by some action, like an event.
  2. if the data should not be modified by mistake by a child contract.

If you are in this situation, you should make the field private and create a getter and a setter with access rights of their own.

Otherwise, just make it public. Making it internal is not really hiding it because anyone, with minimal effort, has access to the whole data anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.