99 bottles of beer: Difference between revisions

m
Line 7,817:
Most people solving this task iterate from 99 to 0. But 2, 1 and 0 are special cases, thus they write conditional expressions to split the flow control. Of course this leads to unnecessary entanglement in the source code.
 
It seems to be due to the way the human brain works. We divide the problem into smaller parts (top-down programming). Once we manage to find a solution to one sub-problem, we try - because it is the most intrusive - to solve the next one in a similar way. However, what is appropriate for one component (verses with consecutive numbers) may not be very good for another component of the problem (the verse without numbers). To understand this you need to be able to '''look at the solving process as a way to achieve the goal. '''. The goal is not to put everything in one ''for'' loop.''' The goal is to correctly write the lyrics of the entire song without unnecessary repetition of the repetitive elements.
 
Therefore it is better exclude these special cases outside the loop. This significantly reduces the number of conditional statements, which is generally a good idea. (Conditional statements hamper pipelining in modern CPUs.)
Line 7,823:
Compared to hackish "not hard coded" spagetti examples (in Python) below you should notice that this trivial solution is: 1. shorter; 2. simpler; 3. more readliable; 4. easy for i18n and l10n (including Hebrew, Japan, Korean); 5. open to extensions; 6. more pythonic (see Zen of Python); 7. better optimized and faster; 8. funny.
 
Quoting from a text formulating the terms of the task: "''simple, obvious solutions [are] allowed"''.
 
BTW, there is a reason to use ''REGULAR_VERSES'' and ''ENDING_VERSES'' constants instead place the text literals directly in program.