Solidity For Beginners
The web has changed dramatically over the years, and its current applications are nearly unrecognizable from its early days. The web’s evolution is frequently divided into three stages: Web 1.0, Web 2.0, and Web 3.0.
web 3.0 which aims to bring about decentralization and uses the blockchain to achieve this feat is pretty much at the beginner phase right now and anyone getting into it right now is the proverbial early bird and in order for one develop applications and write smart contracts on web 3, two languages are used which are solidity and vyper, the popular one is solidity and this writeup is going to cover the basic things you need to know when starting with solidity.
What is solidity?
Solidity is an Ethereum smart contract language and an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs that govern the behavior of accounts within the Ethereum state.
Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features. With Solidity, you can create contracts for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets.
Solidity is useful for writing smart contracts and building decentralized applications, smart contracts are digital contracts stored on a blockchain that is automatically executed when predetermined terms and conditions are met, also the syntax of solidity is similar to that of javascript. Solidity Data types
Solidity is a statically typed programming language which means that the type for variable needs to be specified. For those coming from the javascript background the null and undefined data types does not exist in solidity but each type has it own default values i.e the default value for address is 0x0000000000000000000000000000000000000000.
The Solidity data types can be classified according to the data location. If a variable stores its own data; it is a value type. If it holds the location of the data; it is a reference type.
Value Types in Solidity
We know that the value type are those that store their own data and they are: Booleans
The possible values are constants that are true and false. The operators are…
- ! (logical negation)
- && (logical conjunction, “and”)
- || (logical disjunction, “or”)
- == (equality)
- != (inequality)
- Integers
These are divided into int and uint, while the int stands for signed integers and these are just a range of positive numbers and the uint stands for unsigned integers and includes positive and negative numbers.
String Literals and Types
String literals are written with either double or single quotes ( “foo” or ‘bar’ ), and they can also be split into multiple consecutive parts ( “foo” “bar” is equivalent to “foobar” ) which can be helpful when dealing with long strings. They do not imply trailing zeroes as in C; “foo” represents three bytes, not four. As with integer literals, their type can vary, butthey are implicitly convertible to bytes1, …, bytes32, if they fit, to bytes and to string. Address
The address comes in two ways and they are almost identical
Address: Holds a 20-byte value (size of an Ethereum address).
Address Payable: same as an address, with two more members, transfer and send.
The idea behind this distinction is that address payable is an address you can send Ether to, while a plain address cannot be sent Ether. Fixed-size byte arrays
The value types bytes1, bytes2, bytes3, …, bytes32 hold a sequence of bytes from one to up to 32. a byte is an alias for bytes1.
Reference Types
Values of a reference type can be modified through multiple different names. Contrast this with value types where you get an independent copy whenever a variable of value type is used. Because of that, reference types have to be handled more carefully than value types. Currently, reference types comprise structs, arrays, and mappings. If you use a reference type, you always have to explicitly provide the data area where the type is stored.
## Struct
Solidity allows user to create their own data type in the form of structure. The struct contains a group of elements with a different data type. Generally, it is used to represent a record. To define a structure struct keyword is used, which creates a new data type.
## Enum
Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a failing assert otherwise. Enums require at least one member, and its default value when declared is the first member.
##Array
Arrays are data structures that store the fixed collection of elements of the same data types in which each and every element has a specific location called index. Instead of creating numerous individual variables of the same type, we just declare one array of the required size and store the elements in the array and can be accessed using the index. In Solidity, an array can be of fixed size or dynamic size.
## Types of variables
The type of variables are classified based on they declared in the smart contract, these can be likened to the concept of scope in javascript in which we have local and global variables but in solidity we have:
State Variables: these are variables that are not declared within any function and can be accessed by any function in the smart contract, they are permanently stored in the smart contract
Local Variables: these are variables declared inside functions can only be accessed inside the function in which they declared and they are present till the function is executed
Global Variables: These are some special variables that can be used globally and give information about the transactions and blockChain properties.
Memory and Location
Storage: This is where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use. Local variables of struct, array, or mapping type reference storage by default.
Memory: this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.
Function Types
A function is a group of reusable codes that can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes. The various kinds of function types in solidity are discussed below
External functions: External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works). External functions are sometimes more efficient when they receive large arrays of data because the data is not copied from call data to memory.
Public functions: Public functions are part of the contract interface and can be either called internally or via messages. For public state variables, an automatic getter function is generated. Internal functions: Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using this. Private functions: Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.
Also, functions can be classified into pure and view based on how they interact with the state variables.
**View Functions:** These are functions that read and modify the state variables.
**Pure Functions:** These are functions that do not read or modify state variables in the smart contract.
Finally, another concept in functions to know about is function modifiers they are used to change the behaviors of functions in a smart contract.
We’ve come to the end of this post, but this is just a brief overview of the solidity programming language, other concepts that you need to know are listed below and the resources you need to go through.
Handling Errors in solidity
Inheritance in Solidity
Events
Fallback functions
Constructor
More Resources
- Solidity Tutorial for Beginnners Youtube
- Official Solidity Docs
- Etheruem Solidity Docs
- Smart Contract Programmer Youtube