Asia

Alternatives for Common Anti-Patterns

12 January 2022

Anti-patterns have a negative impact on the maintainability of a software systems. These are nothing but a poor solution to recurring design problem and generally occurs when developers unknowingly introduce them while designing and implementing the features to the product.

So in this blog we will try to replace the negative aspects of the anti-pattern with something that will give a positive outcome.

Using FirstOrDefault and Any

Try avoiding to call Any() or Count > 0 before foreach() such as in the following code:

Alternatives for Common Anti-Patterns

Prefer IEnumerable<>.Any to List<>.Exists

When manipulating IEnumerable, prefer to use Any() instead of ToList().Exists(). Check out the following code:

Alternatives for Common Anti-Patterns

Becomes:

Alternatives for Common Anti-Patterns

Prefer Any() over Count() while checking for empty

The iteration on the sequence stops as soon as the condition (if any) is fulfilled without allocating any temporary list therefore any extension methods should be preferred to count computation on IEnumerable.

Alternatives for Common Anti-Patterns

Becomes:

Alternatives for Common Anti-Patterns

Using Throw Ex

While planning to play with catch and re-throw an exception, then using throw ex will not preserve the exception call stack and that can be a big blunder. So this can be avoided by using simple throw syntax to preserve the exception call stack.

Alternatives for Common Anti-Patterns

Becomes:

Alternatives for Common Anti-Patterns

USING T CASTING

Generally casting has a negative impact because casted objects can be easily castable so to protect casted objects from turning into castable following use as for casting given as follows.

Alternatives for Common Anti-Patterns

Becomes:

Alternatives for Common Anti-Patterns

Tudip Technologies

Tudip Technologies is a value-driven Software Services company. We deliver excellence in dedicated and business-centric web solutions incorporated with innovation and expertise as the tools to achieve business goals. Our high-quality, cost-effective, reliable, and scalable services have helped us build an enduring relationship with our clients and customers.

Latest Posts

  • All Posts
  • Casestudy
  • Cloud
  • Healthcare
  • Ios
  • IOT
  • Java
  • Manufacturing
  • News
  • OTT
  • Supply Chain
  • Support and Maintainance
    •   Back
    • Banking
Cloud-Powered Agility

December 27, 2024

27 December 2024 Introduction: Transforming Chaos into Opportunity Imagine a supply chain where disruptions are predicted before they occur, inventory…

Cloud-Powered Agility

December 24, 2024

Imagine a supply chain where disruptions are predicted before they occur, inventory is optimized in real-time, and every stakeholder works…

Related Posts

  • All Posts
  • Healthcare