I'm currently refactoring a part of a large codebase with no unit tests whatsoever. I tried to refactor code the brute way, i.e. by trying to guess what the code is doing and what changes wouldn't change it meaning, but without success: it randomly breaks features all around the codebase.
Note that refactoring includes moving legacy C# code to a more functional style (the legacy code doesn't use any of the features of .NET Framework 3 and later, including LINQ), adding generics where the code may benefit from them, etc.
I can't use formal methods, given how much would they cost.
On the other hand, I presume that at least "Any refactored legacy code shall come with unit tests" rule should be strictly followed, no matter how much would it cost. The problem is that when I refactor a tiny part of a 500 LOC private method, adding unit tests appears to be a difficult task.
What can help me in knowing which unit tests are relevant for a given piece of code? I'm guessing that static analysis of the code would somehow be helpful, but what are the tools and techniques I can use to:
Know exactly what unit tests should I create,
And/or know if the change I've done affected the original code in a way that it is executing differently from now?
formal methods in software development
anyway because it is used to prove the correctness of a program using predicate logic and would not have applicability to refactoring a large codebase. Formal methods typically used to prove code works correctly in areas like medical applications. You are right it is costly to do which is why it is not used often.