|
I was in a training course last week where the below code was used as part of an example. It was described as a method that would generate a unique ID and add the specified prefix to it.
public static String generateId(String prefix)
{
int randomNumber = new Random().nextInt();
String id = Integer.toString(randomNumber);
if(id.startsWith("-"))
{
id = id.replace('-', '3');
id = prefix + id;
}
return id;
}
Apart from the flaws that I'm sure you'll spot, I should point out that this was in the context of server-side code that forms part of a workflow. So depending on how often the workflow executes, it could be likely that multiple instances of the workflow would execute at the same time and generate the same "unique" ID.
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
|
|
|
|
|
BotCar wrote: ... multiple instances of the workflow would execute at the same time and generate the same "unique" ID. It's worse than that. Many implementations of Random() use a fixed seed by default, so the first value returned is effectively constant.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
That's interesting. I did not realize that.
Do you mean that some languages' implementations of pseudorandom number generators uses fixed seeds, or that some implementations of the Java Virtual Machine uses fixed seeds by default?
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
|
|
|
|
|
Fortunately .NET does it with changing seed...
public Random() : this(Environment.TickCount)
{
}
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Yep. I'm not sure how fortunate that is, though, because it means you don't trip over this problem until your server is running at high load and you instantiate two requests at the same time. That's really really hard to replicate in a test environment.
|
|
|
|
|
This does not generate UNIQUE ids, only RANDOM ids. There is nothing preventing the generation of an id that has been previously generated.
If you want unique ids, use Guids.
String id = Guid.NewGuid().ToString();
|
|
|
|
|
So I have been working on a replicating database.
Trigger on one of the tables being replicated, used to populate the Data Warehouse.
So, trigger writes record to 'Ids that need to be updated in the DW' table and calls SP that reads through that table for all unprocessed records, and processes them.
Because some related data may not (yet) have been replicated, this SP can legitimately fail to update the DW, and so shouldn't update the 'Ids that need to be updated in the DW' table to set it as processed.
I"d been having a few problems with various bits and bobs, and had created a table, MaxxxsTemporaryTable, just used for logging stuff.
So here was my code
...
Insert into DW.SomeTable
Select col, othercol, morecols
From ReplicatedDatabaseTable join AllSortsOfOtherTables
Insert into MaxxxsTemporaryTable Select @Id, @OtherId, @@ROWCOUNT
IF @@RowCount > 0
BEGIN
Update 'Ids that need to be updated in the DW' table
Set IsProcessed = 1
Where Id = @Id
END
But, for some reason, the IsProcessed flag was always being set, even though I knew it was zero, because that was what was written in my temp table!!
I had to post about it to bluff my shame!
PooperPig - Coming Soon
|
|
|
|
|
I don't know anything about SQL, so what is the goof?
What do you get when you cross a joke with a rhetorical question?
|
|
|
|
|
@@ROWCOUNT is a 'system variable' that tells you how many rows were affected by the last statement.
So my Insert into the temporary table, changed it from whatever value it had (zero) , to 1 - so it was always > 0 so always set IsProcessed
PooperPig - Coming Soon
|
|
|
|
|
Ah. I see.
What do you get when you cross a joke with a rhetorical question?
|
|
|
|
|
Gosh, how exciting!
I just have to tell you all about it!
"The whole idea that carbon dioxide is the main cause of the recent global warming is based on a guess that was proved false by empirical evidence during the 1990s." climate-models-go-cold
|
|
|
|
|
Don't be so shy - tell us what did you do with!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
He turned it into poo.
Don't comment your code - it was hard to write, it should be hard to read!
|
|
|
|
|
I have had a new laptop for some three months now.
The FN Keys are reversed,set to media rather than legacy, and I keep meaning to go into the bios and switch them.
Woo Hoo!
Only three months later and I have just done it!
It is not laziness, honest guv, it is just a severe case of can't be arsed.
---------------------------------
Obscurum per obscurius.
Ad astra per alas porci.
Quidquid latine dictum sit, altum videtur .
|
|
|
|
|
Thank gawd you don't work for the emergency services!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Dalek Dave wrote: Only three months later You are definitely prepared for public office...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
No, no - he actually did something.
Something actually useful as well!
For himself, and he's the only one who benefits admittedly, but...
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Sounds like management to me.
|
|
|
|
|
Hello Tosh. Got a Toshiba?
My Toshiba had that and it took me a week to think - "humm, can I change this?" - then Mr Google showed me the way.
|
|
|
|
|
I hope Toshiba paid Alexei Sayle for ripping-off his song[^] in their advert[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I might do the same thing for our Toshiba, but my wife went through two Toshiba laptops which didn't last more than one month each, so now I may, emphasize may, do something about it in my lifetime.
Now THAT'S politics
"Seize the day" - Horace
"It's not what he doesn't know that scares me; it's what he knows for sure that just ain't so!" - Will Rogers, said by him about Herbert Hoover
|
|
|
|
|
Dalek Dave wrote: Only three months later and I have just done it! What a big mistake. Now you set expectations on your performance far to high.
Learn from Wally: http://dilbert.com/2014-06-09/[^]
|
|
|
|
|
if (e.Message.StartsWith("Case we don't care about"))
{
}
else
{
errorToDisplay += "\n" + e.Message;
}
8 years ago I apparently thought I was being clever by having an empty then block and only having actual code in the else branch.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
Dan Neely wrote: 8 years ago I apparently thought I was being clever by having an empty then block and only having actual code in the else branch.
Well, in Ruby, you could write:
unless (e.Message.StartsWith(.....
which I find even more baffling linguistically. I always have to mentally change that to "if not..."
Or worse (because the conditional comes at the end, and in one case I never saw the conditional because it was off-screen on a very long line), but better linguistically:
errorToDisplay += "\n" + e.Message unless e.Message.StartsWith("Case we don't care about")
Why do I bring this up? Because idiomitic Ruby frowns on negative conditions, like "if !".
Personally, there is a lot of idiomatic Ruby that I refuse to use because it just seems idiotic, not idiomatic.
Marc
|
|
|
|
|
I'm glad none of the Ruby on the project I'm working on uses that wtftern...
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|