Rule Definition
Utility classes are not meant to be instantiated because all the functionalities that they provide are accessible without instantiating the class. Instantiating these classes means that the developer has effectively misued the class. It also consumes memory unnecessarily. Providing a private default constructor ensures that the class is not instantiated.
Remediation
Add a private default constructor to ensure that the class can't be instantiated.
Violation Code Sample
public class MyUtilityClass
{
static public bool MyUtilityFunction1() {
//Do Something Usefull
return true;
}
} } // VIOLATION
Fixed Code Sample
public class MyUtilityClass
{
static public bool MyUtilityFunction1() {
//Do Something Usefull
return true;
}
private MyUtilityClass() { // FIXED
// Avoid instantiation of the class
}
}
Related Technologies
.Net
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.