Bug 145759 - Use symbolic constants instead of magic numerical constants
Summary: Use symbolic constants instead of magic numerical constants
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
(earliest affected)
Hardware: All All
: medium normal
Assignee: Somya Srivastava
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Reported: 2021-11-18 14:48 UTC by Hossein
Modified: 2021-11-23 15:23 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2021-11-18 14:48:11 UTC
Using a numerical literal directly in the code can cause problems. Because of this, it is suggested that it should be replaced by some numerical constant.

ES.45: Avoid "magic constants"; use symbolic constants

If it is known (like π), you should use the appropriate symbolic constant like M_PI. If not, you should define a new constant with proper name and type with 'contsexpr'.

One solution to find such magic constants is to start from a list of some well known mathematical constants:

Then, store some of them in a text file, let's say 'constants.txt', then search for all these values inside CPP files:

git grep -Ff constants.txt *.cxx *.hxx

Many of these symbolic constants like M_PI are already defined in C++ standard library or some place in the LibreOffice code, and you can use them easily.

You should examine the 'grep' results carefully, because not every 3.14 refers to PI.
Comment 1 Xisco Faulí 2021-11-23 10:04:24 UTC
Moving to NEW
Comment 2 Somya Srivastava 2021-11-23 15:22:25 UTC
Hi! I would like to work on this issue.