- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Contains any first comment
Admin
Frist!
Admin
Admin
The monstrosity is a LINQ-for-SQL generated statement you could find profiling your DB while running the application. It's generated at runtime (not at compile time). I'm not saying that's an excuse for anything, but I don't expect something generated at runtime to be pretty or concise as long as it's efficient... Which it's not in that case, I'll admit.
Also, the whole method can we written as:
It's more readable, but it would probably not change anything to the simple generated SQL code, although there's probably more to it since it really looks like Actions is a LINQ IEnumerable, because I don't see where Redacted2-5 come from in the generated SQL statement.
Admin
Also you should not return IList from a read operation on the DB. ILists are for adding stuff to them, which you don't do there. And there's no reason to do ToList() here. Enumerate when you need to (whichever method calls this should have that responsibility).
Admin
I'm not buying it... Gordon Freeman is a theoretical physicist, not a theoretical SQL-icist.
Admin
TRWTF is LINQ ... while very expressive, you need to inspect the generated SQL anyway to avoid having your code end up as the featured WTF of the day.
Either that, or be prepared to do the SQL generation in your head, which defeats the point of LINQ
Admin
Still not as bad as The White Flag.
Admin
I guess we have an answer to "How can Erik Gern's stories possibly get any worse?"
Captcha: venio. I wish he would pick a different venio for his "creative" "writing"
Admin
Admin
Admin
It's the HEV suit - who knows when you're going to end up in a room full of programmers and managers a couple of hours before release and the damn thing doesn't work. I'd classify this as a hazardous environment.
Also, it seems that being a programmer in Black Mesa in general would be hazardous - management might have wanted to just dispense the suits to everybody and since it's already doing it, why not include just a bunch of features for all of the staff. Hey, it's government money - if they built the crate smashing room, why not give the janitors a suit with built in developer tools, ammo count and morphine.
Admin
I can't imagine it would generally make much difference in terms of performance, but I have seen an otherwise competent programmer accidentally nuke an entire table because he deleted from a left outer join and stuffed all his conditions in the join predicates rather than separating them between the join and a WHERE clause.
Admin
They are for most RDBMS, such as Oracle, SQL Server, Sybase, etc. About 15 - 20 years ago, the query optimizers that supported the ON clause would give you better plans if you did your join there, due to precedence (JOINs done first, then WHERE clause filtering/joining next, then aggregation, then ordering.)
But since then, all the big players have been good at picking out join columns in the WHERE clause and using that to construct decent query plans. Complex outer joins sometimes yield slightly different results, but that's a narrow edge case to be handled only when it comes up.
Admin
But since then, all the big players have been good at picking out join columns in the WHERE clause and using that to construct decent query plans. Complex outer joins sometimes yield slightly different results, but that's a narrow edge case to be handled only when it comes up.
Admin
They're treated absolutely identically.
SQL Server's first phase of optimization is simplification. It rewrites the query tree to a simpler form. It includes converting subqueries to joins, removing redundant joins, pushing filters from WHERE clauses down to the relevant tables (known as predicate pushdown) and removing contradictions.
The two versions of the query should produce exactly the same execution plan. The JOIN .. ON version is preferred because it's clearer.
For more information see Inside the SQL Server Query Optimizer by Benjamin Nevarez.
Admin
This is why you shouldn't just blindly throw LINQ about without thinking about what it's actually doing.
I see it on Stack Overflow all the time, people who think that LINQ is magic and will do anything to avoid a simple foreach loop.
Admin
I'll go back to pushing buttons and opening windows to alien worlds then.
Admin
Not true at all for left and right joins, at least.
Admin
Correct. Putting the condition in the ON clause for the nullable side of the join results in the table being filtered before the join. Putting the condition in the WHERE clause for the nullable side of the join results in an implicit INNER JOIN.
So this:
Will have a result set identical to this:
To make the first one work like an OUTER JOIN, you must handle your nulls:
Or alternately, use the ON clause for conditionals that should be applied before the join takes place:
Admin
Admin
Are you talking about LINQ to SQL or LINQ to objects? In LINQ to objects, this is irrelevant because there's no generated SQL for it to be "actually doing", and in LINQ to SQL this is irrelevant because you can't do the same thing with a foreach loop.
In LINQ to objects, people often prefer LINQ to writing out loops because it's more concise and- for most tasks which aren't too complex and are just about querying data- more readable. LINQ to SQL is completely different matter and you have to be much more careful.
Admin
TRWTF is using runtime-generated SQL for anything other than CRUD. Anything that try to generate complex SQL set operations from object declaration will end up a monstruosity.
The reason is simple : in objet you think in term of procedural operations, in SQL you have to think in term of dataset and set operations
Admin
Yes, LINQ to Objects is basically just standard functional operations ported to an object-oriented context. Once you understand the basic set of functions available to you, it does a fantastic job at declaring intent and separating your iteration strategy from your operations. It's no magic bullet, and you still need to be aware of what the code is doing, but a well-written set of LINQ transformations can greatly improve the readability of your code in the long run.
(Yes, I have a thing for LINQ. I've also written some code with it that would definitely be considered DailyWTF-worthy. Many-layered nested selects.)
Admin
As long as the statement is one consistent idea, I don't mind the nested selects.
However, when you try to accomplish two tasks with one line of code, it gets questionable. Assignment within an if is about as far as I'll go (if((x=getvalue())<0) ... but even that is unnecessary and doesn't give a real benefit.
More than two tasks and that's just evil.
And don't use a select as a foreach. select(x => { x.Add(2); return 0});
Admin
@foreach: yep, void ain't a type.
And did I neglect to mention all the ternary operators? Seriously, nullable types are the bane of this type of pattern, so '== null ? null :' everywhere.
Admin
Reminds me of the "use jQuery for addition" article.
"LINQ all the things!!"
Admin
This code looks like the result of a horrible resonance cascade, though, which Gordon Freeman is quite knowledgeable in.
Admin
Actually, it looks like someone was having fun with portals and unions.
Admin
update eros set mad_level = 'so mad' where butt = 'hurt'
Admin
Actually, I think it's true in both cases that you need to know what you are actually doing. Even with Linq to object. For example, I've seen code along the lines of:
This will, of course, iterate the collection twice when it would be more efficient to just do it once.
And yes, I love Linq, but it isn't a panacea, and it isn't always the right tool for the job.
Admin
I've always thought it stupid that people want to configure their text editors to line wrap at 80 characters. Sure, I get the historical implications of old CRTs that only supported 80 characters of legible text, but that's a dated concept. Everybody's screen is wider now, and there are plenty of reasons for lines to be that long.
So, I hereby propose a new standard: A single line of code should not be long enough to exceed the width of a DailyWTF article.
Discuss.
Admin
Definitely one the nastier issues with using LinqToSQL... I find it helpful for the basics when you have a properly designed system with all your constraints in place.
For complex queries, it tends to be easier to just go the route of a complex type, hook it up to a function (MS terminology for a procedure in the entity framework) and call it good.
Easy to understand the sql, easy to understand where the result is going.
Admin
Including or excluding indentation?
Admin
And here we see why it's never a good idea to use techniques you learn from Jenga to program anything. Was they going for a record or was appending and loops out of the question?
Admin
facepalm
Admin
It Doesn't matter... He was going for pure maintainability. There is a sort of elegance to it all. The way it structured, the readability of it all.
Admin
Aggregate to tuple for best results!
Admin
Admin
Admin
Admin
Granted, if the caller is going to do futher filtering on it, you may wnat to return an IQueryable, but that also means the caller needs to be careful because not all methods that are acceptable on IQueryable in general my be able to be used with your particular LINQ provider.
Forcing it to a List makes it usable by anything and any further filtering can be handled by LINQ to objects without any worrying of over complicating the query to the DB/underlying LINQ provider.
Admin
Admin
The real WTF is that the DBAs or developers didn't blame the network for poor performance. In my experience that's what usually happens in cases like this.
Admin
Random access to a list won't cause a fire, but still, WTF?
Admin
This is why you avoid Linq-To-Sql like the bubonic plague crossed with ebola. It's handy for rapid prototyping but for production systems that rely on performance you end up rewriting the underlying SQL most of the time anyway.
Admin
But...but...but...it's LINQ! It's wonderful! It's inviolate! It's...a non-standard way to access a database which is unintelligible to all but the anointed! It's PERFECT!!!
Admin
I've come across so many hand-'optimized' stored procedures that were completely blown away and left in the dust performance-wise by LINQ-to-Entities or LINQ-to-SQL generated queries that I'm going to take this remark with a few pounds of salt.
Admin
Well I also came across a few exceptions to that scenario, so there!
Admin
Ah, I've come across this one many, many times as well. It looks like Entity Framework to me though, not LINQ to SQL.
That generated SQL is NOTHING compared to the hell of querying a base table in a large Table-Per-Type hierarchy in Entity Framework.