Truely unique file name.

  • Thread starter Thread starter Mufasa
  • Start date Start date
I really have to disagree here. It's the same justification that is
used to permit like race conditions to crop up.

Yes, depending on the code there may be a very small chance that you
will run into the race condition (and I will concede that everyone has
different definitions of "low" and "high", as they are subjective, so
something I think has a low chance you might think that has a high chance,
etc, etc), but in the end, you shouldn't have run across it in the first
place, because you knew it could happen and didn't prevent it.

And that's what this is really about in the end. With a few extra lines
of code, you could write something that will give you a guarantee vs.
playing the odds, no matter how small those odds might be. It's my opinion
that as a discipline, software development is more akin to engineering than
anything else, and in engineering, you simply don't play the odds like that
when you can easily do something of a preventive nature.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

[...]
That's overblown. What the documentation says, literally, in one
sentence, is "such an identifier has a very low probability of being
duplicated." And this is true.

Thank you for admitting your error.
You seem to think I'm making the argument that GUIDs are absolutely
unique with 100% probability. If that's how it appeared, I did make a
mistake, because that was certainly not my intent.
No. This is never true. Writing your code to rely on very low
probabilities of an error is incorrect. Code like that can and will
eventually fail.
OK, now we're at the heart of the argument! I disagree with the
assertion that it's incorrect. I only agree for high values of "low
probability", which are actually the majority. GUIDs are in a
different league.
 
[...]
That's overblown. What the documentation says, literally, in one
sentence, is "such an identifier has a very low probability of being
duplicated." And this is true.
Thank you for admitting your error.
You seem to think I'm making the argument that GUIDs are absolutely
unique with 100% probability. If that's how it appeared, I did make a
mistake, because that was certainly not my intent.
No. This is never true. Writing your code to rely on very low
probabilities of an error is incorrect. Code like that can and will
eventually fail.
OK, now we're at the heart of the argument! I disagree with the
assertion that it's incorrect. I only agree for high values of "low
probability", which are actually the majority. GUIDs are in a
different league.

I just discovered this thread after being offline for a few days due to power
outages - we have been having ice storms in my part of the world - and have
found it fascinating.

Jeroen, you have not had much support here, but FWIW I agree totally with what I
see as having been your point. Because of the statistical nature of Brownian
motion there is a small but non-zero chance that a "vacuum pocket" will form
around my head (meaning all the air molecules in my vicinity will just happen to
take a random walk to some other part of the room) and I will die of lack of
oxygen as this pocket follows me around the room in my own desparate random walk
to find some air to breathe. Yet somehow I comfortably ignore this possibility
of doom and do not in fact at this moment happen to be carrying a backup O2
supply "just in case". I suppose this is because that at some intuitive level I
reckon the mean expected time one would have to wait for such an unlikely event
far, far exceeds the expected remaining life of the universe. So I judge it
"not worth worrying about".

-rick-
 
Rick Lones said:
I just discovered this thread after being offline for a few days due
to power outages - we have been having ice storms in my part of the
world - and have found it fascinating.

Jeroen, you have not had much support here, but FWIW I agree totally
with what I see as having been your point. Because of the
statistical nature of Brownian motion there is a small but non-zero
chance that a "vacuum pocket" will form around my head (meaning all
the air molecules in my vicinity will just happen to take a random
walk to some other part of the room) and I will die of lack of oxygen
as this pocket follows me around the room in my own desparate random
walk to find some air to breathe. Yet somehow I comfortably ignore
this possibility of doom and do not in fact at this moment happen to
be carrying a backup O2 supply "just in case". I suppose this is
because that at some intuitive level I reckon the mean expected time
one would have to wait for such an unlikely event far, far exceeds
the expected remaining life of the universe. So I judge it "not
worth worrying about".

Or as the guys doing the guids like to say, if you're more likely to
have a bit failure (bit fails to set or value gets switched by cosmic
ray just happening to hit the right spot) than have a natural
collision, then there's no much point in worrying about it.

OTOH, Peter's point about guaranteeing your results is a good one.
Part of our job as programmers is to envision where and how things
might fail to happen as desired, and deal with the failure. If you see
a potential for failure (no matter how unlikely) and ignore it, you
deserve to be bit by it.
 
Simple solution to all of this:

string FileName = string.Empty;

while (FileName == string.Empty || File.Exists(FileName))
{
FileName = Path.Combine(yourPath,
Guid.NewGuid().ToString());
}


If you are SUPER concerred do something crazy like:

string FileName = string.Empty;
int iCount = 0;

while (FileName == string.Empty || File.Exists(FileName))
{
if (iCount > 999)
{
FileName = Path.Combine(yourpath, new
Random(Environment.TickCount).Next(int.MaxValue));
}
else
{
FileName = Path.Combine(yourPath, Guid.NewGuid().ToString());
iCount++;
}
}

0 odds of the same file name.
 
Back
Top