Is it considered bad practice to throw
NotImplementedException for code you haven't written yet? Possibly TODO comments would be considered safer?
NotImplementedException is actually a good practice.
Indeed, if you forget to implement a method, and you use it later on in your project (and believe me, it happens), you might spend a long time debugging looking for what went wrong step by step. If you have the exception, the program will stop directly, prompting the exception (if you catch the exception, you'll find it quickly by looking what exception you caught).
I would recommend using the
NotImplementedException combined with TODO comments, that way you combine GUI help (with the tasks in VS) and program safety.
For the release version, it's even more important in my opinion, as in most cases you would prefer your program to crash rather than having a program apparently working properly but producing erroneous results.
It depends on your general philosophy around errors and error handling. I am the "hard error" type of guy: I will throw an exception at the slightest hint that something might be wrong; I will assert everything; If there is an error, if something was expected to be there, and it is not, or if something is there, and it shouldn't, the entire universe must stop. The windows exclamation sound must ring ominously through the loudspeakers.
There are other people who would rather not be bothered with errors. So what if we ship it to the client and the entire reports module is missing because we forgot to code it, and nobody in testing realized it, because the application was all too silent about it? Better do nothing, than throw an exception in the client's face!
I'd say it's a good idea. Usually I see those exceptions thrown by skeleton code that is auto-generated from a form or diagram or something. The exception reminds me to implement the code, and it makes sure that there will be errors if I try to use the functionality that was set up but never fully implemented. Sometimes I will stub it out or replace it with something that is less likely to halt execution (such as printing a warning to the console), but I find that it works for me.
If you're building a library that others will be using having this exception is better than the alternative, which would be a user of your library calling a function and wondering why nothing seemed to happen. Of course, it's still pretty bad to have this exception in a shipped library but better than silent failures, IMO.
I always use
NotImplementedException -- that's what it's for, after all.
This is related to the concept of "fail fast": if your code is throwing an exception, that should get caught before going to production. If it does get to production, then at least the client knows that the assembly is incorrect.
If the code returns a meaningless value or, for
void methods, takes no action, then the consumers of your code might reasonably think that the call was meaningful when it was not. Then, later, when they get some correct code, their code might break because it depends on the former incorrect behavior.