There is an option in c# to execute code unchecked. It\'s generally not advised
ID: 643489 • Letter: T
Question
There is an option in c# to execute code unchecked. It's generally not advised to do so, as managed code is much safer and it overcomes a lot of problems.
However I am wondering, if you're sure your code won't cause errors, and you know how to handle memory then why (if you like fast code) follow the general advice?
I am wondering this since I wrote a program for a video camera, which required some extremely fast bitmap manipulation. I made some fast graphical algorithms myself, and they work excellent on the bitmaps using unmanaged code.
Now I wonder in general, if you're sure you don't have memory leaks, or risks of crashes, why not use unmanaged code more often ?
PS my background: I kinda rolled into this programming world and I work alone (I do so for a few years) and so I hope this software design question isn't that strange. I don't really have other people out there like a teacher to ask such things.
Explanation / Answer
Well, its mostly a case of the age old adage
Dont optimize
(for expert only) Dont optimize yet
But actually I can think of three main reason to avoid unsafe code.
Bugs : The critical part of your question is "if you're sure your code won't cause errors". Well, how can you be absolutely totally sure? Did you use a prover with formal method that guaranteed your code correct? One thing is certain in programming, and that is that you will have bugs. When you take off a safety, you allow new sort of bugs to creep trough. When you let the garbage collector take care of the memory for you, a lot of problem go away.
Not always as fast as you think: The other point is : depending on the problem, the gain may not be that great. Although I can't seem to find them right now, I remember a study by Google comparing the speed of Java, Scala, Go and C++. Once optimized to the ground, of course C++ was much faster. But the algorithm programmed in the "idiomatic" way were not really that much faster. Idiomatic in the sense that they were using standard structure and idioms (stl container, no unrolled loop, etc). Microsoft did a similar experiment with C# and C++. Raymond Chen, one of the top Microsoft Engineer, had to write his own implementation of std::string to beat C#. (see: http://www.codinghorror.com/blog/2005/05/on-managed-code-performance-again.html) For much less effort, you got pretty decent performance in managed code, so its often not worth the trouble.
Reusability Unsafe code can only be used in a full trust environment. For example, in an ASP.NET server, you usually can't use unsafe code, since it would be pretty easy to introduce a vulnerability by buffer overflow. Another example would be clickonce. Or if your application was accessed from a network share. So if you plan to use your code in a variety of deployment scenario, unsafe code is out of the game.
So basically : its frowned upon because it may introduce unnecessary bugs, it may well be for no gain at all, and it reduce the reusability of your code.
But if your scenario really require it for performance (and you have data to prove it), you are an experienced programmer that know how to handle memory and your code will be used in a controlled environnement, then sure, go for it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.