Open-Source Code Quality - ChoETL

How important is code quality to your team? It should be at the top of the list, not only to make your customers happy but make your team happier when bugs arise and when features need to be added. Putting quality in your code in the future is a lot more expensive than doing it when the code is first written. How does your open-source solution compare to some of the most popular GitHub/ NuGet packages? Do you analyze repositories before using them or just adding them via NuGet? If not, you need to keep reading.
 

ChoETL

Code Quality Rating

8.08

 
Cinchoo ETL is a .NET library for reading and writing CSV, Fixed Length, XML, and JSON files. It supports reading and writing of custom POCO class objects. I am currently using ChoETL in a project where via ASP.NET, we allow customers to download and upload CSV files. Here are the results of my code analysis. I would like to note that I could not calculate unit test coverage since 157 tests are broken. The code quality rating shows there is a code violation every 8.08 lines of code.
 
Errors
Warnings
Information
Unit Test Coverage
0
65,074
14,568
UNKNOWN – Broken
Maintainability Index
Cyclomatic Complexity
Depth of Inheritance
Class Coupling
88
147,107
2,229
70,464
Lines of Source Code
Lines of Executable Code
Lines of Cloned Code
Code Commenting
643,902
231,796
22,190
Grade of F
 

Coding Standard Issues

 
Here are just some examples of what needs to be fixed from the near 80K violations.
 
Using string.Empty
 
Here is a common issue I see in most code bases I analyze,
  1. ele.Add(new XAttribute(kvp.Key.Replace("@"""), value));  
The issue here is that they are using ""which can affect performance. Instead usestring.Empty. Here is how it should look,
  1. ele.Add(new XAttribute(kvp.Key.Replace("@"string.Empty), value));  
Variable Naming
 
Here is an issue of local variable names.
  1. ChoFallbackValueAttributeFallbackValueAttribute = (from a in mi.Attributes.AsTypedEnumerable<Attribute>() where typeof(ChoFallbackValueAttribute).IsAssignableFrom(a.GetType())select a).FirstOrDefault() as ChoFallbackValueAttribute;  
The issue is that local variable names should start with lower-case letters. Here is how I would fix it.
  1. var fallbackValueAttribute = (from a in mi.Attributes.AsTypedEnumerable<Attribute>() where typeof(ChoFallbackValueAttribute).IsAssignableFrom(a.GetType()) select a).FirstOrDefault() as ChoFallbackValueAttribute;  
Missing Accessibility Modifiers
 
All properties, methods, fields, events, etc. should have proper accessibility modifiers. Here is an example of the violation.
  1. ChoYamlRecordFieldConfigurationfieldConfig = null;  
It should be coded like this (includes proper naming).
  1. private ChoYamlRecordFieldConfiguration _fieldConfig;  
Braces Placement
 
Another common issue I see in code is the improper use of braces, usually not using them at all like in this example.
  1. if (methodInfo != null)  
  2.     return methodInfo.Invoke(target, args);  
  3. else  
  4.     throw new ApplicationException(String.Format("Can't find {0} method in {1} type.", name, target.GetType().FullName));  
Here is how it should have been coded.
  1. if (methodInfo != null)  
  2. {  
  3.     return methodInfo.Invoke(target, args);  
  4. }  
  5. else  
  6. {  
  7.     throw new ApplicationException(string.Format("Can't find {0} method in {1} type.", name, target.GetType().FullName));  
  8. }  
The other issue with this code is the use of ApplicationException. Developers should never throw this exception since it is reserved for .NET. They should have used a different one or created a custom one inheriting Exception.
 
Proper braces should be used for foreach statements. Here is an example of the violation.
  1. foreach (object rec1 in buffer)  
  2.     yield return new ChoDynamicObject(MigrateToNewSchema(rec1 as IDictionary<string, object>, recFieldTypes));  
Here is how to fix this.
  1. foreach (var record in buffer)  
  2. {  
  3.     yield return new ChoDynamicObject(this.MigrateToNewSchema(record as IDictionary<stringobject>, recFieldTypes));  
  4. }  
This foreach could improve its performance by changing it to use for as shown in this example.
  1. for (var recordCount = 0; recordCount<buffer.Count; recordCount++)  
  2. {  
  3.     yield return new ChoDynamicObject(this.MigrateToNewSchema((IDictionary<stringobject>)buffer[recordCount] as IDictionary<stringobject>, recFieldTypes));  
  4. }  
Prefix Local Calls
 
When using local calls, they should be prefixed with either this or base. Here is the violation.
  1. public Type RecordMapType  
  2. {  
  3.     get { return _recordMapType == null ? RecordType : _recordMapType; }  
  4.     set { _recordMapType = value; }  
  5. }  
Here is how to fix the issue.
  1. public Type RecordMapType  
  2. {  
  3.     get  
  4.     {  
  5.         return this._recordMapType == null ? this.RecordType : this._recordMapType;  
  6.     }  
  7.     set  
  8.     {  
  9.     this._recordMapType = value;  
  10.     }  
  11. }  
Remove Dead Code
 
Before committing code to main branches, make sure to remove any code that is not being used anymore. Part of this is to remove any code that is commented out, which this codebase has a lot. Also, remove any classes, class members, and parameters that are not being used. For example, the code below has an unused parameter.
  1. private object DeserializeNode(object yamlNode, Type type, ChoYamlRecordFieldConfiguration config)  
  2. {  
  3.     if (_fieldConfig.ItemConverter != null)  
  4.     return RaiseItemConverter(config, yamlNode);  
  5.     else  
  6.     return yamlNode;  
  7. }  
The type parameters are not’ used in this code. Here is how I would fix it.
  1. private object DeserializeNode(object yamlNode, ChoYamlRecordFieldConfiguration config) {  
  2.     return this._fieldConfig.ItemConverter != null ? this.RaiseItemConverter(config, yamlNode) : yamlNode;  
  3. }  
Don’t Set Variables to Their Default State
 
Again, I see many developers setting variables to their default state. The biggest issue is that it can cause performance issues. Here is an example.
  1. string colName = null;  
  2. Type colType = null;  
  3. int startIndex = 0;  
  4. int fieldLength = 0;  
To fix, just remove the assignments like in this example.
  1. string colName;  
  2. Type colType;  
  3. int startIndex;  
  4. int fieldLength;  
Use Object Initialization
 
To properly initialize property values in an object, use object initialization. Here is an example of code not using this.
  1. var obj = new ChoXmlRecordFieldConfiguration(fn, xPath: $"./{fn1}");  
  2. obj.FieldName = fn1;  
  3. obj.IsXmlAttribute = true;  
This is how you would use object initialization.
  1. var obj = new ChoXmlRecordFieldConfiguration(fieldName, xPath: $"./{fieldName1}")  
  2. {  
  3.    FieldName = fieldName1,  
  4.    IsXmlAttribute = true  
  5. };  
Other issues include class members are not ordered correctly. Very little code documentation which means IntelliSense does not work for any of the methods. Many of the projects have references that are not being used anymore. It uses obsolete methods. Classes should be in separate files and more.
 

Summary

 
While there are not any errors in this codebase (except for unit tests), the biggest issues are,
  1. IntelliSense is missing for all the methods and classes. This makes it much harder to use. I had to spend too much time trying to figure things out.
  2. Poor or inconsistent coding standards produce “spaghetti” code.
  3. It needs work to improve performance.
  4. It needs better examples on how to use it online. For example, I could not find any examples of how to import a CSV from the ASP.NET file stream. All examples showed getting the file from a saved location, something that you should always try to avoid in ASP.NET due to IO performance issues.
  5. There are over 22K lines of potential duplicate code… a maintenance nightmare!
To learn the proper way of implementing coding standards for .NET, please pick up a copy of my coding standards book on Amazon!
 
I hope that the next time you want to download a NuGet package for your solution or use an open-source library, you will analyze it first for code quality. I plan to analyze more libraries in the future as a blog post. What library would you like me to analyze? Please comment below.