Table Of ContentSource Code
Analytics With
Roslyn and
JavaScript Data
Visualization
—
Sudipta Mukherjee
Foreword by Dr. Darius Blasband
Source Code Analytics
With Roslyn and
JavaScript Data
Visualization
Sudipta Mukherjee
Foreword by Dr. Darius Blasband
Source Code Analytics With Roslyn and JavaScript Data Visualization
Sudipta Mukherjee
Varthur Hobli, India
ISBN-13 (pbk): 978-1-4842-1924-9 ISBN-13 (electronic): 978-1-4842-1925-6
DOI 10.1007/978-1-4842-1925-6
Library of Congress Control Number: 2016961817
Copyright © 2016 by Sudipta Mukherjee
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage
and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Todd Green
Technical Reviewer: Abhishek Gupta
Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan,
Jonathan Gennick, Todd Green, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal,
James Markham, Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing
Coordinating Editor: Jill Balzano
Copy Editor: Corbin Collins
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springer.com. Apress Media, LLC is a California LLC and the sole
member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is
a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Special Bulk
Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text are available to
readers at www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary
Material section for each chapter.
Printed on acid-free paper
To Mou, for never giving up on me
Contents at a Glance
Foreword ���������������������������������������������������������������������������������������������������������������xiii
About the Author �����������������������������������������������������������������������������������������������������xv
About the Technical Reviewer �������������������������������������������������������������������������������xvii
Acknowledgments ��������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Meet Roslyn Syntax API �����������������������������������������������������������������������1
■ Chapter 2: Code Quality Metrics ��������������������������������������������������������������������������15
■ Chapter 3: Design Quality Metrics �����������������������������������������������������������������������45
■ Chapter 4: Code Performance Metrics �����������������������������������������������������������������71
■ Chapter 5: Code Mining ���������������������������������������������������������������������������������������91
■ Chapter 6: Code Forensics ���������������������������������������������������������������������������������131
■ Chapter 7: Code Visualization ����������������������������������������������������������������������������155
Index ���������������������������������������������������������������������������������������������������������������������167
v
Contents
Foreword ���������������������������������������������������������������������������������������������������������������xiii
About the Author �����������������������������������������������������������������������������������������������������xv
About the Technical Reviewer �������������������������������������������������������������������������������xvii
Acknowledgments ��������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Meet Roslyn Syntax API �����������������������������������������������������������������������1
Syntax Tree Methods ��������������������������������������������������������������������������������������������������������1
GetRoot ���������������������������������������������������������������������������������������������������������������������������������������������������1
DescendantNodes �����������������������������������������������������������������������������������������������������������������������������������2
DescendantTrivia ������������������������������������������������������������������������������������������������������������������������������������2
DescendantTokens ���������������������������������������������������������������������������������������������������������������������������������2
Ancestors �����������������������������������������������������������������������������������������������������������������������������������������������3
Important Types ���������������������������������������������������������������������������������������������������������������3
MethodDeclarationSyntax ����������������������������������������������������������������������������������������������������������������������3
ClassDeclarationSyntax �������������������������������������������������������������������������������������������������������������������������4
SructDeclarationSyntax �������������������������������������������������������������������������������������������������������������������������5
InterfaceDeclarationSyntax �������������������������������������������������������������������������������������������������������������������5
IndexerDeclarationSyntax �����������������������������������������������������������������������������������������������������������������������5
PropertyDeclarationSyntax ��������������������������������������������������������������������������������������������������������������������6
ForStatementSyntax �������������������������������������������������������������������������������������������������������������������������������7
ConstructorDeclarationSyntax ����������������������������������������������������������������������������������������������������������������7
ForEachStatementSyntax �����������������������������������������������������������������������������������������������������������������������8
SwitchStatementSyntax �������������������������������������������������������������������������������������������������������������������������8
vii
■ Contents
ParameterSyntax ������������������������������������������������������������������������������������������������������������������������������������9
DoStatementSyntax ��������������������������������������������������������������������������������������������������������������������������������9
UsingStatementSyntax �������������������������������������������������������������������������������������������������������������������������10
IfStatementSyntax ��������������������������������������������������������������������������������������������������������������������������������11
TryStatementSyntax �����������������������������������������������������������������������������������������������������������������������������11
ConditionalExpressionSyntax ���������������������������������������������������������������������������������������������������������������13
ParenthesizedLambdaExpressionSyntax ����������������������������������������������������������������������������������������������13
WhileStatementSyntax �������������������������������������������������������������������������������������������������������������������������13
Summary ������������������������������������������������������������������������������������������������������������������������14
■ Chapter 2: Code Quality Metrics ��������������������������������������������������������������������������15
Setting Up LINQPad to Use Roslyn ���������������������������������������������������������������������������������16
Magic Numbers in Mathematics ������������������������������������������������������������������������������������18
Magic Numbers Used in Index ��������������������������������������������������������������������������������������20
Magic Numbers Used in Conditions ������������������������������������������������������������������������������21
Ladder if Statements �����������������������������������������������������������������������������������������������������24
Fragmented Conditions �������������������������������������������������������������������������������������������������25
Hungarian Names for Variables ������������������������������������������������������������������������������������27
Lots of Local Variables in Methods ���������������������������������������������������������������������������������30
Methods Not Using All Parameters ���������������������������������������������������������������������������������31
Multiple Return Statements ����������������������������������������������������������������������������������������32
Long Parameter Lists �����������������������������������������������������������������������������������������������������33
goto Labels ��������������������������������������������������������������������������������������������������������������������35
Large Class ��������������������������������������������������������������������������������������������������������������������37
Lines of Code �����������������������������������������������������������������������������������������������������������������38
Control Flags ������������������������������������������������������������������������������������������������������������������40
Code-to-Comment Ratio ������������������������������������������������������������������������������������������������42
Summary ������������������������������������������������������������������������������������������������������������������������44
viii
■ Contents
■ Chapter 3: Design Quality Metrics �����������������������������������������������������������������������45
Deeply Nested Loops �����������������������������������������������������������������������������������������������������45
Out Parameters ��������������������������������������������������������������������������������������������������������������47
Deeply Nested if Blocks (Bow and Arrow) ����������������������������������������������������������������������49
Long List of Switches ����������������������������������������������������������������������������������������������������51
Data Classes (That Aren’t Classes) ��������������������������������������������������������������������������������52
Local Classes �����������������������������������������������������������������������������������������������������������������54
Refused Bequest ������������������������������������������������������������������������������������������������������������55
Lots of Overloaded Versions of a Method �����������������������������������������������������������������������58
Empty Interfaces �����������������������������������������������������������������������������������������������������������60
Too Many Parameters on Generic Types �������������������������������������������������������������������������61
Static Members on Generic Types ����������������������������������������������������������������������������������63
Abstract Types with Constructors ����������������������������������������������������������������������������������64
Sealed Class and Protected Members ���������������������������������������������������������������������������66
Object Obsession �����������������������������������������������������������������������������������������������������������67
Summary ������������������������������������������������������������������������������������������������������������������������69
■ Chapter 4: Code Performance Metrics �����������������������������������������������������������������71
Avoid Boxing ������������������������������������������������������������������������������������������������������������������71
Avoid Using dynamic by Default �������������������������������������������������������������������������������������73
Avoid Excessive Local Variables �������������������������������������������������������������������������������������74
Prefer Literals Over Evaluation ���������������������������������������������������������������������������������������75
Avoid volatile Declarations ���������������������������������������������������������������������������������������������77
Don’t Use an Object Array in params������������������������������������������������������������������������������78
Avoid Unnecessary Projections �������������������������������������������������������������������������������������79
Value Types Should Override Equals and GetHashCode ������������������������������������������������81
Avoid Using Empty Strings to Find Zero Length Strings �������������������������������������������������83
Prefer “Jagged” Arrays over Multidimentional Arrays ����������������������������������������������������85
ix
■ Contents
Don’t Return Array from a Property �������������������������������������������������������������������������������86
Don’t Use Thread�Abort or Thread�Suspend �������������������������������������������������������������������88
Summary �����������������������������������������������������������������������������������������������������������������������90
■ Chapter 5: Code Mining ���������������������������������������������������������������������������������������91
Useful Patterns in Code Mining ��������������������������������������������������������������������������������������91
Identifying Near Duplicate Code �����������������������������������������������������������������������������������������������������������91
Plagarism Detection �����������������������������������������������������������������������������������������������������������������������������92
Source Code Forensics �������������������������������������������������������������������������������������������������������������������������93
Knowledge Mining ��������������������������������������������������������������������������������������������������������������������������������93
SmartDiff: A Smart Code Comparer ��������������������������������������������������������������������������������93
Anatomy of a Code Block �����������������������������������������������������������������������������������������������94
Anatomy of a Property Declaration ������������������������������������������������������������������������������������������������������94
Anatomy of a Method ���������������������������������������������������������������������������������������������������������������������������95
Anatomy of a Class �����������������������������������������������������������������������������������������������������������������������������100
Detecting Plagarism �����������������������������������������������������������������������������������������������������103
Completely Duplicate Code �����������������������������������������������������������������������������������������������������������������103
Almost Duplicate Code �����������������������������������������������������������������������������������������������������������������������105
Knowledge Mining �������������������������������������������������������������������������������������������������������108
Design Pattern Mining ������������������������������������������������������������������������������������������������������������������������108
Mining Frequent Nested Patterns in Code ������������������������������������������������������������������������������������������123
API Mining ������������������������������������������������������������������������������������������������������������������������������������������127
Summary ����������������������������������������������������������������������������������������������������������������������130
■ Chapter 6: Code Forensics ���������������������������������������������������������������������������������131
What Is Developer Style? ���������������������������������������������������������������������������������������������131
Elements of Style ���������������������������������������������������������������������������������������������������������132
MethodsPerClass ��������������������������������������������������������������������������������������������������������������������������������132
ClassesPerNamespace �����������������������������������������������������������������������������������������������������������������������133
PublicMethodsPerClass ����������������������������������������������������������������������������������������������������������������������133
PrivateMethodsPerClass ���������������������������������������������������������������������������������������������������������������������133
x
■ Contents
ProtectedMethodsPerClass ����������������������������������������������������������������������������������������������������������������133
OverriddenMethodsPerClass ��������������������������������������������������������������������������������������������������������������133
ParametersPerMethod ������������������������������������������������������������������������������������������������������������������������133
StaticClassesPerNamespace ��������������������������������������������������������������������������������������������������������������133
SealedClassesPerNamespace ������������������������������������������������������������������������������������������������������������133
PercentageOfMethodsWithVerbs ��������������������������������������������������������������������������������������������������������134
PercentageOfEnglishWordsUsed ��������������������������������������������������������������������������������������������������������134
CodeToCommentRatio ������������������������������������������������������������������������������������������������������������������������134
Vector Representation of Style ����������������������������������������������������������������������������������������������������������134
Getting the Data for Experimentation ���������������������������������������������������������������������������134
Creating the Profile Vector��������������������������������������������������������������������������������������������135
Finding MethodsPerClass �������������������������������������������������������������������������������������������������������������������136
Finding ClassesPerNamespace ����������������������������������������������������������������������������������������������������������137
Finding Methods That Are of a Given Type ������������������������������������������������������������������������������������������137
Finding ParametersPerMethod �����������������������������������������������������������������������������������������������������������138
Finding Sealed and Static Classes ����������������������������������������������������������������������������������������������������139
Finding PercentageOfMethodsWithVerbs �������������������������������������������������������������������������������������������139
Finding PercentageOfEnglishWordTokens ������������������������������������������������������������������������������������������140
Finding CodeToCommentRatio ������������������������������������������������������������������������������������������������������������142
Generating CSV from the Profile ��������������������������������������������������������������������������������������������������������142
We Have Profile Vectors—Now What? �������������������������������������������������������������������������144
kNN (k nearest neighbor) �������������������������������������������������������������������������������������������������������������������144
Demo on Troelson and Freeman’s Code �����������������������������������������������������������������������147
Calculating Accuracy of the System �����������������������������������������������������������������������������148
Demo on GitHub Repositories ���������������������������������������������������������������������������������������149
Non-Obvious Usages of Code Forensics �����������������������������������������������������������������������150
Mentor-Mentee Association ����������������������������������������������������������������������������������������������������������������150
Data-Driven Developer Rotation ���������������������������������������������������������������������������������������������������������152
Summary ����������������������������������������������������������������������������������������������������������������������154
xi
Description:Learn how to build an interactive source code analytics system using Roslyn and JavaScript. This concise 150 page book will help you create and use practical code analysis tools utilizing the new features of Microsoft’s Roslyn compiler to understand the health of your code and identify parts of th