Skip to content

Instantly share code, notes, and snippets.

@smtalimett
Last active January 20, 2023 05:20
Show Gist options
  • Save smtalimett/9d14e90a604bd4cf14ed4a8b3e110152 to your computer and use it in GitHub Desktop.
Save smtalimett/9d14e90a604bd4cf14ed4a8b3e110152 to your computer and use it in GitHub Desktop.
A simple Solidity program with Getters and Setters
pragma solidity ^0.4.0;
// A simple smart contract
contract MessageContract {
string message = "Hello World";
function getMessage() public constant returns(string) {
return message;
}
function setMessage(string newMessage) public {
message = newMessage;
}
}
@istiaque010
Copy link

pragma solidity 0.8.11;

contract Greeter{

string greeting;

function greeter(string memory _greeting) public{
    greeting = _greeting;

}

function greet() public returns(string memory)
{
    return greeting;
}

}

@orlundo-wursta
Copy link

@istiaque010 isn't this the long way around though?

cant you just so something like:

` string public greeting;

function greeter(string memory _greeting) public {
greeting = _greeting;
}`

This way you'll be able to call the greeter function and still be able to see the greeting since the visibility is public? Just wondering.

@aignermax
Copy link

aignermax commented Nov 3, 2022

@orlundo-wursta Public variables are bad practice because it is difficult to keep control over them thus they should be avoided. The contract should be a black box to the outside world following the rules of encapsulation - you could use getter and setter functions if it really has to be global.
https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/

@x5engine
Copy link

@orlundo-wursta Public variables are bad practice because it is difficult to keep control over them thus they should be avoided. The contract should be a black box to the outside world following the rules of encapsulation - you could use getter and setter functions if it really has to be global. https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/

bro this isn't java, this is THE blockchain! keep the needed vars public so you don't have to waste gas or deploy extra code for nothing!
private stuff should stay private ;)

@aignermax
Copy link

@orlundo-wursta Public variables are bad practice because it is difficult to keep control over them thus they should be avoided. The contract should be a black box to the outside world following the rules of encapsulation - you could use getter and setter functions if it really has to be global. https://stackify.com/oop-concept-for-beginners-what-is-encapsulation/

bro this isn't java, this is THE blockchain! keep the needed vars public so you don't have to waste gas or deploy extra code for nothing! private stuff should stay private ;)

Well if the code is not too big then go ahead. This reminds me on the Atmega 128 microprocessor programming days where you don't care much about architecture because the code cannot be big enough on those tiny processors to be super unreadyable anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment