Which of the following statements correctly describe a way to improve the performance, readability or maintainability of this block of code?
BEGIN FOR month_index IN 1 .. 12 LOOP UPDATE monthly_sales SET pct_of_sales = 100 WHERE company_id = 10006 AND month_number = month_index; END LOOP; END;We scored as correct the following choice:
"Replace all hard-coded literal values with named constants or function calls."
Several players did not agree. I will offer one such comment and then open it up for discussion:
While it's generally a good idea to replace magic numbers with sensibly named constants, in the case of month names I feel the month number itself is a very well readable and commonly used name (as in "Quiz for 2011-01-11 Tuesday"). And did you know that even in German we have two names for the first month, namely "Januar" and "JÃnner"? But why is this "AND month_number BETWEEN 1 AND 12" clause there anyway? Unless someone invents more months (e. g. "Tricember") all months in any real world table may be assumed to range between 1 and 12 unless they are null. So I would probably change the statement like UPDATE monthly_sales SET pct_of_sales = percent_in WHERE company_id = company_id_in Yet another point is binding: If you introduce constants, they will be bound where they are used in SQL statements. The literals won't. Binding constants is probably not as good an idea as binding variables. Well these are just my 2 cents.
Ah - one other thing: another person objected to scoring this choice as correct because he feels it would WORSEN performance (I will leave it to the player to post his comments here). Even if that were true, the question uses the word "or" not "and" - so as long as replacement of literals with constants satisfies improved readability or maintainability, it does NOT have to improve performance.
So, dear players, what do you think?