Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

simple recursion

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is what I have to get th directory size. Is there any obvious no-no in what I have? I want someone to confirm that recursion of a static method is ok.
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,
Recursion of a static method is fine.
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. thanks a bunch. I wanted someone to validate what I wrote.
 
Rancher
Posts: 3572
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the other hand, I do think there's a problem here:

If there's a problem, this gives you no information about what the problem is. You might not know that there was a problem. Yes, you might check the return value to see if it's less than zero (if you remember to check that). But what if you have a directory containing two other directories, where the first has files with total size 12345, and the second is unreadable because you don't have proper permission to read it. Your method will report the total size of the parent directory as 12344 (that is, 12345 + -1). How would you know that this is not a correct answer?

I think it would be better if an exception is thrown here, just let it be thrown from getSize() as well. Whoever calls this method can deal with it however they want.
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

whenever a child directory returns a -1, the parent directory returns a -1 also. in what you said, the parent directory will not return 12345 + -1, it will just return -1. Please confirm.

Brian
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brian Percival wrote:Mike,

whenever a child directory returns a -1, the parent directory returns a -1 also. in what you said, the parent directory will not return 12345 + -1, it will just return -1. Please confirm.

Brian



It looks fine. The negative status does look like it gets passed up, and not merge with the file size total.

However, a "-1" doesn't give any detail on the file that caused the exception, or the exception itself.

Henry
 
Saloon Keeper
Posts: 22289
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possible way to get the "-1" is if you're working with a volatile set of directories. For example, if the file gets deleted between the time you do the listFiles() and the time you do getSize();

Yes it's not going to happen very often, but some of us actually get paid extra to worry about stuff like that.
 
Brian Percival
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim, thanks for taking the time.

I understand it is not foolproof, my intention was to return a -1 even if anything slightly odd happens (like file deletion after acquiring folder listing). This function is a very small piece of a bigger jigsaw where the details of why it failed are not important. if it fails for some reason, user will request it again. (user does not need to know the error of why he couldn't get the directory size).

Put it simply, ignoring the exceptions was intentional
 
Henry Wong
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:One possible way to get the "-1" is if you're working with a volatile set of directories. For example, if the file gets deleted between the time you do the listFiles() and the time you do getSize();

Yes it's not going to happen very often, but some of us actually get paid extra to worry about stuff like that.



Another possibility. If you try to access a directory that you don't have access to. Example: in Windows, there is a system directory on the C:\ directory. With listFiles(). you should get a null entry for that directory, which will get you a NPE, which will get you a -1. And this will happen every time.

Henry
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic