tags:

views:

76

answers:

4

Recently there was a big debate during a code reveiw session on the use of constants. The developers had used constants for the following purposes:

  1. Each and every message key used in the i18N application was declared as a constant. The application contained around 3000 message keys and hence the same number of constants.
  2. Each and every database column name was declared as a constant. There were around 5000 column names and still counting..

Does it make sense to have such a huge number of constants in any application? IMHO, common sense should prevail. Message keys just don't need to be declared as constants. We already have one level of indirection - why add one more?

Reg. database column names, I have mixed opinions. If a column is being used in multiple classes, does it make sense to declare it as a global constant?

Please pour in with your thoughts...

A: 

If the constant is used in multiple places and the compiler really catches the problem, yes.

Thomas Jung
A: 

This depends on the programming language, I think.

In PHP it's not uncommon to ude defines aka contants for such things, while I'd not use this in Java or C#.

In most projects we tried to extract the SQL to templates, so not only the table and column names were configurable but the whole sql statement. We used velocity for basic templating mechanics like variables, small loops,...

Regarding the language constants: Another layer doesn't make much sense to me, but you hav eto choose your identifiers for the language translation carefully. Using the whole english sentence as key may end up in a lot of work for the translators if you fix the wording for example in the english sentence without changing the meaning. So all translators would have to update their files.

Patrick Cornelissen
+1  A: 

I think is a good practice to put message keys used for i18N as constants. I don't see much benefits in doing the same for the DB columns, if you have a well designed persistence layer.

JuanZe
+2  A: 

If I18N message keys aren't defined as constants, how do you enforce consistency? How do you automatically differentiate between a typo and a missing value? How do you audit to make sure that all I18N keys are fulfilled in each new language file?

As to database columns, you could definitely use some indirection - if your application knows about column names, you've got a binding problem. So there, you might consider a config file with the actual column names - but of course, you would want to refer to the column names by symbolic keys, which should be defined as auditable constants, just like the I18N keys.

CPerkins