Solve a Debate

Quentin Gallet-Gilles qgallet at gmail.com
Fri Feb 15 11:58:22 EST 2008


If the data becomes much bigger, change your way of storing it, not the
code. You don't want to code hundreds of "if - elif - else" because you have
hundreds of different data, right ? TheDailyWTF is full of horror stories
like this, by the way ;-)
Data growth shouldn't result in modification in logic code.

Also I find that avoiding conditionals is always a win. That's in the spirit
of the pythonic way of removing what would be an if-elif chain (or a switch
statement in another language) by a dict-based dispatch.

Returning to your month exercise... Since "code is data", assigning the
numbers of days or comparing the month number in if statements is basically
the same. In the end, you have twelve different values stored in one way or
another in your program and loaded in RAM at execution time.

Therefore, I find the array solution (or whatever storage you wish to use)
better by all counts.

Cheers,
Quentin

On Fri, Feb 15, 2008 at 5:24 PM, nexes <nexes128 at gmail.com> wrote:

> Alright so me and my friend are having argument.
>
> Ok the problem we had been asked a while back, to do a programming
> exercise (in college)
> That would tell you how many days there are in a month given a
> specific month.
>
> Ok I did my like this (just pseudo):
>
> If month = 1 or 3 or etc ....
>        noDays = 31
> Elseif month = 4 or 6 etc ....
>        noDays = 30
> Else
>        noDays = 29
> (we didn't have to take into account a leap year)
>
> He declared an array and assigned the number of days in a month to its
> own element in an array. Now
> I realise that in this example it would not make a difference in terms
> of efficiency, but it is my belief that if
> there is more data that needed to be assigned(i.e. a couple megs of
> data) it would be simpler (and more efficient) to
> do a compare rather then assigning all that data to an array, since
> you are only going to be using 1 value and the rest
> of the data in the array is useless.
>
> What are everyone else's thoughts on this?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080215/43ff7272/attachment-0001.html>


More information about the Python-list mailing list