Use constants always


Link to this posting

Postby Ursego » 24 May 2013, 12:07

If a constant is defined in the application for a value you are using, ALWAYS use the constant rather then hard-coded value.

Sometimes, it's much easier to simply hard-code the values rather than use constants. But what if those system codes will be changed in the future? In fact, that's why we enjoy constants at all: it's enough to change one value (of the constant itself), and that change automatically spreads out through the whole application, so the existing logic keeps working everywhere. I don't believe you are against using constants in comparisons in PowerScript code. But if you use constants only there, it won't be really "through the whole application" - there are other situation when application codes are used. Some of them are described here.

Use constants in expressions

Use concatenation to utilize constants in all expressions - such as expressions for functions (Find(), SetFilter(), Modify() etc.) and expressions for Required and Protected properties of columns. For Protected property, use the uf_protect_col() function.

Populate Code Tables from constants

Creating a control with a Code Table (like DropDownListBox or RadioButton), populate its Code Table programmatically if constants are defined in the application for the added values (instead of hard-coding in the DataWindow painter). That is done using the SetValue() method of DataWindow. For example, you have to create a radio button for the field "payment_method" in your DW while constants n_payment_method.CREDIT_CARD (with value "C") and n_payment_method.PAYPAL (with value "P") exist in your system (see here):

Code: Select all
dw_payment.SetValue("payment_method", 1, "Credit Card~t" + n_payment_method.CREDIT_CARD)
dw_payment.SetValue("payment_method", 2, "PayPal~t" + n_payment_method.PAYPAL)

Use constants in SQL SELECTs of DWs

For that, pass them as retrieval arguments.
User avatar
Ursego
Site Admin
 
Posts: 127
Joined: 19 Feb 2013, 20:33



IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:




cron
free counters

eXTReMe Tracker