Bug 145759 - Use symbolic constants instead of magic numerical constants
Summary: Use symbolic constants instead of magic numerical constants
Status: ASSIGNED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Somya Srivastava
URL:
Whiteboard:
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Blocks:
 
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:


Attachments

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
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-magic

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:
https://en.wikipedia.org/wiki/List_of_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.