Hot answers tagged

274 votes
Accepted

Are bad programming practices typical within the software industry?

The principles that you cited in your question are just that... principles. They are not mandates, laws or orders. While the people who came up with these principles are very smart, they are not ...
160 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

I have a bag with five potatoes in it. Are there .Any() potatoes in the bag? "Yes," you say. <= true I take all of the potatoes out and eat them. Are there .Any() potatoes in the bag? "No," you ...
  • 3,006
96 votes
Accepted

In C#, why are variables declared inside a try block limited in scope?

What if your code was: try { MethodThatMightThrow(); var firstVariable = 1; } catch {} try { var secondVariable = firstVariable; } catch {} Now you'd be trying to use an undeclared ...
  • 6,803
94 votes
Accepted

How to deal with fear of taking dependencies

... We are forced to stay on the lowest API level of the framework (.NET Standard) … This to me highlights the fact that, not only are you potentially restricting yourselves too much, you may also be ...
  • 38.5k
81 votes

Is my usage of explicit casting operator reasonable or a bad hack?

It is... Not great. I've worked with code that did this clever trick and it led to confusion. After all, you would expect to be able to just assign the BigObject into a SmallObject variable if the ...
  • 108k
67 votes

In C#, why are variables declared inside a try block limited in scope?

I know that this has been well answered by Ben, but I wanted to address the consistency POV that was conveniently pushed aside. Assuming that try/catch blocks didn't affect scope, then you would end ...
  • 1,989
65 votes
Accepted

Inverting an IF statement

It depends. In your example having the non-inverted if statement is not a problem, because the body of the if is very short. However you usually want to invert the statements when the bodies grow. We ...
  • 10k
53 votes

Is it unreasonable to expect Any() *not* to throw a null reference exception?

First off, it appears that that source code will throw ArgumentNullException, not NullReferenceException. Having said that, in many cases you already know that your collection is not null, because ...
51 votes

Are bad programming practices typical within the software industry?

It is not uncommon. One thing to realize is that the software industry is incredibly diverse. Some companies are cutting edge. Leading universities and innovative software companies (even some labs in ...
51 votes
Accepted

What advantage was gained by implementing LINQ in a way that does not cache the results?

What advantage was gained by implementing LINQ in a way that does not cache the results? Caching the results would simply not work for everybody. As long as you have tiny amounts of data, great. Good ...
  • 7,134
51 votes

How to deal with fear of taking dependencies

We are forced to stay on the lowest API level of the framework (.net standard). The reasoning behind this is that a new platform could one day arrive that only supports that very low API level. The ...
  • 1,747
47 votes
Accepted

How to treat unhandled exceptions? (Terminate the application vs. Keep it alive)

You have to expect your program to terminate for more reasons than just an unhandled exception anyway, like a power failure, or a different background process which crashes the whole system. Therefore ...
  • 190k
44 votes

Is it worth even checking to see if Guid.NewGuid() is Guid.Empty?

If you find Guid.NewGuid() == Guid.Empty you have won the hardest lottery on earth. Don't bother with any uniqueness or collision checking. Not having to do that is what guids are for. I will spare ...
  • 2,684
44 votes
Accepted

What is the benefit to having pure POCO models?

Your two questions are unrelated. What is the benefit to having pure POCO models? A pure POCO is not dependent on some enterprisy framework, convention, [] thingy, or intimately connected to some ...
42 votes
Accepted

Why use the discard variable in C#?

It matters for two reasons. One is conventional, the other technical. The conventional reason is that _ conveys active disinterest in the returned value. Sure, you could write var dontcare instead, ...
  • 39.4k
39 votes
Accepted

Why do we need the async keyword?

There are several answers here, and all of them talk about what async methods do, but none of them answer the question, which is why async is needed as a keyword that goes in the function declaration. ...
39 votes

Refactoring constructor that has too many parameters

Here's a heretical answer: I see nothing inherently wrong with have lots of parameters. Having a function that takes lots of parameters is often a target for refactoring, but just because we've ...
  • 3,803
36 votes
Accepted

What is the purpose of Code analysis and when do I need to use it?

What is Code analysis? Code analysis (previously FxCop) is a static analysis tool which searches for common patterns which may indicate that something is wrong in the source code. For example, if an ...
34 votes
Accepted

Is it worth even checking to see if Guid.NewGuid() is Guid.Empty?

I would suggest it's not worth checking for Guid.Empty. The docs for Guid.NewGuid for some reason mention that The chance that the value of the new Guid will be all zeros or equal to any other Guid ...
34 votes
Accepted

What is the reasoning behind naming of the .NETs Select (Map) and Aggregate (Reduce)?

This mostly comes down to the history of LINQ. LINQ was originally intended to be SQL-like, and used (largely, though not exclusively) to connect to SQL databases. This leads to much of its ...
  • 44.1k
34 votes

Inverting an IF statement

Don't not avoid negatives. Humans suck at parsing them even when the CPU loves them. However, respect idioms. We humans are creatures of habit so we prefer well worn paths to direct paths full of ...
32 votes

Why use the discard variable in C#?

By using the discard _, you are making it explicit that you, as the developer, understand that the method is returning a value but that you do not care about the value. This gives some insight into ...
  • 77.9k
31 votes
Accepted

Why would you ever 'await' a method, and then immediately interrogate its return value?

As I understand it, asking for the Task's value in this manner will block code execution until the value from the awaited method is returned, effectively making this a synchronous call. Not quite. ...
  • 108k
30 votes
Accepted

What is the "right" way to implement DI in .NET?

Don't think yet about the tool that you are going to use. You can do DI without an IoC Container. First point: Mark Seemann has a very good book about DI in .Net Second: composition root. Make sure ...
30 votes

C# 8 non-nullable references and the Try pattern

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics....
30 votes

How to treat unhandled exceptions? (Terminate the application vs. Keep it alive)

It depends to some extent on the application you're developing but in general, I'd say that if your application encounters an unhandled exception, you need to terminate it. Why? Because you can no ...
  • 1,075
29 votes

Best way to define, start and stop threads in C#

I am making a little program, which have few Threads, constantly running. At some point, I may want to stop one of them, and then, after random period of time, to start it again. What is the best way ...
  • 45.4k
27 votes

Am I wrong in thinking that needing something like AutoMapper is an indication of poor design?

In my experience, when someone has complained about 'too much boilerplate' and wants to use AutoMapper, it's been one of the following: They find it irritating to write the same code over and over. ...
27 votes

When and why to use Nested Classes?

Typically, a nested class N is created inside of a class C whenever C needs to use something internally which should never be (directly) used outside of C, and for whatever reason that something needs ...
  • 27.3k

Only top scored, non community-wiki answers of a minimum length are eligible