Thursday, July 8, 2010

Hard coding

Hard coding (also, hard-coding or hardcoding) refers to the software development practice of embedding input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input.

-Wikipedia http://en.wikipedia.org/wiki/Hard_coding

Hard coding has been widely known as a bad practice but I've seen in a lot of programs that constants are being misused to resolve "hard coding" issues. For example, in a java class that has SQL queries, people tend to break down the SQL into pieces and define java constants for the table names or even column names. Their reason is, if the table name or column name changed, it will be easy to change in java class - since there's only one constant for that table or column name. It seems making perfect sense. But think it twice, how often does a table or a column change, how often does the code need to be altered than viewed? Needless to say how hard it is to jumping over the variables when reading the code. Also when there's a change needs to be done, how do you navigate to the places? Isn't it a massive search and replace? So what's the difference between replacing it in one place and replacing it in multiple places? No difference, it's automatic search and replace!
Moreover, embedding SQL in java class isn't really hard coding if you think of the table name as a variable name.

No comments:

Post a Comment