• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple recursion  RSS feed

 
Brian Percival
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • 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
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Tim Holloway
Bartender
Posts: 18663
71
Android Eclipse IDE 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
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!