approach to writing functions

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Mon Feb 9 21:16:02 EST 2004


On Mon, 09 Feb 2004 21:22:27 -0500, Bart Nessux wrote:
> I understand that most people write functions to reuse code, no?

No.  I write functions to make complex code sequences more simple --
i.e. for abstraction.  Parcel up the behaviour that can conceptually be
considered a single action, and put it away in a function.

This has the valuable benefit that the function can then be re-used in
other places needing the same action; but that's not the reason I write
them to begin with.

> So, I assume it would be better to write functions that are very
> specific, as opposed to those that are more generic. 

I've no idea how "re-use the code" leads you to think of more specific
functions -- surely the trend would be to more *generic* functions, that
can thus be re-used in more places?

> Is this wrong? I know functions were not intended for this, but if I
> don't use them in this manner, I might as well do everything globally,
> and I don't see any difference in the two approaches.

I recommend you get a copy of Code Complete (Steve McConnell, published
by Microsoft Press).  It is an extremely comprehensive and approachable
tome on the actual practice of writing code.

In particular, it explains just about every reason to use abstraction in
data, code and algorithms.  Some you may have heard before, but my
feeling from your message is that a lot of it will be new -- and all of
it is valuable.

-- 
 \        "If you go to a costume party at your boss's house, wouldn't |
  `\     you think a good costume would be to dress up like the boss's |
_o__)                       wife? Trust me, it's not."  -- Jack Handey |
Ben Finney <http://bignose.squidly.org/>



More information about the Python-list mailing list