A Code Quality Lesson – How to Get Rid of the Clones
Have you ever read someone else’s code and thought you’ve seen similar or redundant code elsewhere (me -> Raises Hand! I love hate Fortran)? It’s often a style of should be extinct dinosaur thinking how-to-code mentality where you need modular code in its own silo. Common or redundant features (ex: printing, API features, etc.…) may be included just because that programmer is responsible for their own silo in the organization and they have no “Care Bear” feelings for team-based code efficiency. Well this article is meant to discuss code clone detection as a mean to reduce silos, look for common ground, eliminate inefficiencies, and identify issues early on in development and testing phases. Code clone detection increases maintainability of code and lowers the cost of code changes.
Visual Studio’s integrated development environment (IDE) can help identify snippets of code that are very similar. It is clever enough to find clones with different variable names or even rearranged statements but is limited to working with statements and not declarations. It delivers code clone detection into a results window to allow a developer to compare two potential code clones side by side.
Note: According to Microsoft [link], the code clone detection feature works with only code 10 lines or longer. I verified the clone detection did not work with anything less, which would had been a nice feature but they just don’t have it yet.
To launch Code Clone Detection in your Visual Studio, go to Menu -> Analyze -> Analyze Solution for Code Clones. This feature may be only available in your premium+ editions.
Here is an example of a project [link: CloneExample.zip] I made for displaying code clone detection features.
After running code clone detection with code snippets that are >= 10 lines of code, I see some activity in the Code Clone Analysis Results window where I can then select the two matches, right-click, and then Compare the code (see below).
The compare command will bring up the two sections of the code that are side by side into two separate windows aligned next together with the similar sections shown and highlighted. An example is shown below.
This begs the question, what level of similarity is needed to be flagged as a possible Code Clone problem?
I tinkered with the Test Method in sequential fashion and re-ran the Code Clone Analysis for every change to see what effect, if any, small changes had on the code clone detection. Surprisingly, it picked up potential matches even though I had started to deviate from the comparison Main method.
|Sequential Change||Change in Test Method||Result|
|1||number -> testnumber||Strong Match|
|2||40000 -> 400000||Strong Match|
|3||Remove an else if statement||Medium Match|
|4||Add stuff to my else if statement||Medium Match|
|5||Add new statement, sandwich inside the code area||Medium Match|
|6||Add a new statement||Weak Match|
|7||Remove an else if statement||Weak Match|
|8||Remove another else if statement||Weak Match|
|9||Add a conditional if nested in the else if statement||No Match|
The final Test method code after sequence 9 is shown below and finally it is unique enough to have No Match.
Using Visual Studio, it is easy enough the identify code clones, down to the weakest matches, but now what?
Recognize you may need to work as a team, pick up the phone, call that programmer over the pond, discuss the code quality issue. You could agree to maybe create a separate library with common code functionality, methods, etc… and call it a utility library to do common utilities. In small programs where you are the sole developer, you could create more inter dependencies, change your code to call a method in one class instead of repeating the code in different modules/classes. Your team could also consider using polymorphic techniques to process actions on objects that share some relationships to reduce the number of redundant lines of code. Complex design for efficiency will take planning, deep thought, and teamwork so be prepared to step out of your silo.
I hope you enjoyed my article. I think we engineers in the workplace often feel pressured and compelled to produce results independently to strut our skills and prove our value in the workplace. Huddling up together to discuss potential code quality synergies may be frowned upon as a time waster and big hurdle to some introverted geniuses in the workplace. I would suggest making Code Quality a topic before, during, and after development in whatever agile/other trendy/development methodologies your organization prefers. Get people to let their code-macho guard down, work and talk with others, and promote it as an opportunity to care to change. Go out for drinks to celebrate! (P.S. Do invite the gals, we feel left out if you do not invite us to lunch/after work drinks with the guys!)