

Clean Code Tip: Avoid using too many Imports in your classes
source link: https://www.code4it.dev/cleancodetips/too-many-imports
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Clean Code Tip: Avoid using too many Imports in your classes

Actually, this article is not about a tip to write cleaner code, but it is an article that aims at pointing out a code smell.
Of course, once you find this code smell in your code, you can act in order to eliminate it, and, as a consequence, you will end up with cleaner code.
The code smell is easy to identify: open your classes and have a look at the imports list (in C#, the using
on top of the file).
A real example of too many imports
Here's a real-life example (I censored the names, of course):
using MyCompany.CMS.Data;
using MyCompany.CMS.Modules;
using MyCompany.CMS.Rendering;
using MyCompany.Witch.Distribution;
using MyCompany.Witch.Distribution.Elements;
using MyCompany.Witch.Distribution.Entities;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using MyProject.Controllers.VideoPlayer.v1.DataSource;
using MyProject.Controllers.VideoPlayer.v1.Vod;
using MyProject.Core;
using MyProject.Helpers.Common;
using MyProject.Helpers.DataExplorer;
using MyProject.Helpers.Entities;
using MyProject.Helpers.Extensions;
using MyProject.Helpers.Metadata;
using MyProject.Helpers.Roofline;
using MyProject.ModelsEntities;
using MyProject.Models.ViewEntities.Tags;
using MyProject.Modules.EditorialDetail.Core;
using MyProject.Modules.VideoPlayer.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace MyProject.Modules.Video
Sounds familiar?
If we exclude the imports necessary to use some C# functionalities
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
We have lots of dependencies on external modules.
This means that if something changes in one of the classes that are part of those namespaces, we may end up with code that is difficult to update.
Also, guess what comes with all those imports? Constructor with too many parameters (and, in fact, in this class, I have 11 dependencies injected in the constructor) and code that is too long and difficult to understand (and, in fact, this class has 500+ lines).
A solution? Refactor your project in order to minimize scattering those dependencies.
Wrapping up
Having all those imports (in C# we use the keyword using
) is a good indicator that your code does too many things. You should focus on minimizing those imports without cheating (like using global imports).
Happy coding!
Published under Clean Code, Clean Code Tip on 19 April, 2022.
Recommend
-
11
The Many Benefits of Teaching Private Classes Daniel de Groot - April 26, 2020 Private classes a...
-
17
11 Jul 2021•1min readSnippetsHow to avoid using relative path imports in Next.jsDoes this look familiar? 🤯javascriptimport M...
-
8
TIL: Avoid using too many MATCH() functions in Excel Friday, February 28, 2014 Yet another interesting finding in Excel. Today, I tried INDEX+MATCH to track and filter data...
-
6
Benefits Better UX Breaks your JavaScript bundle into multiple modules that intelligently load only when a user uses that functionality. Loading and wait times are improved app...
-
6
How to avoid lifetime annotations in Rust (and write clean code) Tue, Aug 31, 2021 Lifetime annotations are one of the things that distract the most new rustaceans. From my experience writing an...
-
8
Copy link Contributor inquisitivecrystal
-
7
Platform Specific Code using Partial Classes in .NET MAUITable of contentsWhen writing a cross platform app it is common to need some pl...
-
7
Why git add & amp; removes the Storyboard & lt; classes & gt; Many times? advertisements When saving the storyb...
-
8
Contens If you ever had to plot point clouds with more than say ten classes, you’ve probably run into this problem before. You need to assign colors to many classes such that they all appear distinct...
-
6
Inconsistent JavaScript imports can lead to duplicate code
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK