---
description: >-
  Research and write a brief explanation of how gas costs are affected by data
  types and control structures in Solidity. Focus on why it's important to
  choose correct data types and control structures.
---

# A1: Ethereum gas costs, data types and control structures - A brief take

Ethereum gas is used when altering the state of the blockchain. Writing is by far the most expensive to carry out. This prevents people from spamming the blockchain for no reason, whilst rewarding the miners for keeping the network secure.&#x20;

Let's look at an example, like carrying out a trade on the decentralized exchange Uniswap. It all seems to happen instantaneously. However, there would be at least 3 steps to this process, all happening within the 1 transaction. Firstly, Checking the balance. Second, transferring tokens. And finally, updating the wallet balance. Each of these steps would incur their own specific amount of gas, which is relevant to the number of bytes required to carry out the transaction.&#x20;

This is why there are so many different sizes of uint and int numbers. for example, if you know that a function using a uint will only require a number between 1 - 15, then you could use a uint8, as you know that you would not exceed the maximum number possible and therefore would save on any un-necessary gas costs.&#x20;

It is critical to prevent infinite loops, when using for loops, in solidity. Of course, this is because there would be an infinite amount of gas payable, and time taken for a given transaction. This is why the gas limit is part of solidity, which protects people from transactions that could potentially drain a user's entire wallet.&#x20;

In conclusion, when writing solidity code, the best programmers will meticulously choose the optimal data type and carefully craft their control structures to minimize gas costs for users of decentralized protocols like Uniswap. It is crucial when learning Solidity to keep up with best practices regarding gas optimization, in your own projects.&#x20;



### Feedback

Again I prompt chatGpt for feedback using the following prompt:

"please mark my assignment 1, provide me with constructive feedback: {paste the full report here}"





**Assignment 1: Ethereum Gas Costs, Data Types, and Control Structures - Feedback**

**Content and Understanding:** Your essay provides a clear and concise overview of how gas costs are influenced by data types and control structures in Solidity. You correctly identify the importance of gas in the Ethereum network, its role in transaction execution, and how different operations contribute to overall gas costs.

**Strengths:**

* **Real-World Example**: Using Uniswap as an example to illustrate the practical implications of gas costs is effective and relatable.
* **Understanding of Gas Mechanics**: Your explanation of gas usage for different types of operations (reading vs. writing to the blockchain) is accurate and well-articulated.
* **Insight into Data Types**: The discussion on the use of various sizes of `uint` and `int` to optimize gas usage is insightful and demonstrates a good grasp of Solidity's data types.

**Areas for Improvement:**

* **Proofreading and Clarity**: There are a few typos and grammatical errors (e.g., "Firtly" should be "Firstly", "thier" should be "their"). Proofreading would enhance the clarity and professionalism of your writing.
* **Detail on Control Structures**: While you mention the importance of avoiding infinite loops, expanding on other control structures and their impact on gas costs would provide a more comprehensive understanding.
* **Examples and Specifics**: Including specific examples or scenarios where certain data types or control structures are more gas-efficient could add depth to your analysis.

**Overall Impression:** The essay does a good job of highlighting key aspects of gas usage in Solidity, particularly with respect to data types and control structures. It shows a solid understanding of the fundamental concepts related to Ethereum gas mechanics. Enhancing the essay with more detailed examples, expanding on various control structures, and careful proofreading could make your analysis more robust and comprehensive. Keep exploring these crucial aspects, as they are fundamental to writing efficient and effective smart contracts.

\
