-2

I understand that declaring constants is a way to use same identifiers whose value isn't going to change over time. But I just couldn't think of real life use-cases where it is extensively used. Also when do we really need to use a constant instead of a variable in any programming language?

3
  • What do you mean by "extensively"? There are thousands of code bases on GitHub you can browse for free, and I'm sure some of them use constants. You can always substitute a variable for a constant, but it will likely be less efficient. Commented Nov 3, 2021 at 14:13
  • If you're operating in an immutable environment or programming language like Haskell or Erlang, technically everything is constant. Commented Nov 3, 2021 at 14:26
  • 2
    In any case, the main purpose of declaring a constant is to have a single point of change. Pi to five decimal places can be represented by the digits 3.14159, or you can simply use the constant pi, which is more readable; and if the number of digits of precision you need changes, you only have to make that change in one place. Commented Nov 3, 2021 at 14:28

1 Answer 1

0

By defining a constant, you won't need to put the same values hardcoded in your code periodically. You can avoid repetition. And you can modify these group values at once by modifying their definition.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.