Choose the right name
I was scared when I read the name of your function recurse()
because recently I have been cursed, and that function's name threatens to re-curse me. That is why you must choose the name that says why your function actually does.
No empty lines before return
I see frequently Python code on this website where the authors leave an empty line before the return
statement. I do not see the mystery behind this practice on PEP8.
Natural way to be recursive
The natural way to design a recursive function is to write the exit condition first, not at the end. So you could re-write your function like this:
def recurse(head, length, node):
if length in {0, 1} return True # write the exit condition first
# write the rest of code here
Never hire a developer who computes the factorial using Recursion
In software engineering, we have this saying "Never hire a developer who computes the factorial using Recursion". That is why I do not want to suggest an improved recursive version of your function. Bex' answer mentioned this by the end, but I am just emphasizing it:
def better_function_name(head, length):
while (length > 0):
head = head.next
length /= 2
return head
Too much parameters
Parameters belong to a level of abstraction which is different from that of the function they are passed to. That is why we must use as less parameters as possible with functions (and classes initializers). That is why I want to render your function monadic.
Bex explained you why you do not really need the node
parameter. So we let us see if we can get rid of length
and head
parameters.
I think by head
(and node
), you refer to a data structure you are using in other places of your program. The most obvious way
to do fulfill this goal is by using declaring it global:
def pick_a_better_function_name(length):
global head
while (length > 0):
head = head.next
length /= 2
return head
But it is commonly advised not to use global variables whatever the programming language you are coding with because they can result into, among other ugly things, spaghetti code. An alternative to use a monadic (one parameter) function and avoid global is to use a class where you store all your global variables:
Class MyGlobalVariables:
head = ...
and our function becomes:
def pick_a_better_function_name(length):
head = MyGlobalVariables.head
while (length > 0):
head = head.next
length /= 2
return head
Do not use function's parameters directly
Whatever the programming language you use, do not use the parameters inside your function:
def my_function(param):
par = param
# Now work with "par" instead of "param"
So the improved approach of your function would be:
def pick_a_better_function_name(length):
l = length
head = MyGlobalVariables.head
while (l > 0):
head = head.next
l /= 2
return head
EDIT:
The fact the question is closed does not encourage me to elaborate more about what the commentators pointed on. However, that is something you can read on Do not make a direct use of function parameters.
True
). Could you use your return value (and therefore the stack) to convey some part of your program data? If so, this construction would probably collapse to just a few lines. \$\endgroup\$