Understanding Solidity Syntax and Data Types

I am a blockchain software developer. Building for better user experience and satisfaction.
Solidity is a programming language specifically designed for creating smart contracts on the Ethereum Virtual Machine (EVM). The syntax is similar to JavaScript and supports a variety of data types and constructs.
Like any other programming language, you work with variables. When declaring a variable in solidity, you need to state the data type and the visibility before the variable name. This is to help prevent unnecessary type conversion and to help the compiler know how much space to allocate to each variable. so it looks like this:
// Signed integer (int) and unsigned integer (uint)
int256 public number = 10;
uint256 public number = 10;
//Strings
string public greeting = "Hello, World!";
int256, uint256, string - data types
public - visibility
number, greetings - variable name
Signed integers represent both negative and positive integers and unsigned integers only represent non-negative integers (zero and positive numbers).
Visibilities
Visibilities attached to variables determine how those variables can be accessed or modified by other parts of the contract or by external contracts. The four visibility levels are public, private, eternal and internal.
A public contract can be accessed outside the contract, typically using getter functions but it cannot be modified directly from outside the contract.
A private variable can only be accessed and modified within the same contract where they are declared. They are not visible to external contracts or even derived contracts.
External variables are similar to public variables in that they can be accessed from outside the contract, but they cannot be read directly within the contract. They are typically used for function parameters to prevent direct access to the variable's value within the contract.
Internal variables are visible to the contract that declares them and any contracts derived from it (i.e., contracts that inherit from it). They cannot be accessed from external contracts.
Data Types
Solidity supports various data types, such as:
Boolean:
bool- true or falseAddress:
addressString:
string- lettersArrays: dynamic-size or fixed-size and can be declared with other types.
Structs:
struct- user-defined types that group multiple variables of possibly different types under a single name.Enums:
enum- user-defined type with a finite set of possible valuesMapping:
mapping- an array of key-value pairs
Here are implementations for each data type:
// Boolean
bool public isCompleted = false;
// Integer (signed and unsigned)
int256 public number = -10;
uint256 public number = 10;
// Address
address public userAddress = 0x742d35Cc6634C0532925a3b844Bc454e4438f44e;
// String
string public greeting = "Hello, World!";
// Bytes (dynamic-size and fixed-size)
bytes public dynamicBytes = "hello, solidity";
bytes32 public fixedBytes = "hello, solidity";
// Arrays (dynamic-size and fixed-size)
uint[] public dynamicArray = [1, 2, 3];
uint[5] public fixedArray = [1, 2, 3, 4, 5];
address[] public dynamicAddressArray = [0xd41d1744871f42Bb724D777A2d0Bf53FB43a0040, 0x1f514ae9834aEAF6c2c3eb6D20E27e865F419010];
address[3] public fixedAddressArray = [0xC90cd0D820D6dc447B3cD9545185B046873786A6, 0x401997E856CE51e0D4A8f26ce64952313BEA0E25, 0x221d3b9821f3Cc49B42E7dd487E2a6d1b3ed0E05];
bool[] public dynamicBoolArray = [true, false, true];
bool[2] public fixedBoolArray = [true, false];
// Struct
struct Person {
string name;
uint age;
}
Person public person = Person("Alice", 30);
// Enums
enum Status { Open, Closed, Pending }
Status public currentStatus = Status.Open;
Status public nextStatus = Status.Closed;
Status public previousStatus = Status.Pending;
// Mapping
mapping(address => uint) public balances;
There are a lot more I didn't touch on but hope this helps with readability the next time you come across a solidity code block. Have fun and enjoy!!

