FSharpLint
FSharpLint copied to clipboard
New rule proposal: Avoid commented-out lines of code
Description
Commented-out lines should be removed before release. It might already be broken. If old code is later required again, it will be available in source code management system.
See also:
- Robert C. Martin's Clean Code Tip of the Week #7: Clean up Old Commented Out Code
- Can commented-out code be valuable documentation?
Expected behavior
Report any commented-out code.
- Report multi-line commented code
let animal = "bird"
// Test the length of the string.
if (animal.Length = 1) then
// Not reached.
printfn "A"
(*
elif (animal.Length = 2) then
// Not reached.
printfn "B"
*)
else
// This statement is reached.
printfn "C"
- Report single line commented code
assignment.
let result =
if count >= 200 then 1
// elif count <= 100 then 2
else 3
Actual behavior
Rule not available today.
Known workarounds
One might configure a hint
Related information
Rule not listed at http://fsprojects.github.io/FSharpLint/Rules.html
I think this would be a useful rule to have, but it may be difficult to implement. How can we tell apart normal comments from commented-out code? We would need to come up with some heuristic.
I have not investigated how other analysers do the job. But there are some, which do that for other languages, like the C# analyzer, Rule S125 from SonarQube.
Without knowing in detail what they do my first idea:
- XML doc comments are always valid and will not be reported
- other commented lines "remove the comment and try non-empty lines to tokenize".
- have a look at
src/SonarAnalyzer.CSharp/Rules/CommentedOutCode.cs
in the above mentioned C# analyzer