top | item 25071973

(no title)

acupofnope | 5 years ago

Like so: `ISomething something = new Something();`

Construction injection is a type of DI, and it's optional, you don't have to use it.

edit: for clarity

discuss

order

mandeepj|5 years ago

OMG! I was wondering you might be proposing this. You should not be using "new" keyword anywhere in your code. If you have hard coded implementations like that in your code then how will you mock them in unit tests? I hope you'll not reply-with saying "don't write unit tests".

acupofnope|5 years ago

"OMG! I was wondering you might be proposing this. You should not be using "new" keyword anywhere in your code"

That sounds like dogmatism, and I'm going to take a special note of the emotionally charged language you're using here; it's symptom of some of the underlying issues I see in our industry (more to be said on this).

That said, I do agree with the sentiment in general, but you can (and sometimes should) use the `new` keyword in your code, especially when writing tests. In some cases, it makes testing so much easier and helps with the maintainability.

"I hope you'll not reply-with saying "don't write unit tests"

After 2 decades in this profession, the only think I'm sure of is there is no "silver bullet". Of course, as a general rule, it's a good idea to have a test coverage of your codebase but sometimes it's not valuable and not worth adding it. So it's a "it depends" from me!

Thiez|5 years ago

I am a big fan of DI, but you can often get a lot of useful unit testing done when you try and keep I/O mostly seperate from the rest of the code. If you have ever programmed in Haskell you will know what I mean.