0

I am new to solidity.Below is the snippet of solidity source code which I came across :-

    pragma solidity ^0.5.1;

library SafeMath { 
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
      assert(b <= a); //error handling condition
      return a - b;
    }

    function add(uint256 a, uint256 b) internal pure returns (uint256) {
      uint256 c = a + b;
     
      return c;
    }
}

contract SmartContract {          
   

    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
    event Transfer(address indexed from, address indexed to, uint tokens);

    modifier onlyOwner  {
        require (msg.sender == ownerCon);
        _;
    }
    
    mapping(address => uint256) balances;  
    

    uint256 totalSupply_;
    address ownerCon;
    using SafeMath for uint256;
    string public constant name = "SmartCoin"; 
    string public constant symbol = "SC"; 
    uint8 public constant decimals = 18;     

// some more functions

    }

Here all the entire variable declaration was not done in the beginning as what we do in C++ , but rather in the middle of programme. I want to know that whether this Smart contract will work properly or not. And if it will , then how the compiler will compile the source code given that it encounters few functions even before those variables have been declared .

1 Answer 1

0

the compiler has an expected layout for the order in which the code is laid out

Layout contract elements in the following order:

  1. Pragma statements
  2. Import statements
  3. Interfaces
  4. Libraries
  5. Contracts

Inside each contract, library or interface, use the following order:

  1. Type declarations
  2. State variables
  3. Events Modifiers
  4. Functions

At a quick glance, that contract should compile. Please check out this link for more info on the code layout:

https://docs.soliditylang.org/en/v0.8.14/style-guide.html

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.