[Ethereum] Why creating a private variable and a getter instead of just creating a public variable in solidity

solidity

I want to implement a contract like the
ERC20Detailed.sol
example from open Zeppelin:

My question: why setting a private variable and then, creating a getter for the same variable when the solidity compiler can automatically create the getter for a public variable ?

According to Solidity: private vs public variables it seams there is no advantage of doing that.

Am I missing something ?

Best Answer

I see good observations about style but I didn't see the safety aspect.

Setting a variable to private ensures that it can only be manipulated via functions in the contract designed for that purpose and never by a successor contract that inherited it.

That means an errant contract could not accidentally zero out, say, owner ... a potentially disastrous accident that is preventable with private.

So, it's worth considering for vital state elements.

Hope it helps.

UPDATE

    pragma solidity 0.4.25;
    
    contract Risky {
        
        string public vital;
        
        constructor() public {
            vital = "do not change";
        }
    }
    
    contract Careless is Risky {
        
        function oops() public {
            
            /*
            Since vital is "public", this child contract is free to write to it.
            This is not ideal because it could lead to critical values overwritten.
            This child contract is free to make mistakes that could be disasterous.
            */
            
            vital = "game over";
        }
    }
    
    /*
    ----------------------------------------
    Using private for safety
    ----------------------------------------
    */
    
    contract Cautious {
        
        string private vital;
        
        constructor() public {
            vital = "cannot be changed by accident";
        }
        
        function getVital() public view returns(string) {
            return vital;
        }
        
        function setVital(string youKnowWhatYoureDoing) public {
            vital = youKnowWhatYoureDoing;
        }
    }
    
    contract Isolated is Cautious {
        
        /* 
        This child cannot stomp on Cautious' "vital" even if it wants to because it's not "public" or "internal". 
        It *must* use Cautious' getter and setter functions which means Cautious is in control. 
        */
        
    }