1/5
## Antipattern: Magic Numbers We're going to learn about a programming antipattern called "magic numbers." An antipattern is essentially a "bad coding habit" or an unrecommended way to write code. We can see a magic number in the following code block: ```python eth_amount_in_usd: uint256 = (eth_price * eth_amount) // (10 ** 18) ``` The magic number here is 18. We'll improve readability by using a variable to represent this value: ```python PRECISION: constant(uint256) = 1 * (10 ** 18) ``` Now, we can replace the magic number with the `PRECISION` variable, which makes the code clearer: ```python eth_amount_in_usd: uint256 = (eth_price * eth_amount) // PRECISION ```
A practical guide to refactoring magic numbers in Vyper code. This lesson introduces the concept of magic numbers, explains their drawbacks, and demonstrates how to replace them with variables and constants to improve code readability and maintainability.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on May 15, 2025
Vyper Developer
Introduction to Python and VyperDuration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on May 15, 2025