patternjavaMinor
Is this recursion a bad idea and does it make for clean code?
Viewed 0 times
thisidearecursionmakebadcodefordoesandclean
Problem
This is a program I made to bounce the letter a across the screen. I made a few others, but this one I made with a recursive
Is my code clean (probably far from it)? Easy to read? Am I overdoing the comments, or not specifing the code well enough?
Is it a bad idea using a recursive method for this as it has to keep memory for 80 recursions? It seemed like a good idea, because I was condensing so much more code into a smaller space.
Also, is there a way to test for efficiency? That would help greatly.
bounce() method. I know main() is a bit empty, but I intend to make an input menu that main refrences, so it seemed better to put the code in another method. Is my code clean (probably far from it)? Easy to read? Am I overdoing the comments, or not specifing the code well enough?
Is it a bad idea using a recursive method for this as it has to keep memory for 80 recursions? It seemed like a good idea, because I was condensing so much more code into a smaller space.
/**
* This code is designed for use with CMD with an 80
* ASCII character width space.
* It adds and subtracts a space to a string and out prints
* the line.
* It uses recursion to create one full Right-Left cycle,
* then uses an infinite while loop to repeat.
*/
class Wave8 {
static String spaces = ""; // Holds the spaces
static boolean direction = true; // True is right, false is left
// Bouncing loop
public static void bounce() {
if (spaces.length() == (80 - 1)) { // Width minus the string after lead
System.out.print(spaces + "a");
direction = false; // Change direction
}
else
System.out.println(spaces + "a");
if (direction) { // If going right
spaces += " ";
bounce(); // Recursion
}
if (!direction) { // If going left
spaces = spaces.substring(1);
if (spaces.isEmpty()) {
direction = true;
return;
}
else System.out.println(spaces + "a"); // Normal routine
}
}
public static void main(String args[]) {
while (true) {
bounce();
}
}
}Also, is there a way to test for efficiency? That would help greatly.
Solution
Before explaining why I think recursion might be a bad idea here, let me show you the solution I came up with.
The advantage of this solution against the recursive one :
-
clarity : one can clearly see what was intended.
-
termination : there is no doubt on the fact that this solution will eventually return
-
no use of object members : if I call
-
performance : I'd expect this solution to be much faster.
On the other hand, many functions are much better when written in a recursive way. This is the case when it's easy to see that we call the function on a "smaller" problem.
class Wave8 {
public static void bounce() {
String spaces = "";
for (int i=0; i<80; i++, spaces += " ")
{
System.out.println(spaces + "a");
}
for (int i=0; i<80; i++, spaces = spaces.substring(1))
{
System.out.println(spaces + "a");
}
}
public static void main(String args[]) {
bounce();
}
}The advantage of this solution against the recursive one :
-
clarity : one can clearly see what was intended.
-
termination : there is no doubt on the fact that this solution will eventually return
-
no use of object members : if I call
bounce() twice, I expect it to behave the same way twice.-
performance : I'd expect this solution to be much faster.
On the other hand, many functions are much better when written in a recursive way. This is the case when it's easy to see that we call the function on a "smaller" problem.
Code Snippets
class Wave8 {
public static void bounce() {
String spaces = "";
for (int i=0; i<80; i++, spaces += " ")
{
System.out.println(spaces + "a");
}
for (int i=0; i<80; i++, spaces = spaces.substring(1))
{
System.out.println(spaces + "a");
}
}
public static void main(String args[]) {
bounce();
}
}Context
StackExchange Code Review Q#31392, answer score: 7
Revisions (0)
No revisions yet.