Moonglade

Modern Markdown blogging with AI-powered localization and search.

Tag: Code Quality


Using Moq Events to Discuss the Importance of Unit Testing

The prominent .NET unit testing library Moq sparked intense community controversy after it was discovered that the library scans local Git configurations during the build process and sends users' email addresses to check their sponsorship status. This led some developers to condemn the author and resulted in the emergence of alternative solutions such as CleanMoq. Using this incident as a starting point, the article delves into the core value of unit testing, pointing out that it can significantly enhance code quality, maintainability, and refactoring efficiency. Conversely, the absence of unit testing can lead to a vicious cycle of fragile code, increased project risks, and reduced development efficiency. Although this incident did not trigger widespread discussion domestically, the author reflects on the fact that small and medium-sized companies in the region generally lack a habit of writing unit tests. This is attributed to cost considerations and time pressures, which ultimately result in declining software quality. The article calls on local developers and enterprises to place greater importance on unit testing, breaking the dilemma where a lack of tests prevents refactoring and code rot slows down development. The author also plans to share introductory tutorials on unit testing in the future.

Views 206
.NET Code Quality Software Development Software Development Methodology Test unit testing

C# required: Say goodbye to null reference exceptions with strings

The article discusses the issue of null reference exceptions for strings in C# and presents solutions. The author points out that although Tony Hoare famously referred to the introduction of null as a "billion-dollar mistake," in practical development, `string` is often misused as a value type despite being a reference type. This misuse leads to the default `null` value triggering `NullReferenceException`. Prior to C# 11, developers typically used `null!` to force non-null status or declared variables as `string?` to suppress warnings. However, this approach often masks the underlying inconsistency between database fields that can be null and code logic that expects non-null values, making it highly susceptible to causing production incidents in teams lacking rigorous testing. The `required` keyword introduced in C# 11 offers a superior solution by mandating that non-null strings be assigned during object initialization, thereby eliminating null reference risks at compile time. Finally, using this technical topic as a springboard, the article criticizes certain teams that adopt an "outsourcing model" of compressing working hours to pursue performance metrics. The author argues that this practice sacrifices software quality; not only does it fail to genuinely accelerate product iteration, but it also leads to user churn due to code defects, ultimately undermining the long-term competitiveness of their own products.

Views 195
C# Code Quality Engineering Practices Nullables Software Product Management required keyword

Which One Is Right for You? A Guide to Choosing Between IEnumerable, IQueryable, and ICollection

Which One Is Right for You? A Guide to Choosing Between IEnumerable, IQueryable, and ICollection

This article provides an in-depth analysis of the selection strategies for collection interfaces in C#, with a focus on the characteristics and application scenarios of `IEnumerable<T>`, `IQueryable<T>`, and `IReadOnlyCollection<T>`. The article points out that blindly using `IEnumerable<T>` can easily lead to performance issues or logical errors caused by multiple iterations, such as the repeated execution of asynchronous tasks. Although `IQueryable<T>` can improve ORM query efficiency, it carries the risk of abstraction leakage and should not be used as the return type of public methods. It is recommended to use it internally within the Repository layer and convert it using `AsEnumerable()`. To enhance code maintainability and prevent callers from inadvertently modifying the data source, it is recommended that methods return `IReadOnlyCollection<T>` or `IReadOnlyList<T>`. This not only clarifies the contract of collection immutability but also avoids resource release anomalies caused by deferred evaluation. Summary recommendation: Prioritize `IEnumerable<T>` for input parameters, while preferring read-only interfaces when returning collections, to balance flexibility, performance, and security.

Views 196
.NET C# Code Quality IEnumerator IQueryable Lazy Evaluation