Home Update How to refactor God objects in C#

How to refactor God objects in C#

355


In object-oriented programming (OOP), so-called God objects are an anti-pattern as a result of they violate numerous good design rules. God objects are objects of lessons that know an excessive amount of or do an excessive amount of, and therefore make your code tightly coupled and tough to keep up. This article talks about this anti-pattern and how one can eliminate it.

To work with the code examples supplied on this article, you need to have Visual Studio 2019 put in in your system. If you don’t have already got a duplicate, you’ll be able to obtain Visual Studio 2019 right here.

Create a console software mission in Visual Studio

First off, let’s create a .NET Core console software mission in Visual Studio. Assuming Visual Studio 2019 is put in in your system, observe the steps outlined under to create a brand new .NET Core console software mission in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, choose “Console App (.NET Core)” from the record of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window proven subsequent, specify the title and placement for the brand new mission.
  6. Click Create.

This will create a brand new .NET Core console software mission in Visual Studio 2019. We’ll use this mission within the subsequent sections of this text.

What is a God object?

Writing code with out following the perfect practices may result in code that’s exhausting to debug, prolong, and keep over time. Object-oriented programming has change into extraordinarily common over the previous few a long time as a result of it’s adept at serving to you to eradicate poorly written code out of your software. However, anti-patterns may typically emerge in the event you take shortcuts and your main goal is getting the duty achieved quite than getting it achieved the appropriate method.

Whereas a design sample is a confirmed resolution to a standard design downside in software program improvement, an anti-pattern is an ineffective sample that reveals us how to not resolve an issue, as a result of doing so would lead to poor design. In different phrases, an anti-pattern is a standard resolution to a given downside that may have unfavourable penalties — an answer that’s ineffective or counterproductive in observe. A God object is one such anti-pattern.

A God object accommodates cluttered code that’s tough to keep up, prolong, use, take a look at, and combine with different elements of the applying. God objects violate the single accountability precept and the Law of Demeter. I’ll focus on the…



Source hyperlink

LEAVE A REPLY

Please enter your comment!
Please enter your name here