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

  • Certified Lead Developer

    Generally we use constants where otherwise we'd need to hardcode values that could change in the future.  Imagine implementing a new system from scratch, and there's a business term / Buzzword used in interfaces, processes, and other types of logic all over the system - and at the start of the project build-out, you're only ~90% sure that the term you're using will be the final version.  When care is taken to use the constant instead of manually referring to that buzzword everwhere, and it eventually changes (even just a subtle change - imagine a tweak of how the word is spelled or even just capitalized), then the only thing that needs to be done is to change the value of the constant, instead of potentially hours of manual rework sifting through the system and changing its spelling in all sorts of places.

    Also for some things we'd like to refer to occasionally, it's either impossible or really difficult to hardcode a value even if we wanted to.  That can be references to a specific folder or document, or a process model, or a data store entity (among other things).  Constants make it quick and easy to pass such things into queries, components, subprocess calls, or whatever is needed.

  • 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.

  • Certified Senior Developer

    1.suppose you are providing  Gender (1.Male 2.Female) in 10 form interfaces, so you don't have to hard code every time just create a constant and use it as value in all forms .

    2.for same example , if in future you want add one more gender as (3.Others) so if you have used constant you have to update only your constant and the gender will be updated in all 10 form interfaces, if you don't use Constant in these type of scenarios you have to change gender manually in all 10 forms.