DEVELOPER Refactoring Code Clones in Visual Studio 2012 Debugging and updating lengthy applications, with repeated lines of code for multiple instances, is one of the major pain points faced by developers. The Code Clone Analysis feature in the latest VS version aims to ease some of it

— Adeesh Sharma

ost websites today want to capture and deliver customised information to clients. SNAPSHOT You might need to store information specific Applies to: Developers using VS Mto thousands or even millions of unique USP: Learn how to use the new code clone analysis customers that visit your site regularly in order to work out feature in VS 2012 their likes and dislikes. This could translate into as many Primary link: http://bit.ly/puhQpt code segments, unique to each customer. To give you an Keywords: VS 2012, visual studio example, we once visited the premises of one of India’s largest companies in Bangalore as part of our annual Best IT Implementations awards’ audit process. That company uses Microsoft technologies to deploy and Categorising code clones maintain ecommerce applications for its customers spread This is how MSDN defines code clones: across the globe. A challenge they regularly faced was in “Code clones are separate fragments of code that are updating code whenever a bug arose which impacted the very similar. They are a common phenomenon in an ap- performance of their applications. Since most of the code plication that has been under development for some time. they were using was duplicated for customers, it became a Clones make it hard to change your application because mundane task for the team to go through individual code you have to find and update more than one fragment. segments and correct the anomaly. They were using tools Visual Studio can help you find code clones so that you can to reduce the duplication of effort but still were on the refactor them.” lookout for better technologies to resolve this. The latest Taking a closer look at this definition, it is very clear edition of Microsoft’s Visual Studio 2012 comes with a code that code clones could be found in methods that have clone and analysis feature that attempts to address this. been developed using the same logic, different instances of the same object, or even conditional loops like ‘if-else’, Why do a ‘Code Clone Analysis’ ‘do-while’, etc. First select a code segment for which you Large applications are spread across various projects and need to find clones for. The tool would suggest clones divisions within an organisation. In case some of these across the entire code, in varying degrees of similarity experience performance issues or require urgent updates and categorises them accordingly. For instance, there or even code refactoring, a developer needs quick access could be code segments that are replicas of previous to the code segments where changes are required. Also, code segments, which are categorised as exact matches. as part of cleanup, it’s good to Other code segments could be similar in logic but with a do away with software clones so that the source tree change in name of the method used, such code clones are becomes easier for other developers to understand. labelled as strong matches. Yet others might look similar After completing a new code segment it’s a good habit to in logic and syntax but represent different functionality, refactor code into shared classes or methods, and also look such clones are referred to as medium matches. Others up for similar code that exists. that may be distantly related get categorised as weak

54 PCQuest February 2013 pcquest.com twitter.com/pcquest facebook.com/pcquest linkd.in/pcquest [email protected] matches. You get count of the various clones for the code tool as is used for comparing versions under source control. segment you selected. To change settings, open Options from the Tools menu and The code clones are grouped based on their similarity expand Source Control and Visual Studio Team Foundation to the original code. You can click the code segment you se- Server. Under the Configure User Tools you can add the lected and also on each group of code clones to find their desired changes. location in the . Apart from finding clones for a selected code segment you also have the option to analyze What’s excluded under Code the complete source code and get a consolidated count of Clone Analysis the instances of different code clones. While the logic behind finding code clones is pretty much To compare, open the Code Clone Results window justified, it does make sense to be aware about the obvious where you can open the two code clones in adjacent win- exclusions. First, any kind of type declarations are excluded. dows. The tool should automatically highlight the portions For instance, two classes with similar set of field declarations of similar code. This feature uses the same comparison shall not be reported. Likewise, statements containing the same variables with different names are not to be compared. Only the statements in methods or functions, conditional statements or definitions are to be compared. Also, while analysing the entire source code, under the Analyse Solution for Code Clones option, small code segments (less than 10 statements long) are not compared. This setting can however be circumvented by applying ‘Find matching clones in solution’ to shorter code segments. After the search for clones is complete, the results are categorised based on strength of similarity to the original code Within a project, you can also define a .codeclonesettings file, which shall contain code elements that shall not be searched if they are put under the Exclusions section. You can also exclude generated code such as the text templates, by naming them in the .codeclonesettings file.

Setting up exclusions An XML file with a .CODECLONESETTINGS extension is available at the project level. Within this file you can add the elements to be excluded from code clone analysis. The base elements consist of a CodeCloneSettings element with an Exclusions child: ... The elements within this template can consist of, for instance, a directory of text templates called text. So, the code gets modified as under: By clicking on the above option, you can get your entire code analysed in one shot text\*.cs You can also exclude namespaces, types and functions. Simply remove the line in the above code starting with and replace it with one of the following, as required: ... The count of code snippets similar to the original code are ... displayed. Also, you get details of the total clone groups and the amount of code lines that have been analysed. ... pcquest.com twitter.com/pcquest facebook.com/pcquest linkd.in/pcquest [email protected] February 2013 PCQuest 55