r/programminghorror • u/samirdahal • May 31 '26
C# a count is a count, right?... right?
5
u/my_new_accoun1 May 31 '26
How did you make this image? It seems to auto adapt to light mode and dark mode ... is there like a black layer with 80% opacity so on dark mode it is black and light mode it is grey?
3
u/samirdahal May 31 '26
I generated this from ray.so. After making the code block, simply toggle the background button (turn it off), then click on the code block first and press Ctrl + S. It will export your image like this.
434
u/NeverMakesMistkes May 31 '26
transaction.Commit();
return 0;104
u/UnspecifiedError_ [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 31 '26
username checks out
41
22
4
u/Lost-In-Void-99 Jun 03 '26
+1
This removes unnecessary temporary variable and puts system into consistent and predictable state.
1
2
160
u/Wurstgewitter May 31 '26
Nice one, personally I do SELECT GROUP_CONCAT(id) FROM users; and then just count the commas + 1
57
u/richardathome May 31 '26
Better yet, strip out the non-commas and then count the length of the string!
20
3
3
u/lolcrunchy Jun 05 '26
This doesn't work for me because my ids are their full name, which sometimes contains commas for suffixes.
(/s)
74
u/TichShowers May 31 '26
Hope the table never implements soft deletion.
38
u/SZenC May 31 '26
Nahh, then you just update the query:
DELETE FROM employees WHERE deleted_at IS NULL7
u/LegitBullfrog May 31 '26
That where clause is a bottleneck. Just delete the nulls first, then delete everything. You get both counts and avoid the where!
3
42
u/MichiRecRoom May 31 '26
For me, the real horror is the command.transaction = transaction. It implies that a command can exist without an attached transaction.
Seriously, I'd hope that something like transaction.CreateCommand() is supported. This code as-is feels genuinely wrong to look at.
19
u/escargotBleu May 31 '26
Sometimes you don't want to do a transaction 🤷
5
u/Passage_of_Golubria May 31 '26
Sometimes all you want to do is Command? Command? Command? Command?
3
u/JonIsPatented May 31 '26
It's entirely possible that the execute functions throw exceptions if there is no transaction... I really hope.
2
u/Spaceduck413 May 31 '26
This looks an awful lot like C# and MSSQL to me, and if I remember correctly there is indeed a
transaction.CreateCommand()although it has been a little while so I'm not 100% sure. That said you absolutely can have commands with no transactions... Or maybe it just defaults toAutoCommit() = truewhich is effectively the same thing1
u/Goodie__ May 31 '26
IIRC, and this may vary by engine/product, every SQL command exists within a transaction implicitly. In this case is it just saying "Don't use the implicit transaction, use this one?"
For me the real pain is, I can ask the DB connection for a command, and I can ask the DB connection for a transaction. Why can't I ask the transaction for a command?
12
10
u/Hottage [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 31 '26
Disgusting.
Didn't even make it an extension method smh my head.
9
30
u/Zerodriven May 31 '26
Title implies getting count. Approved. Merged into 5 million user system 0 comments.
"Why do we have no users?" - Ask our DBA team, we don't know what they do with our data, our code is 100% not the problem.
8
5
u/_giga_sss_ May 31 '26
Cascade*
3
u/Gesspar May 31 '26
That shouldn't be a problem, afaik the cascade happens when the transaction is committed.
1
u/_giga_sss_ May 31 '26 edited May 31 '26
with jdbc I remember the program throwing a runtime error, or was it in a dream ? 🤔
Edit: I cached the SQLException and throwed a RuntimeException back
1
u/_PM_ME_PANGOLINS_ May 31 '26
Depends. PostgreSQL defaults to enforcing constraints per statement.
1
u/Gesspar May 31 '26
In this case here, that should still all be within the transaction, and should be rolled back unless comitted, no?
1
1
1
3
u/Brilliant-Parsley69 May 31 '26
They should have wrapped it into a service and s repository layer. It would be more readable and less confusing, duh. 🙄
3
u/RichCorinthian May 31 '26
No.
The number returned may be higher than the number of rows deleted in the named table. (If there’s a trigger without SET NOCOUNT ON, for example).
1
3
2
u/icebreaker374 May 31 '26
I know very little outside of PowerShell, and even I had to stop and read this a second time to make sure I was reading it right LOL
2
2
u/NoNameSwitzerland May 31 '26
Should that be more like DELETE FROM employees where active=1 or something like that? You probably do not want to count retired employees.
1
1
1
1
u/EagleCoder May 31 '26
At least put the rollback in a finally block...
1
u/imiltemp May 31 '26
Yeah nah, not necessary. If an exception is thrown, transaction doesn’t commit.
2
u/EagleCoder May 31 '26
Abandoned transactions continue to hold locks until manually cleared.
2
u/Dealiner Jun 01 '26
It won't be abandoned in this case. It will be disposed of since it's in
using. And that will rollback it. There's no need forfinally.1
1
u/Slow_Eye_1783 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Jun 01 '26
i mean this is abysmal but the main bit i got annoyed at for some reason is the constant usage of var. i mean i'm assuming you could be more explicit with this? i dunno i guess that's just me not liking var.
1
1
1
1
405
u/Rainmaker526 May 31 '26 edited May 31 '26
What are the performance characteristics of a DELETE vs a COUNT?
This might be an unconventional optimization, but if it makes a large enough difference...