[Tutor] How (not!) lengthy should functions be?
boB Stepp
robertvstepp at gmail.com
Thu Apr 16 18:47:43 CEST 2015
As I go through my current coding project(s) I find myself breaking
functions down into other functions, especially when I see see
(unnecessarily) duplicated code fragments. I understand this to be
regarded as good practice. However, I am wondering if I am carrying
things too far? For instance I have a collection of functions that do
simple units conversions such as:
def percent2Gy(dose_percent, target_dose_cGy):
"""
Convert a dose given as a percent of target dose into Gy (Gray).
"""
dose_Gy = cGy2Gy((dose_percent / 100.0) * target_dose_cGy)
return dose_Gy
This function calls another units conversion function, cGy2Gy(), in
doing its work. Generally speaking, I have units conversions functions
for every conversion I currently need to do plus some that I am not
using yet because I can easily see the need for them in the future.
My current understanding of function length best practice is that: 1)
Each function should have preferably ONE clearly defined purpose. 2) I
have seen varying recommendations as to number of lines of code per
function, but I have seem multiple recommendations that a function
generally should fit into one screen on one's monitor. Of course, some
people have HUGE monitors! And I assume that any guidance applies
equally well to methods.
Am I on-track or am I getting carried away?
--
boB
More information about the Tutor
mailing list