• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursiveness: class variables or method variables  RSS feed

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that has one method that recursively visits hundreds of files and folders. Is it better to declare my several String variables for reuse at the class level or the method level ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want all instances of the class to share the same variables? Does that make sense in this scenario?
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will only be one instance of the class. Declaring them hundreds of times at method level whilst interating ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is better to do what makes sense for the problem you are trying to solve. The answer to a question like this would depend on how exactly these variable are being used, which we can't answer.

You'd do best to StopCoding (<--- click that link and read the page) and think through the problem. Sketch it out on paper. Then the answer should be obvious what is the right way to do it.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It runs very quickly, but is it more effective than having the same variables at the class level in this case ? The method iterates several hundred times and does some String stuff along the way. Those variables at the top of the method are declared hundreds of times obviously. If declared once at class level, they are reused anyway ? The class itself is instantiated within a method so all is supposed to be disposed of through memory management thereafter and not hang around for the life of the program. ? Sorry if I seem daft ...



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isaac Hewitt wrote:It runs very quickly, but is it more effective than having the same variables at the class level in this case ? The method iterates several hundred times and does some String stuff along the way.

In general, static (class) variables are a bad thing, and you should avoid them if you possibly can.

Those variables at the top of the method are declared hundreds of times obviously. If declared once at class level, they are reused anyway ?

Actually, both statements are almost certainly wrong. But even if they're not, obsessing about things like this is micro-management of the worst kind. Allocating objects is precisely what Java was designed to do, and my clunky 7-year old Dell can bang out anywhere between 10 and 20 million objects a second, so I suspect you'd be hard pressed to even measure the time difference over a few hundred declarations.

Furthermore, the directory traversal itself is likely to be thousands of times slower than allocating objects is, because it involves hard disk access.

Concentrate on writing good, clear code; and don't worry about speed until you know that you have a problem - and furthermore can prove that your change will make all the difference.

And just on a general note: directory traversal is a classic application for recursive logic. The only thing you may need to be careful about is "endless" links (ie, a link to a portion of the tree that you've already processed). One way to get around that is to simply ignore links of any kind, but there are other, less drastic, alternatives.

HIH

Winston
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like long methods like these, and especially declaring variables at the top of the method. My man problem in giving you a correct answer is that looking at the code it's hard to stay what the scope f the variables is. For example, you are using many of the variables inside the for loop.. Why didn't you just declare it inside the for loop. Better yet, put the cde inside the for loop into logical functions so your code becomes self documented

Second, where the variables are declared does jack squat for performance. What affects performance is how many objects are allocated/garbage collected. Declare and name your variables to make your code easier to read. Make sure that the scope of a variable matches the actual scope of usage. It makes the code more beautiful.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is very hard to understand because you are using short, cryptic names for methods and variables: irt, fo, br, af, l, p2, p, n, x, h, mth, CU, f2, r_L, t_L, p3, FCC, t_L_2, rcl, getBR, sc_L, tm, ...

If you use descriptive names, it will make your code much easier to understand and maintain. How long the names of your variables are does not matter at all for performance.

Also, "hundreds of times" is nothing for a computer, as Winston and Jayesh have already said. Computers execute many millions of instructions per second. It is much more important to write clear, understandable code than trying to optimize code for speed. And optimizing based on vague feelings is not very effective. Only optimize when you actually have a performance problem, and measure (using a profiler) to find out what actually causes the performance problem, rather than making presumptions about what makes the program run slow.
 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston Gutkowski, Jayesh A Lalwani, fred rosenberger, and Jesper de Jong for your responses - all very thought provoking. I will see if I can apply these thoughts to the method at hand. Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!