Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ This one should be obvious. Functions increase a contract size quite a bit.

### Avoid additional variables {#avoid-additional-variables}

A simple change like this:

```solidity
function get(uint id) returns (address,address) {
MyStruct memory myStruct = myStructs[id];
Expand All @@ -66,15 +64,14 @@ function get(uint id) returns (address,address) {
}
```

makes a difference of **0.28kb**. Chances are you can find many similar situations in your contracts and those can really add up to significant amounts.
A simple change like this makes a difference of **0.28kb**. Chances are you can find many similar situations in your contracts and those can really add up to significant amounts.

### Shorten error message {#shorten-error-message}

Long revert messages and in particular many different revert messages can bloat up the contract. Instead use short error codes and decode them in your contract. A long message could be become much shorter:

```solidity
require(msg.sender == owner, "Only the owner of this contract can call this function");

```

```solidity
Expand All @@ -101,7 +98,7 @@ You can also change the optimizer settings. The default value of 200 means that

### Avoid passing structs to functions {#avoid-passing-structs-to-functions}

If you are using the [ABIEncoderV2](https://solidity.readthedocs.io/en/v0.6.10/layout-of-source-files.html#abiencoderv2), it can help to not pass structs to a function. Instead of passing the parameter as a struct...
If you are using the [ABIEncoderV2](https://solidity.readthedocs.io/en/v0.6.10/layout-of-source-files.html#abiencoderv2), it can help to not pass structs to a function. Instead of passing the parameter as a struct, pass the required parameters directly. In this example we saved another **0.1kb**.

```solidity
function get(uint id) returns (address,address) {
Expand All @@ -123,8 +120,6 @@ function _get(address addr1, address addr2) private view returns(address,address
}
```

... pass the required parameters directly. In this example we saved another **0.1kb**.

### Declare correct visibility for functions and variables {#declare-correct-visibility-for-functions-and-variables}

- Functions or variables that are only called from the outside? Declare them as `external` instead of `public`.
Expand Down