Source Code Analytics With Roslyn and JavaScript Data Visualization Mukherjee pdf download
Source Code Analytics With Roslyn and JavaScript Data Visualization Mukherjee pdf download
https://textbookfull.com/product/source-code-analytics-with-
roslyn-and-javascript-data-visualization-mukherjee/
https://textbookfull.com/product/code-generation-with-roslyn-
nick-harrison/
https://textbookfull.com/product/d3-js-in-action-data-
visualization-with-javascript-2nd-edition-elijah-meeks/
https://textbookfull.com/product/d3-js-in-action-data-
visualization-with-javascript-second-edition-elijah-meeks/
https://textbookfull.com/product/data-visualization-with-python-
and-javascript-scrape-clean-explore-transform-your-data-1st-
edition-kyran-dale/
Begin to Code with JavaScript 1st Edition Miles
https://textbookfull.com/product/begin-to-code-with-
javascript-1st-edition-miles/
https://textbookfull.com/product/data-visualization-toolkit-
using-javascript-rails-and-postgres-to-present-data-and-
geospatial-information-barrett-clark/
https://textbookfull.com/product/practical-python-data-
visualization-a-fast-track-approach-to-learning-data-
visualization-with-python-ashwin-pajankar/
https://textbookfull.com/product/neuroimaging-software-and-
communication-the-social-code-of-source-code-edison-bicudo/
https://textbookfull.com/product/data-visualization-
with-d3-4-x-cookbook-nick-zhu/
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
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
[email protected], 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 [email protected], 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
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
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
Summary���������������������������������������������������������������������������������������������������������������������� 154
xi
■ Contents
■
■Chapter 7: Code Visualization���������������������������������������������������������������������������� 155
Parts of a D3.js Script�������������������������������������������������������������������������������������������������� 155
Creating a Bubble Chart����������������������������������������������������������������������������������������������� 158
Finding the Aspect Ratio of Methods ������������������������������������������������������������������������������������������������� 158
Generating JSON from the List of Tuples ������������������������������������������������������������������������������������������� 159
Re-using the Same Code for Other Types of Charts ���������������������������������������������������� 163
A Different Graph to Show Dense Namespaces����������������������������������������������������������� 164
Summary ��������������������������������������������������������������������������������������������������������������������� 165
Index��������������������������������������������������������������������������������������������������������������������� 167
xii
Foreword
xiii
■ Foreword
Nowhere is this point about DIY metaprogramming more obvious than in the chapter about
visualization, where he shows how one can connect with existing (often freely available) software
components to best depict whatever it is he wants to emphasize, demonstrate, isolate—in a nutshell,
visualize. In a few dozen lines of code, he shows how the most intricate, project-specific metric or factoid can
be rendered effectively.
In this area, I am personally into arrow and graphs more than dots of varying magnitude, pie, and bar
charts. All a matter of taste. Quite logically, graphviz is my logical favorite rendition tool.
But my opinion is irrelevant. And with all due respect, so is Sudipta’s. What really matters is your
opinion, and how you can put metaprogramming to the best use possible to solve the problems you face,
address your concerns, integrate in your environment, and adapt to your ways of working and thinking. And
on your way to get there, this book is an invaluable contribution.
—Dr. Darius Blasband, CEO raincode.com
xiv
About the Author
xv
About the Technical Reviewer
xvii
Acknowledgments
First, I want to thank the Microsoft Roslyn team for constantly evolving the API to make it cover more
features of C#. As this book is being written, the next version of the C# compiler is being built using Roslyn.
If you have ever tried to create analyzers for source code and been faced with the enormous challenge of
writing your own parsers, then this book is for you, and Roslyn is your savior.
Although my name appears on the book as author, this book wouldn’t have been possible without
the support from my editors at Apress. I especially want to mention Jill Balzano and Todd Green for being
patient and supportive when I missed some of the deadlines for chapter delivery. Thank you both for being
nice and kind.
I want to thank Dr. Darius Blasband, CEO of Raincode (www.raincode.com) for kindly writing the
foreword.
Last but not the least, I want to thank my wife Mou for always being there and being supportive. Since
last time, my son graduated. He’s now potty trained. So much has changed since this picture was taken. He is
taller, sharper.
What hasn’t changed, though, is his love for computers and typing. Here is what he typed, sitting on my
computer.
xix
Another Random Scribd Document
with Unrelated Content
The Project Gutenberg eBook of Airiselän
tukinajossa: Kuvaus Lapin rajoilta
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: Finnish
AIRISELÄN TUKINAJOSSA
Kuvaus Lapin rajoilta
Kirj.
VÄINÖ KATAJA
Helsingissä, Kustannusosakeyhtiö Otava, 1915.
1.
— Vai oli maininnut minunkin nimeni! No, se tiesi sitä, että hyvät
välit jäivät!
"Sepä nyt oli… Vai jo ehti mennä. En tiennyt, että liikkeellä oli",
päivitteli Antti.
"Perä siinä on", alkoi isäntä puhua, yskien kuivaa yskää. "Perä
siinä on. Kuuluu tulevan valta-ajo nyt Airiselän maille, ja nyt pitäisi
miesten aloittaa, vaan lumen vähyys on esteenä. Vänttinen tulee
ylijohtajaksi, puulaaki luottaa Vänttiseen, kuulemma, ja on antanut
hänelle kaiken vallan Airiselän mailla… On se semmoinen
turhantarkka mies, Vänttinen. Ei kuulu ottavan ajoonsa yhtään
viinaanmenevää miestä, raittiita hakee… Kuuluukin olevan taas se
Pirtu-Santeri viinoineen liikkeellä… eikä sille voi virkakuntakaan
mitään… Tulee, kuulemma, niin suuri ajo, ettei ole takatalvina niin
suurta ollutkaan… Ja kuuluvat olevan Airiselän puut järjestään
jättiläispuita… että kysyy miestä jos hevostakin…"
"Sanoi jääneensä hyvin pitämään, että oli rehti ajaja ja hoiti hyvin
hevosensa… Ja moneen kertaan muistutti, että toimittaa
Ahvenjärven Antille sana, jotta tulla niin pian kuin… vaikka heti, että
työhön pääsee… Vaikka kuuluu olevan kovin paljon pyrkijöitäkin…"
Antti ei hyvillään osannut mitään virkkaa. Itsekseen hän ajatteli,
että tulipahan kerran puulaakiin semmoinen työnjohtaja, joka näkee,
kuka hevostaan hoitaa ja muuten rehellisesti menettelee.
Isäntä ryiskeli.
"Lie mitä lie. Vaan rennosti minulle maksoi sen mitä puhe oli… eikä
minun enempää sopinut vaariakaan… Hyvä on mies ja
suoraluontoinen", kehui Antti.
Nyt vasta isäntä ilmoitti, että Vänttinen oli hänelle jättänyt rahaa
Antille matkaa varten annettavaksi.
"En minä kiellä kerran saatua", sanoi Antti, tullen sanomasta kovin
hyvilleen. "On se miesten mies se Vänttinen…"
Metsäherra suostui.
Antti hymähti.
Antti muisti, kuinka suurin toivein hän silloin oli Selman tänne
tuonut, kun kesä oli kirkkaimmillaan ja järvi kalaisena. Eivät olleet ne
silloiset toiveet toteutuneet. Ei ollut ehtinyt eikä jaksanut maata
viljellä niin kuin silloin oli aikonut ja luulotellut jaksavansa. Ei ollut
ehtinyt korpia perkaamaan eikä ojanvarsia niityksi raivaamaan,
niinkuin oli luullut kerkiävänsä ja jaksavansa. Ja kaksikymmentä
vuotta oli siitä jo kulunut, yhdeskolmatta alkanut…
— Vaan jos tänä talvena ansiot ovat hyvät — ja hyvät oli Vänttinen
vakuuttanut niiden olevan — niin koko ensi kevään, kesän ja
syksynkin saisi rauhassa olla maatyössä. Saisi tehdä ja kerätä heinää
enemmän, kuokkia peltoa ja raivata ojanvarsia, kun ei olisi ruoka
hakemisessa, jatkoi Antti mietteitään. — Nyt ei enää kulu ansioista
mitään vieraalle, kun oma poika on kumppanina, niin että kyllähän
tässä nyttemmin joutaa talontöitäkin tekemään.
Oli toki hyvä, että tuiskusi lunta, jotta pääsi matkaan. Niinkuin
taivas olisi tiennyt hänen tulisen kiireensä.
"Oli luvannut, vaan kun nyt näin pian rekikeliksi tuiskusi lunta, niin
ei sovi vitkastella. Lähteä täytyy. Luottaa siihen saapi Vänttiseen, se
on kerrassaan taattu. Mutta huolettaa minua nyt jättää sinut tänne
turvattomaksi lasten kanssa, kun tuo nuorin vielä on kipeä…"
"Älähän nyt meistä… kyllä me, kun ovat puut, heinät kotona ja
ruokaakin on pitkäksi aikaa. Ja eikö poikine Lainakin parin viikon
päästä… Mutta se minua huolettaa, kuinka te siellä tukkimetsässä…"
"Viime yönäkö?" kysyi Antti, aivan kuin epäilisi, ettei Selma ollut
unta nähnyt.
"En tiedä oliko yö vai aamu", jatkoi Selma. "Mutta tuntui, että olin
tulossa navetasta tänne pirttiin. Oli olevinaan pimeä ja taivas
paksussa pilvessä. Kun sitten katsahdin taivaalle… niin tähti lensi
toiselta taivaan rannalta toiselle ja tuli yhtäkkiä niinkuin kesäkirkas
päivä… Ja kun ehdin portaille, näkyi toiselta puolen järveä musta
kuorma tulossa, mutta ei yhtään ihmistä kuormalla eikä kuorman
perässä…"
"No, ne nyt ovat niitä sinun joutavia uniasi. Kesä oli olevinaan ja
reessä kuorma!" koetti Antti lyödä leikiksi.
Mutta Selman silmissä oli vakava ilme ja hän virkkoi:
"Ethän sinä tänä aamuna ole vielä ulkona käynytkään!" koetti Antti
yhä leikiksi lyödä.
Isän ja pojan välit olivat aina olleet erittäin hyvät. Antti rakasti
koko perhettään hellästi, ja monta murheen hetkeä hän oli viettänyt
sen vuoksi, ettei kyennyt lapsiraukoilleen lämpöisiä vaatteitakaan
laittamaan. Puolialastomina olivatkin nuorimmat, vähän riekaleita
ympärillään, ja nukkuivat rivissä lattialla, lähellä uunia.
Yrjö oli kuitenkin jo parina kesänä ollut tukkijoella ja viime kesän
kylässä kesämiehenä, niin että oli ansainnut hyvät vaatteet ylleen.
Olipa siskoillekin ostanut paitakankaat.
"Jopa tuo nyt riittää", arveli Yrjö ja alkoi pukeutua siinä mielessä,
että matkaan tulee lähtö.
"Puhui se. Sanoi, ettei hänen ole koskaan ennen ollut niin ikävä
jäädä kotia kuin nyt…"
2.
"Niin monta niitä on", sanoi Antti, ja hänen äänensä vähän vapisi.
"Kuinkahan monta niistä pitäisi olla poissa, että olisi kruunun ja
teidän mielestänne parhaiksi?" lisäsi hän sitten katkerasti.
Antti oli luonnostaan tyyni mies, mutta nyt oli sattunut solvaus niin
arkaan paikkaan, ettei hän enää voinut hillitä itseään.
"Tuli lisää tahi ei", sanoi hän nousten seisomaan, "se ei kuulu
teihin! Minulla ovat lapset tässä näkösällä kaikki… lienevätkö niin
forstmestarillakin… Ja minä tunnustan ja tiedän ne omikseni…"
Mutta metsäherralle tuli yhtäkkiä kiire pois, niin että hän istui
reessä, ennenkuin vallesmanni ehti uloskaan.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com