Do you know a tool that automatically refactors a method with a single loop into a recursive method, preferably in Java?
This is for teaching purposes.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think such a tool exists, since usually refactoring aims at increasing performance, not decreasing it (which is the case when using recursive methods instead of loops). If it is for teaching purposes, why not make the students create the tool that would do that? This way, they could learn at the same time recursion and parsing.
I don’t know if the recursification can be automated, but here’s what the transformation should look like. Let’s take a generic for loop, in pseudo-code, for the sake of demonstration:
The loop is composed of four parts:
initialization, which initialize the context (usually variables);test, which is a boolean expression that checks wheter or not the loop is finished;counting_exp, which is a statement that is performed after each iteration; and finally,loop_body, that represents the operations that are executed at each iteration.A recursive version of this method should be decomposed in two parts: one for initialization, and the other one to actually perform the loop:
I didn’t think about the problem enough to be 100% sure that this would work in all cases, but for simple loops this should be correct. Of course, this transformation can easily be adapted to other types of loops (while, do while).