TwentyOne

First Flight #29
Beginner FriendlyGameFiFoundrySolidity
100 EXP
View results
Submission Details
Severity: low
Invalid

Using Magic Number instead of const

Description:
A magic number is a hardcoded numeric value used directly in the code without explanation. Magic numbers reduce the readability and maintainability of the code because they lack descriptive context. If the value needs to be changed or reused, developers must hunt through the code to find all instances of the number, increasing the risk of errors.

function playersHand(address player) public view returns (uint256) {
_;
if (cardValue == 0 || cardValue >= 10) {
@> playerTotal += 10;
}
_;
function dealersHand(address player) public view returns (uint256) {
_;
if (cardValue == 0 || cardValue >= 10) {
@> playerTotal += 10;
}
_;

Impact:

  1. Reduced Code Readability:
    Other developers or auditors may struggle to understand the purpose of the hardcoded value, making the code harder to interpret and debug.

  2. Increased Risk of Errors:
    

If the same magic number is used in multiple places and needs to be updated, there's a higher chance of missing one instance or inconsistently applying the update.

  1. Lack of Flexibility:
    Magic numbers make it difficult to change behavior dynamically or adapt to new requirements since the hardcoded values aren't centralized.

Recommended Mitigation:

  1. use a const with values of all magic numbers.

Updates

Lead Judging Commences

inallhonesty Lead Judge 11 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.