Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

New rule: Magic numbers (or literals) should be replaced with symbolic constants

Open
#423 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
java
Domain
devtools

Research direction

No implementation files, tests, or entry points are named in the issue. Start by locating the Java rule implementations in this SonarQube Delphi plugin and comparing their test structure. Done means a new rule reports non-exempt magic literals and covers the documented examples and exceptions with tests.

Written by the indexing model from the issue text.

Description

feature rule
Prerequisites
  • This rule has not already been suggested.
  • This should be a new rule, not an improvement to an existing rule.
  • This rule would be generally useful, not specific to my code or setup.
Suggested rule title

Magic numbers (or literals) should be replaced with symbolic constants

Rule description

Magic values are numbers or other literals that appear inlne in code without any obvious meaning. This rule should identify such literals and report them to be replaced with a symbolic constant. Simple examples can be seen with error codes or scientific constants:

if e.ErrorCode = 5 then WriteLn('You may not pass!');

would be better as:

if e.ErrorCode = ERROR_ACCES_DENIED then WriteLn('You may not pass!');

Note: not every number is a magic value. I would neither consider 0 nor 1 in this code a magic value:

for (var i := 0 to myList.Count - 1) do

If found a good description of such a rule here (not mine!): https://refactoring.guru/replace-magic-number-with-symbolic-constant

Rationale

Magic values (most often numbers, applies to other literals as well though) make it harder to read, understand and maintain code. By replacing them with a symbolic constant, one gains the benefits of

  • a single/reusable point of definition that can be updated when needed
  • a meaningful/symbolic name that describes and documents the contents and improves readability
Dominant language
Java
Stars
159
Forks
33
Avg merge
4d 17h
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from integrated-application-development/sonar-delphi

All issues in integrated-application-development/sonar-delphi

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.