-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNDependRuleFile.ndrules
More file actions
6730 lines (6030 loc) · 292 KB
/
NDependRuleFile.ndrules
File metadata and controls
6730 lines (6030 loc) · 292 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<Queries>
<Group Name="NDepend Defined Rules" Active="True" ShownInReport="False">
<Group Name="Security" Active="True" ShownInReport="False">
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Don't use CoSetProxyBlanket and CoInitializeSecurity</Name>
// <Id>ND3100:DontUseCoSetProxyBlanketAndCoInitializeSecurity</Id>
warnif count > 0
from m in Application.Methods
where (m.HasAttribute ("System.Runtime.InteropServices.DllImportAttribute".AllowNoMatch()))
&& m.SimpleName.EqualsAny("CoSetProxyBlanket","CoInitializeSecurity")
select new {
m,
Debt = 1.ToHours().ToDebt(),
AnnualInterest = 2.ToHours().ToAnnualInterest()
}
//<Description>
// As soon as some managed code starts being JIT’ed and executed by the CLR,
// it is too late to call *CoInitializeSecurity()* or *CoSetProxyBlanket()*.
// By this point in time, the CLR has already initialized the
// COM security environment for the entire Windows process.
//</Description>
//<HowToFix>
// Don't call CoSetProxyBlanket() or CoInitializeSecurity() from managed code.
//
// Instead write an unmanaged "shim" in C++ that will call
// one or both methods before loading the CLR within the process.
//
// More information about writing such unmanaged "shim"
// can be found in this StackOverflow answer:
// https://stackoverflow.com/a/48545055/27194
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Don't use System.Random for security purposes</Name>
// <Id>ND3101:DontUseSystemRandomForSecurityPurposes</Id>
warnif count > 0
from m in Application.Methods
where m.CreateA("System.Random".AllowNoMatch())
select new {
m,
Debt = 15.ToMinutes().ToDebt(),
AnnualInterest = 1.ToHours().ToAnnualInterest()
}
//<Description>
// The algorithm used by the implementation of **System.Random** is weak
// because random numbers generated can be predicted.
//
// Using predictable random values in a security critical context
// can lead to vulnerabilities.
//</Description>
//<HowToFix>
// If the matched method is meant to be executed in a security
// critical context use **System.Security.Cryptography.RandomNumberGenerator**
// or **System.Security.Cryptography.RNGCryptoServiceProvider** instead.
// These random implementations are slower to execute but the random numbers
// generated cannot be predicted.
//
// Find more on using *RNGCryptoServiceProvider* to generate random values here:
// https://stackoverflow.com/questions/32932679/using-rngcryptoserviceprovider-to-generate-random-string
//
// Otherwise you can use the faster **System.Random** implementation and
// suppress corresponding issues.
//
// More information about the weakness of *System.Random* implementation
// can be found here: https://stackoverflow.com/a/6842191/27194
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Don't use DES/3DES weak cipher algorithms</Name>
// <Id>ND3102:DontUseDES3DESWeakCipherAlgorithms</Id>
warnif count > 0
from m in Application.Methods
where
m.CreateA("System.Security.Cryptography.TripleDESCryptoServiceProvider".AllowNoMatch()) ||
m.CreateA("System.Security.Cryptography.DESCryptoServiceProvider".AllowNoMatch()) ||
m.IsUsing("System.Security.Cryptography.DES.Create()".AllowNoMatch()) ||
m.IsUsing("System.Security.Cryptography.DES.Create(String)".AllowNoMatch())
select new {
m,
Debt = 1.ToHours().ToDebt(),
Severity = Severity.High
}
//<Description>
// Since 2005 the NIST, the US National Institute of Standards and Technology,
// doesn't consider DES and 3DES cypher algorithms as secure. Source:
// https://www.nist.gov/news-events/news/2005/06/nist-withdraws-outdated-data-encryption-standard
//</Description>
//<HowToFix>
// Use the AES (Advanced Encryption Standard) algorithms instead
// with the .NET Framework implementation:
// *System.Security.Cryptography.AesCryptoServiceProvider*.
//
// You can still suppress issues of this rule when using
// DES/3DES algorithms for compatibility reasons with legacy applications and data.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Don't disable certificate validation</Name>
// <Id>ND3103:DontDisableCertificateValidation</Id>
warnif count > 0
from m in Application.Methods
where
m.IsUsing("System.Net.ServicePointManager.set_ServerCertificateValidationCallback(RemoteCertificateValidationCallback)".AllowNoMatch())
select new {
m,
Debt = 30.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Description>
// Matched methods are subscribing a custom certificate validation
// procedure through the delegate: *ServicePointManager.ServerCertificateValidationCallback*.
//
// Doing so is often used to disable certificate validation
// to connect easily to a host that is not signed by a **root certificate authority**.
// https://en.wikipedia.org/wiki/Root_certificate
//
// This creates a **vulnerability to man-in-the-middle attacks** since the client will trust any certificate.
// https://en.wikipedia.org/wiki/Man-in-the-middle_attack
//</Description>
//<HowToFix>
// Don't rely on a weak custom certificate validation.
//
// If a legitimate custom certificate validation procedure must be subscribed,
// you can chose to suppress related issue(s).
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Review publicly visible event handlers</Name>
// <Id>ND3104:ReviewPubliclyVisibleEventHandlers</Id>
warnif count > 0
from m in Application.Methods
where
m.IsPubliclyVisible
&& m.Name.EndsWith("(Object,EventArgs)")
select new {
m,
Debt = 10.ToMinutes().ToDebt(),
Severity = Severity.Low
}
//<Description>
// Review publicly visible event handlers to check those that are
// running security critical actions.
//
// An event handler is any method with the standard signature *(Object,EventArgs)*.
// An event handler can be registered to any event matching this standard signature.
//
// As a consequence, such event handler can be subscribed
// by malicious code to an event to provoke execution of the
// security critical action on event firing.
//
// Even if such event handler does a security check,
// it can be executed from a chain of trusted callers on the call stack,
// and cannot detect about malicious registration.
//</Description>
//<HowToFix>
// Change matched event handlers to make them non-public.
// Preferably don't run a security critical action from an event handler.
//
// If after a careful check no security critical action is involved
// from a matched event-handler, you can suppress the issue.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Pointers should not be publicly visible</Name>
// <Id>ND3105:PointersShouldNotBePubliclyVisible</Id>
warnif count > 0
from f in Application.Fields
where
f.FieldType != null &&
f.FieldType.FullName.EqualsAny("System.IntPtr","System.UIntPtr") &&
(f.IsPubliclyVisible || f.IsProtected)
let methodsUserOutsideMyAssembly = f.MethodsUsingMe.Where(m => m.ParentAssembly != m.ParentAssembly)
select new {
f,
f.FieldType,
methodsUserOutsideMyAssembly,
Debt = (15 + 10*methodsUserOutsideMyAssembly.Count()).ToMinutes().ToDebt(),
Severity = f.IsInitOnly ? Severity.Medium : Severity.High
}
//<Description>
// Pointers should not be exposed publicly.
//
// This rule detects fields with type *System.IntPtr* or *System.UIntPtr*
// that are public or protected.
//
// Pointers are used to access unmanaged memory from managed code.
// Exposing a pointer publicly makes it easy for malicious code
// to read and write unmanaged data used by the application.
//
// The situation is even worse if the field is not read-only
// since malicious code can change it and force the application
// to rely on arbitrary data.
//</Description>
//<HowToFix>
// Pointers should have the visibility - private or internal.
//
// The estimated Debt, which means the effort to fix such issue,
// is 15 minutes and 10 additional minutes per method using the field outside its assembly.
//
// The estimated Severity of such issue is *Medium*, and *High*
// if the field is non read-only.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Seal methods that satisfy non-public interfaces</Name>
// <Id>ND3106:SealMethodsThatSatisfyNonPublicInterfaces</Id>
warnif count > 0
from m in Application.Methods
where
!m.IsFinal && // If the method is not marked as virtual the flag final is set
m.IsPubliclyVisible &&
!m.ParentType.IsSealed &&
m.ParentType.IsClass
let overridenInterface =
m.OverriddensBase.FirstOrDefault(mb => mb.ParentType.IsInterface && !mb.ParentType.IsPubliclyVisible)
where overridenInterface != null
select new {
m,
overridenInterface = overridenInterface.ParentType,
Debt = 30.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Description>
// A match of this rule represents a virtual method, publicly visible,
// defined in a non-sealed public class, that overrides a method of an
// internal interface.
//
// The interface not being public indicates a process that
// should remain private.
//
// Hence this situation represents a security vulnerability because
// it is now possible to create a malicious class, that derives from
// the parent class and that overrides the method behavior.
// This malicious behavior will be then invoked by private implementation.
//</Description>
//<HowToFix>
// You can:
//
// - seal the parent class,
//
// - or change the accessibility of the parent class to non-public,
//
// - or implement the method without using the *virtual* modifier,
//
// - or change the accessibility of the method to non-public.
//
// If after a careful check such situation doesn't represent
// a security threat, you can suppress the issue.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Review commands vulnerable to SQL injection</Name>
// <Id>ND3107:ReviewCommandsVulnerableToSQLInjection</Id>
warnif count > 0
let commands = ThirdParty.Types.WithFullNameIn(
"System.Data.Common.DbCommand",
"System.Data.SqlClient.SqlCommand",
"System.Data.OleDb.OleDbCommand",
"System.Data.Odbc.OdbcCommand",
"System.Data.OracleClient.OracleCommand")
where commands.Any()
let commandCtors = commands.ChildMethods().Where(m => m.IsConstructor).ToHashSetEx()
let commandExecutes = commands.ChildMethods().Where(m => m.SimpleName.Contains("Execute")).ToHashSetEx()
let commandParameters = commands.ChildMethods().Where(m => m.IsPropertyGetter && m.SimpleName == "get_Parameters").ToHashSetEx()
from m in Application.Methods.UsingAny(commandCtors)
.UsingAny(commandExecutes)
where !m.MethodsCalled.Intersect(commandParameters).Any() &&
// Check also that non of the method call rely on command parameters to get less false positives:
!m.MethodsCalled.SelectMany(mc => mc.MethodsCalled).Intersect(commandParameters).Any()
select new { m,
createA = m.MethodsCalled.Intersect(commandCtors).First().ParentType,
calls = m.MethodsCalled.Intersect(commandExecutes).First(),
Debt = 15.ToMinutes().ToDebt(),
Severity = Severity.High
}
// <Description>
// This rule matches methods that create a DB command
// (like an *SqlCommand* or an *OleDbCommand*)
// that call an *Execute* command method (like *ExecuteScalar()*)
// and that don't use command parameters.
//
// This situation is prone to **SQL Injection** https://en.wikipedia.org/wiki/SQL_injection
// since malicious SQL code might be injected in string parameters values.
//
// However there might be false positives.
// So review carefully matched methods
// and use suppress issues when needed.
//
// To limit the false positives, this rule also checks whether
// command parameters are accessed from any sub method call.
// This is a solid indication of non-vulnerability.
// </Description>
// <HowToFix>
// If after a careful check it appears that the method is indeed
// using some strings to inject parameters values in the SQL query string,
// **command.Parameters.Add(...)** must be used instead.
//
// You can get more information on adding parameters explicitely here:
// https://stackoverflow.com/questions/4892166/how-does-sqlparameter-prevent-sql-injection
// </HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="True" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Review data adapters vulnerable to SQL injection</Name>
// <Id>ND3108:ReviewDataAdaptersVulnerableToSQLInjection</Id>
warnif count > 0
let adapters = ThirdParty.Types.WithFullNameIn(
"System.Data.Common.DataAdapter",
"System.Data.Common.DbDataAdapter",
"System.Data.SqlClient.SqlDataAdapter",
"System.Data.OleDb.OleDbDataAdapter",
"System.Data.Odbc.OdbcDataAdapter",
"System.Data.OracleClient.OracleDataAdapter")
where adapters.Any()
let adapterCtors = adapters.ChildMethods().Where(m => m.IsConstructor).ToHashSetEx()
let adapterFill = adapters.ChildMethods().Where(m => m.SimpleName.Contains("Fill")).ToHashSetEx()
let commands = ThirdParty.Types.WithFullNameIn(
"System.Data.Common.DbCommand",
"System.Data.SqlClient.SqlCommand",
"System.Data.OleDb.OleDbCommand",
"System.Data.Odbc.OdbcCommand",
"System.Data.OracleClient.OracleCommand")
where commands.Any()
let commandParameters = commands.ChildMethods().Where(m => m.IsPropertyGetter && m.SimpleName == "get_Parameters").ToHashSetEx()
from m in Application.Methods.UsingAny(adapterCtors)
.UsingAny(adapterFill)
where !m.MethodsCalled.Intersect(commandParameters).Any() &&
// Check also that non of the method call rely on command parameters to get less false positives:
!m.MethodsCalled.SelectMany(mc => mc.MethodsCalled).Intersect(commandParameters).Any()
select new { m,
createA = m.MethodsCalled.Intersect(adapterCtors).First().ParentType,
calls = m.MethodsCalled.Intersect(adapterFill).First(),
Debt = 15.ToMinutes().ToDebt(),
Severity = Severity.High
}
// <Description>
// This rule matches methods that create a DB adapter
// (like an *SqlDataAdapter* or an *OleDbDataAdapter*)
// that call the method *Fill()*
// and that don't use DB command parameters.
//
// This situation is prone to **SQL Injection** https://en.wikipedia.org/wiki/SQL_injection
// since malicious SQL code might be injected in string parameters values.
//
// However there might be false positives.
// So review carefully matched methods
// and use suppress issues when needed.
//
// To limit the false positives, this rule also checks whether
// command parameters are accessed from any sub method call.
// This is a solid indication of non-vulnerability.
// </Description>
// <HowToFix>
// If after a careful check it appears that the method is indeed
// using some strings to inject parameters values in the SQL query string,
// **adapter.SelectCommand.Parameters.Add(...)** must be used instead
// (or *adapter.UpdateCommand* or *adapter.InsertCommand*, depending on the context).
//
// You can get more information on adding parameters explicitely here:
// https://stackoverflow.com/questions/4892166/how-does-sqlparameter-prevent-sql-injection
// </HowToFix>]]></Query>
</Group>
<Group Name="Code Quality" Active="True" ShownInReport="False">
<Query Active="True" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="True"><![CDATA[// <Name>Types too big - critical</Name>
// <Id>ND1000:AvoidTypesTooBig</Id>
warnif count > 0 from t in JustMyCode.Types where
// First filter on type to optimize
t.NbLinesOfCode > 500
// # IL Instructions is commented, because with LINQ syntax, a few lines of code can compile to hundreds of IL instructions.
// || t.NbILInstructions > 3000
// What matters is the # lines of code in JustMyCode
let locJustMyCode = t.MethodsAndConstructors.Where(m => JustMyCode.Contains(m)).Sum(m => m.NbLinesOfCode)
where locJustMyCode > 500
let isStaticWithNoMutableState = (t.IsStatic && t.Fields.Any(f => !f.IsImmutable))
let staticFactor = (isStaticWithNoMutableState ? 0.2 : 1)
orderby locJustMyCode descending
select new {
t,
locJustMyCode,
t.NbILInstructions,
t.Methods,
t.Fields,
Debt = (staticFactor*locJustMyCode.Linear(200, 1, 2000, 10)).ToHours().ToDebt(),
// The annual interest varies linearly from interest for severity major for 300 loc
// to interest for severity critical for 2000 loc
AnnualInterest = staticFactor*(locJustMyCode.Linear(
200, Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes,
2000, Severity.Critical.AnnualInterestThreshold().Value.TotalMinutes)).ToMinutes().ToAnnualInterest()
}
//<Description>
// This rule matches types with more than 200 lines of code.
// **Only lines of code in JustMyCode methods are taken account.**
//
// Types where *NbLinesOfCode > 200* are extremely complex
// to develop and maintain.
// See the definition of the NbLinesOfCode metric here
// https://www.ndepend.com/docs/code-metrics#NbLinesOfCode
//
// Maybe you are facing the **God Class** phenomenon:
// A **God Class** is a class that controls way too many other classes
// in the system and has grown beyond all logic to become
// *The Class That Does Everything*.
//</Description>
//<HowToFix>
// Types with many lines of code
// should be split in a group of smaller types.
//
// To refactor a *God Class* you'll need patience,
// and you might even need to recreate everything from scratch.
// Here are a few refactoring advices:
//
// • The logic in the *God Class* must be splitted in smaller classes.
// These smaller classes can eventually become private classes nested
// in the original *God Class*, whose instances objects become
// composed of instances of smaller nested classes.
//
// • Smaller classes partitioning should be driven by the multiple
// responsibilities handled by the *God Class*. To identify these
// responsibilities it often helps to look for subsets of methods
// strongly coupled with subsets of fields.
//
// • If the *God Class* contains way more logic than states, a good
// option can be to define one or several static classes that
// contains no static field but only pure static methods. A pure static
// method is a function that computes a result only from inputs
// parameters, it doesn't read nor assign any static or instance field.
// The main advantage of pure static methods is that they are easily
// testable.
//
// • Try to maintain the interface of the *God Class* at first
// and delegate calls to the new extracted classes.
// In the end the *God Class* should be a pure facade without its own logic.
// Then you can keep it for convenience or throw it away and
// start to use the new classes only.
//
// • Unit Tests can help: write tests for each method before extracting it
// to ensure you don't break functionality.
//
// The estimated Debt, which means the effort to fix such issue,
// varies linearly from 1 hour for a 200 lines of code type,
// up to 10 hours for a type with 2.000 or more lines of code.
//
// In Debt and Interest computation, this rule takes account of the fact
// that static types with no mutable fields are just a collection of
// static methods that can be easily splitted and moved from one type
// to another.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="True"><![CDATA[// <Name>Methods too complex - critical</Name>
// <Id>ND1001:MethodsTooComplexCritical</Id>
// Adjusted - orig: m.CyclomaticComplexity > 20 || m.ILCyclomaticComplexity > 60 && m.ILNestingDepth > 2
warnif count > 0 from m in JustMyCode.Methods where
// Don't match async methods here to avoid
// false positives because of special compiler tricks.
!m.IsAsync &&
(m.CyclomaticComplexity > 30 ||
m.ILCyclomaticComplexity > 60) &&
m.ILNestingDepth > 5
let complexityScore = m.NbLinesOfCode/2 + m.CyclomaticComplexity + m.ILCyclomaticComplexity/3 + 3*m.ILNestingDepth
orderby complexityScore descending,
m.CyclomaticComplexity descending,
m.ILCyclomaticComplexity descending,
m.ILNestingDepth descending
select new {
m,
m.NbLinesOfCode,
m.CyclomaticComplexity,
m.ILCyclomaticComplexity,
m.ILNestingDepth,
complexityScore,
Debt = complexityScore.Linear(30, 40, 400, 8*60).ToMinutes().ToDebt(),
// The annual interest varies linearly from interest for severity minor
// to interest for severity major
AnnualInterest = complexityScore .Linear(30, Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes,
200, 2*(Severity.High.AnnualInterestThreshold().Value.TotalMinutes)).ToMinutes().ToAnnualInterest()
}
//<Description>
// This rule matches methods where *CyclomaticComplexity* > 30
// or *ILCyclomaticComplexity* > 60
// or *ILNestingDepth* > 6.
// Such method is typically hard to understand and maintain.
//
// Maybe you are facing the **God Method** phenomenon.
// A "God Method" is a method that does way too many processes in the system
// and has grown beyond all logic to become *The Method That Does Everything*.
// When need for new processes increases suddenly some programmers realize:
// why should I create a new method for each processe if I can only add an *if*.
//
// See the definition of the *CyclomaticComplexity* metric here:
// http://www.ndepend.com/docs/code-metrics#CC
//
// See the definition of the *ILCyclomaticComplexity* metric here:
// http://www.ndepend.com/docs/code-metrics#ILCC
//
// See the definition of the *ILNestingDepth* metric here:
// http://www.ndepend.com/docs/code-metrics#ILNestingDepth
//</Description>
//<HowToFix>
// A large and complex method should be split in smaller methods,
// or even one or several classes can be created for that.
//
// During this process it is important to question the scope of each
// variable local to the method. This can be an indication if
// such local variable will become an instance field of the newly created class(es).
//
// Large *switch…case* structures might be refactored through the help
// of a set of types that implement a common interface, the interface polymorphism
// playing the role of the *switch cases tests*.
//
// Unit Tests can help: write tests for each method before extracting it
// to ensure you don't break functionality.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="True"><![CDATA[// <Name>Methods with too many parameters - critical</Name>
// <Id>ND1002:MethodsWithTooManyParametersCritical</Id>
warnif count > 0 from m in JustMyCode.Methods where
m.NbParameters > 8 &&
// Don't match a method that overrides a third-party method with many parameters
!m.OverriddensBase.Any(mo => mo.IsThirdParty) &&
// Don't match a constructor that calls a base constructor with many parameters
!(m.IsConstructor && m.MethodsCalled.Any(
mc => mc.IsConstructor &&
mc.NbParameters > 8 &&
m.ParentType.DeriveFrom(mc.ParentType))) &&
// Don't match DllImport P/invoke methods with many parameters
!m.HasAttribute("System.Runtime.InteropServices.DllImportAttribute".AllowNoMatch())
orderby m.NbParameters descending
select new {
m,
m.NbParameters,
Debt = m.NbParameters.Linear(8, 1, 40, 6).ToHours().ToDebt(),
// The annual interest varies linearly from interest for severity Medium for 8 parameters
// to interest for severity Critical for 40 parameters
AnnualInterest = m.NbParameters.Linear(8, Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes,
40, Severity.Critical.AnnualInterestThreshold().Value.TotalMinutes).ToMinutes().ToAnnualInterest()
}
//<Description>
// This rule matches methods with 8 or more parameters.
// Such method is painful to call and might degrade performance.
// See the definition of the *NbParameters* metric here:
// https://www.ndepend.com/docs/code-metrics#NbParameters
//
// This rule doesn't match a method that overrides a third-party method
// with 8 or more parameters because such situation is the consequence
// of a lower-level problem.
//
// For the same reason, this rule doesn't match a constructor that calls a
// base constructor with 8 or more parameters.
//</Description>
//<HowToFix>
// More properties/fields can be added to the declaring type to
// handle numerous states. An alternative is to provide
// a class or a structure dedicated to handle arguments passing.
// For example see the class *System.Diagnostics.ProcessStartInfo*
// and the method *System.Diagnostics.Process.Start(ProcessStartInfo)*.
//
// The estimated Debt, which means the effort to fix such issue,
// varies linearly from 1 hour for a method with 7 parameters,
// up to 6 hours for a methods with 40 or more parameters.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Quick summary of methods to refactor</Name>
warnif count > 0 from m in JustMyCode.Methods where
// Code Metrics' definitions
m.NbLinesOfCode > 30 || // http://www.ndepend.com/docs/code-metrics#NbLinesOfCode
// We've commented # IL Instructions, because with LINQ syntax, a few lines of code can compile to hundreds of IL instructions.
// m.NbILInstructions > 200 || // http://www.ndepend.com/docs/code-metrics#NbILInstructions
m.CyclomaticComplexity > 20 || // http://www.ndepend.com/docs/code-metrics#CC
m.ILCyclomaticComplexity > 50 || // http://www.ndepend.com/docs/code-metrics#ILCC
m.ILNestingDepth > 5 || // http://www.ndepend.com/docs/code-metrics#ILNestingDepth
m.NbParameters > 5 || // http://www.ndepend.com/docs/code-metrics#NbParameters
m.NbVariables > 8 || // http://www.ndepend.com/docs/code-metrics#NbVariables
m.NbOverloads > 6 // http://www.ndepend.com/docs/code-metrics#NbOverloads
select new { m, m.NbLinesOfCode, m.NbILInstructions, m.CyclomaticComplexity,
m.ILCyclomaticComplexity, m.ILNestingDepth,
m.NbParameters, m.NbVariables, m.NbOverloads }
//<Description>
// Methods matched by this rule somehow violate
// one or several basic quality principles,
// whether it is too large (too many *lines of code*),
// too complex (too many *if*, *switch case*, loops…)
// has too many variables, too many parameters
// or has too many overloads.
//</Description>
//<HowToFix>
// To refactor such method and increase code quality and maintainability,
// certainly you'll have to split the method into several smaller methods
// or even create one or several classes to implement the logic.
//
// During this process it is important to question the scope of each
// variable local to the method. This can be an indication if
// such local variable will become an instance field of the newly created class(es).
//
// Large *switch…case* structures might be refactored through the help
// of a set of types that implement a common interface, the interface polymorphism
// playing the role of the *switch cases tests*.
//
// Unit Tests can help: write tests for each method before extracting it
// to ensure you don't break functionality.
//</HowToFix>]]></Query>
<Query Active="True" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="True"><![CDATA[// <Name>Methods too big</Name>
// <Id>ND1004:MethodsTooBig</Id>
// Adjusted: Orig - m.NbLinesOfCode > 35
warnif count > 0 from m in JustMyCode.Methods where
// Don't match async methods here to avoid
// false positives because of special compiler tricks.
!m.IsAsync &&
m.NbLinesOfCode > 75
// We've commented # IL Instructions, because with LINQ syntax, a few lines of code can compile to hundreds of IL instructions.
// || m.NbILInstructions > 200
let complexityScore = m.NbLinesOfCode/2 + m.CyclomaticComplexity + m.ILCyclomaticComplexity/3 + 3*m.ILNestingDepth
orderby complexityScore descending,
m.CyclomaticComplexity descending,
m.ILCyclomaticComplexity descending,
m.ILNestingDepth descending
select new {
m,
m.NbLinesOfCode,
m.CyclomaticComplexity,
m.ILCyclomaticComplexity,
m.ILNestingDepth,
complexityScore,
Debt = complexityScore.Linear(30, 40, 400, 8*60).ToMinutes().ToDebt(),
// The annual interest varies linearly from interest for severity minor
// to interest for severity major
AnnualInterest = complexityScore .Linear(30, Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes,
200, 2*(Severity.High.AnnualInterestThreshold().Value.TotalMinutes)).ToMinutes().ToAnnualInterest()
}
//<Description>
// This rule matches methods where *NbLinesOfCode > 30* or
// (commented per default) *NbILInstructions > 200*.
// Such method can be hard to understand and maintain.
//
// However rules like *Methods too complex* or *Methods with too many variables*
// might be more relevant to detect *painful to maintain* methods,
// because complexity is more related to numbers of *if*,
// *switch case*, loops… than to just number of lines.
//
// See the definition of the *NbLinesOfCode* metric here
// http://www.ndepend.com/docs/code-metrics#NbLinesOfCode
//</Description>
//<HowToFix>
// Usually too big methods should be split in smaller methods.
//
// But long methods with no branch conditions, that typically initialize some data,
// are not necessarily a problem to maintain nor to test, and might not need
// refactoring.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Methods too complex</Name>
// Adjusted: orig - m.ILNestingDepth > 4
warnif count > 0 from m in JustMyCode.Methods where
// Don't match async methods here to avoid
// false positives because of special compiler tricks.
!m.IsAsync &&
(m.CyclomaticComplexity > 20 ||
m.ILCyclomaticComplexity > 40 ||
m.ILNestingDepth > 13) // The nesting depth is too high for switch, await, yield statements
let complexityScore = m.NbLinesOfCode/2 + m.CyclomaticComplexity + m.ILCyclomaticComplexity/3 + 3*m.ILNestingDepth
orderby complexityScore descending,
m.CyclomaticComplexity descending,
m.ILCyclomaticComplexity descending,
m.ILNestingDepth descending
select new {
m,
m.NbLinesOfCode,
m.CyclomaticComplexity,
m.ILCyclomaticComplexity,
m.ILNestingDepth,
complexityScore,
Debt = complexityScore.Linear(30, 40, 400, 8*60).ToMinutes().ToDebt(),
// The annual interest varies linearly from interest for severity minor
// to interest for severity major
AnnualInterest = complexityScore .Linear(30, Severity.Medium.AnnualInterestThreshold().Value.TotalMinutes,
200, 2*(Severity.High.AnnualInterestThreshold().Value.TotalMinutes)).ToMinutes().ToAnnualInterest()
}
//<Description>
// This rule matches methods where *CyclomaticComplexity > 20*
// or *ILCyclomaticComplexity > 40*
// or *ILNestingDepth > 4*.
// Such method is typically hard to understand and maintain.
//
// See the definition of the *CyclomaticComplexity* metric here:
// http://www.ndepend.com/docs/code-metrics#CC
//
// See the definition of the *ILCyclomaticComplexity* metric here:
// http://www.ndepend.com/docs/code-metrics#ILCC
//
// See the definition of the *ILNestingDepth* metric here:
// http://www.ndepend.com/docs/code-metrics#ILNestingDepth
//</Description>
//<HowToFix>
// A large and complex method should be split in smaller methods,
// or even one or several classes can be created for that.
//
// During this process it is important to question the scope of each
// variable local to the method. This can be an indication if
// such local variable will become an instance field of the newly created class(es).
//
// Large *switch…case* structures might be refactored through the help
// of a set of types that implement a common interface, the interface polymorphism
// playing the role of the *switch cases tests*.
//
// Unit Tests can help: write tests for each method before extracting it
// to ensure you don't break functionality.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Methods potentially poorly commented</Name>
warnif count > 0 from m in JustMyCode.Methods where
m.PercentageComment < 20 &&
m.NbLinesOfCode > 20
orderby m.PercentageComment ascending
select new { m, m.PercentageComment, m.NbLinesOfCode, m.NbLinesOfComment }
//<Description>
// This rule matches methods with less than 20% of comment lines and that have
// at least 20 lines of code. Such method might need to be more commented.
//
// See the definitions of the *Comments metric* here:
// http://www.ndepend.com/docs/code-metrics#PercentageComment
// http://www.ndepend.com/docs/code-metrics#NbLinesOfComment
//</Description>
//<HowToFix>
// Typically add more comment. But code commenting is subject to controversy.
// While poorly written and designed code would needs a lot of comment
// to be understood, clean code doesn't need that much comment, especially
// if variables and methods are properly named and convey enough information.
// Unit-Test code can also play the role of code commenting.
//
// However, even when writing clean and well-tested code, one will have
// to write **hacks** at a point, usually to circumvent some API limitations or bugs.
// A hack is a non-trivial piece of code, that doesn't make sense at first glance,
// and that took time and web research to be found.
// In such situation comments must absolutely be used to express the intention,
// the need for the hacks and the source where the solution has been found.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Methods with too many parameters</Name>
warnif count > 0 from m in JustMyCode.Methods where
m.NbParameters > 5
orderby m.NbParameters descending
select new { m, m.NbParameters }
//<Description>
// This rule matches methods with more than 5 parameters.
// Such method might be painful to call and might degrade performance.
// See the definition of the *NbParameters* metric here:
// http://www.ndepend.com/docs/code-metrics#NbParameters
//</Description>
//<HowToFix>
// More properties/fields can be added to the declaring type to
// handle numerous states. An alternative is to provide
// a class or a structure dedicated to handle arguments passing.
// For example see the class *System.Diagnostics.ProcessStartInfo*
// and the method *System.Diagnostics.Process.Start(ProcessStartInfo))*.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Methods with too many local variables</Name>
warnif count > 0 from m in JustMyCode.Methods where
m.NbVariables > 15
orderby m.NbVariables descending
select new { m, m.NbVariables }
//<Description>
// This rule matches methods with more than 15 variables.
//
// Methods where *NbVariables > 8* are hard to understand and maintain.
// Methods where *NbVariables > 15* are extremely complex and must be refactored.
//
// See the definition of the *Nbvariables* metric here:
// http://www.ndepend.com/docs/code-metrics#Nbvariables
//</Description>
//<HowToFix>
// To refactor such method and increase code quality and maintainability,
// certainly you'll have to split the method into several smaller methods
// or even create one or several classes to implement the logic.
//
// During this process it is important to question the scope of each
// variable local to the method. This can be an indication if
// such local variable will become an instance field of the newly created class(es).
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Methods with too many overloads</Name>
warnif count > 0 from m in JustMyCode.Methods where
m.NbOverloads > 6 &&
!m.IsOperator // Don't report operator overload
orderby m.NbOverloads descending
let overloads =
m.IsConstructor ? m.ParentType.Constructors :
m.ParentType.Methods.Where(m1 => m1.SimpleName == m.SimpleName)
select new { m, overloads }
//<Description>
// Method overloading is the ability to create multiple methods of the same name
// with different implementations, and various set of parameters.
//
// This rule matches sets of method with more than 6 overloads.
//
// Such method set might be a problem to maintain
// and provokes higher coupling than necessary.
//
// See the definition of the *NbOverloads* metric here
// http://www.ndepend.com/docs/code-metrics#NbOverloads
//</Description>
//<HowToFix>
// Typically the *too many overloads* phenomenon appears when an algorithm
// takes a various set of in-parameters. Each overload is presented as
// a facility to provide a various set of in-parameters.
// In such situation, the C# and VB.NET language feature named
// *Named and Optional arguments* should be used.
//
// The *too many overloads* phenomenon can also be a consequence of the usage
// of the **visitor design pattern** http://en.wikipedia.org/wiki/Visitor_pattern
// since a method named *Visit()* must be provided for each sub type.
// In such situation there is no need for fix.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Types with too many methods</Name>
warnif count > 0 from t in JustMyCode.Types
// Optimization: Fast discard of non-relevant types
where t.Methods.Count() > 20
// Don't match these methods
let methods = t.Methods.Where(
m => !(m.IsGeneratedByCompiler ||
m.IsConstructor || m.IsClassConstructor ||
m.IsPropertyGetter || m.IsPropertySetter ||
m.IsEventAdder || m.IsEventRemover))
where methods.Count() > 20
orderby methods.Count() descending
select new { t,
nbMethods = methods.Count(),
instanceMethods = methods.Where(m => !m.IsStatic),
staticMethods = methods.Where(m => m.IsStatic)}
//<Description>
// This rule matches types with more than 20 methods.
// Such type might be hard to understand and maintain.
//
// Notice that methods like constructors or property
// and event accessors are not taken account.
//
// Having many methods for a type might be a symptom
// of too many responsibilities implemented.
//
// Maybe you are facing the **God Class** phenomenon:
// A **God Class** is a class that controls way too many other classes
// in the system and has grown beyond all logic to become
// *The Class That Does Everything*.
//</Description>
//<HowToFix>
// To refactor such type and increase code quality and maintainability,
// certainly you'll have to split the type into several smaller types
// that together, implement the same logic.
//
// To refactor a *God Class* you'll need patience,
// and you might even need to recreate everything from scratch.
// Here are a few advices:
//
// • Think before pulling out methods:
// What responsibility does it have?
// Can you isolate some subsets of methods that operate on the same subsets of fields?
//
// • Try to maintain the interface of the god class at first
// and delegate calls to the new extracted classes.
// In the end the god class should be a pure facade without own logic.
// Then you can keep it for convenience
// or throw it away and start to use the new classes only.
//
// • Unit Tests can help: write tests for each method before extracting it
// to ensure you don't break functionality.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Types with too many fields</Name>
warnif count > 0 from t in JustMyCode.Types
// Optimization: Fast discard of non-relevant types
where !t.IsEnumeration &&
t.Fields.Count() > 20
// Count instance fields and non-constant static fields
let fields = t.Fields.Where(f =>
!f.IsGeneratedByCompiler &&
!f.IsLiteral &&
!(f.IsStatic && f.IsInitOnly))
where fields.Count() > 20
orderby fields.Count() descending
select new { t,
instanceFields = fields.Where(f => !f.IsStatic),
staticFields = fields.Where(f => f.IsStatic),
// See definition of Size of Instances metric here:
// http://www.ndepend.com/docs/code-metrics#SizeOfInst
t.SizeOfInst }
//<Description>
// This rule matches types with more than 20 fields.
// Such type might be hard to understand and maintain.
//
// Notice that constant fields and static-readonly fields are not counted.
// Enumerations types are not counted also.
//
// Having many fields for a type might be a symptom
// of too many responsibilities implemented.
//</Description>
//<HowToFix>
// To refactor such type and increase code quality and maintainability,
// certainly you'll have to group subsets of fields into smaller types
// and dispatch the logic implemented into the methods
// into these smaller types.
//</HowToFix>]]></Query>
<Query Active="False" DisplayList="True" DisplayStat="False" DisplaySelectionView="False" IsCriticalRule="False"><![CDATA[// <Name>Types with poor cohesion</Name>
warnif count > 0 from t in JustMyCode.Types where
(t.LCOM > 0.8 || t.LCOMHS > 0.95) &&
t.NbFields > 10 &&
t.NbMethods >10
orderby t.LCOM descending, t.LCOMHS descending
select new { t, t.LCOM, t.LCOMHS,
t.NbMethods, t.NbFields }
//<Description>
// This rule is based on the *LCOM code metric*,
// LCOM stands for **Lack Of Cohesion of Methods**.
// See the definition of the LCOM metric here
// http://www.ndepend.com/docs/code-metrics#LCOM
//
// The LCOM metric measures the fact that most methods are using most fields.
// A class is considered utterly cohesive (which is good)
// if all its methods use all its instance fields.
//
// Only types with enough methods and fields are taken account to avoid bias.
// The LCOM takes its values in the range [0-1].
//
// This rule matches types with LCOM higher than 0.8.