Why we use Constant ?

Certified Associate Developer

Hi everyone, 

 Can anyone explain the purpose of using constant ? I know that constant is literal values, and it can be resued in multiple objects. Is there any purpose of using constant ? Moreover, when we will create constant in application ?

Thanks!

  Discussion posts and replies are publicly visible

Parents
  • Constants give you two valuable outcomes:

    1. you can hold the definition of something once and re-use it throughout your application many times. If, for example, you had an application that needed to make reference to a Sales Tax value, and that application needed to refer to that value in multiple places then when the Sales Tax value changes you only have to change it once, in the constant, rather than everywhere in the application that needs to use that value
    2. it also makes your code more readable (assuming you name your constant meaningfully). So, the difference between:
      1. local!totalOrderValue: fn!sum(local!orders.orderValue) * 12.5
      2. local!totalOrderValue: fn!sum(local!orders.orderValue) * cons!APP_DECIMAL_SALES_TAX_PERCENT

    Beware "magic numbers" in your code, makes it hard to read and, if used in more than one place, hard to maintain.

Reply
  • Constants give you two valuable outcomes:

    1. you can hold the definition of something once and re-use it throughout your application many times. If, for example, you had an application that needed to make reference to a Sales Tax value, and that application needed to refer to that value in multiple places then when the Sales Tax value changes you only have to change it once, in the constant, rather than everywhere in the application that needs to use that value
    2. it also makes your code more readable (assuming you name your constant meaningfully). So, the difference between:
      1. local!totalOrderValue: fn!sum(local!orders.orderValue) * 12.5
      2. local!totalOrderValue: fn!sum(local!orders.orderValue) * cons!APP_DECIMAL_SALES_TAX_PERCENT

    Beware "magic numbers" in your code, makes it hard to read and, if used in more than one place, hard to maintain.

Children
No Data