
What does this code do? - fogus
http://www.johndcook.com/blog/2010/07/21/what-does-this-code-do/
======
mcantor
Note: Spoiler alert.

The author says, "Meaningful variable and function names would have helped,
but..." and I don't really see why he needed the "but." The first function can
be renamed "get_exponent(base, exponent)" and the second,
"get_random_integer()", and that's the end of it. What more comments would
really be appropriate?

~~~
jpablo
I think a comment specifying the name of the algorithm would be great if you
want to dig deeper when the function isn't working.

~~~
mcantor
That's a good point. The author said, "... a comment explaining the problem
the code is trying to solve [would have helped]," which is what threw me--
that's what the function name is for!

------
BoppreH

      if n < 0:
            n = -n
            x = 1.0 / x
    

Dead giveaway.

~~~
silentbicycle
Dead giveaway _for developers who know math_ , you mean.

~~~
calcnerd256
Who else should be reading procedural code written in a compiled language?

~~~
silentbicycle
Well, sure, but it doesn't always work out that way.

------
edw519
I once worked for a software house that had real standards, real code walk-
throughs, and real quality control. Standards were enforced vigorously through
automation and peer review. Those standards included precise variable naming
conventions.

At the time, I hated it because I thought it cramped my style.

Since then, I have worked in many shops with no standards at all and most of
the crap I've had to maintain looked just like OP's examples. Probably written
by someone who didn't like his style cramped.

Funny how it sometimes takes years to understand that which once made no
sense.

~~~
dangoldin
Reminded me of a good quote:

“When I was a boy of 14, my father was so ignorant I could hardly stand to
have the old man around. But when I got to be 21, I was astonished at how much
the old man had learned in seven years.” - Mark Twain

------
JoeAltmaier
The reason to document WHAT code does, is so a reader can parse it faster. The
most common ignorant reader is yourself, several weeks/months later.

Method names are good, but some clue about the approach is helpful too, which
the method name would probably not include.

------
drx
The article asks the question "what does this code do" but never reveals the
answer. The first algorithm is fast exponentiation.

~~~
mcantor
He provides links for both functions, here:
[http://www.johndcook.com/blog/2008/12/10/fast-
exponentiation...](http://www.johndcook.com/blog/2008/12/10/fast-
exponentiation/) and here:
<http://www.codeproject.com/KB/recipes/SimpleRNG.aspx>

~~~
drx
Ah, I missed that, my bad.

