I once was refactoring and came across something like this code:
string strMyString; try { strMyString = Session["MySessionVar"].ToString(); } catch { strMyString = ""; }
Resharper pointed out that .ToString () is redundant, so I pulled it out. Unfortunately, this led to a code violation. Whenever MySessionVar was null, it did not raise a NullReferenceException, which he was referring to raise it to the catch block. I know it was some kind of sad code. But I learned a good lesson from it. Do not quickly go through the old code, relying on a tool that will help you do refactoring - think about it through yourself.
I ended up refactoring as follows:
string strMyString = Session["MySessionVar"] ?? "";
Update: Since this post is supported and does not technically contain an answer to a question, I decided that I should answer this question. (Well, it bothered me to such an extent that I really dreamed about it.)
Personally, I ask myself a few questions before refactoring.
1) Is the system under source control? If so, continue with refactoring because you can always back down if something breaks.
2) Are there unit tests for the functionality that I am modifying? If so, great! Refactoring The danger here is that the presence of unit tests does not indicate the accuracy and scope of these unit tests. Good unit tests should pick up any violations.
3) Do I fully understand the code I'm refactoring? If there is no source control and no tests, and I really don't understand the code I'm changing, this is a red flag. I would need to be more comfortable with the code before refactoring.
In case # 3, I will probably take the time to actually keep track of all the code that currently uses the method I'm refactoring. Depending on the size of the code, this may be easy or impossible (that is, if it is a public API). If it comes to the public API, you really need to understand the initial purpose of the code from a business perspective.