C
C#5mo ago
Mr. Roach

How do I reduce class coupling? Is class decoupling always necessary?

Hello, yesterday I discovered the code metrics feature in vs. The Microsoft doc says "9" would be the "magic number" regarding class coupling. When I looked at my metrics results, I discovered I got a class with 43 class coupling score, where a single method holds 27 of the score and some more classes with 20+ class coupling score. I mean everything works fine at the moment, so theres no issue needed to be fixed that stops my code from working but since ms docs seem to say good metric scores indicate better manageable and less subsceptible code, I thought it couldn't harm to ask. When I googled I got results mentioning implementing interfaces but dont even know if I got pointed to the right direction by that. As far as I understood, one could make an Interface ICar whichs content would be like a template classes that use ICar would have to implement as well which means one could make different types of car-classes with i.E different behaving Drive methods if I understood correctly. So if I understood those google/ms doc examples correctly til here, I still don't know how I would apply Interfaces onto my classes that I'm don't really even able to compare to that examples I read. The class with a 43 score is a class that handles an import process for csv files into a sqlite database. The method with 27 score is ProcessCsvFileAsync. https://github.com/MisterRoach/WoW_AH_Data_Project/blob/master/WoW_AH_Data_Project/Database/DatabaseImportCsvs.cs
12 Replies
Jimmacle
Jimmacle5mo ago
where does it say how that metric is calculated? in general yes, decoupling makes code more maintainable but like everything it's not always strictly neccessary and can also be overdone
Anton
Anton5mo ago
the concerns are "what if it's not csv but some other format" and "what if it's not sqlite but some other dbms" the method works on a higher level -- load data from some file to some db
PixxelKick
PixxelKick5mo ago
I only decouple stuff when there's a reason to do it. Decoupling amd abstraction "just cuz" produces a bunch of pointless code bloat and effectively doubles the amount of files you now need to maintain, and obfuscates the chain of logic.
Anton
Anton5mo ago
it's not as straightforward to do, but that's the idea they push
Mr. Roach
Mr. RoachOP5mo ago
Thanks for your responses! Thanks for Input. How exactly do you mean that with concerns and higher level Method? The method is based on a GUI interaction where csv Files are the only valid outcomes in the selection. The sqlite comes with the App... do you mean it in the way that in a way that the only Option to adapt the Code to changing circumstances is by changing it as a whole? Based on your input I came to the idea "what if User already made an excel file and wants to Import that". Naively i would write separate code for the case. Not sure how i would act when i should decide to Change the dbms Hmm..
Anton
Anton5mo ago
I never said that's always appropriate, I just said that's what they push But you'd work with input and output interfaces basically, you'll have to have a lot more abstraction if you want to support that
Jimmacle
Jimmacle5mo ago
as an example, i have a program at work that has to do calculations on data that can come from several sources
Anton
Anton5mo ago
You'd need a thing that's an abstraction for some common set of things and some general way of describing data that can work for any format
Jimmacle
Jimmacle5mo ago
the calculations never change, but the code that reads the data does
Anton
Anton5mo ago
then you'd write your db code in a way that it accepts this general thing
Jimmacle
Jimmacle5mo ago
so those 2 parts of the problem are split into different classes in my code that don't know about each other one part reads the data into a list of records, the other part does the calculations on that list
Anton
Anton5mo ago
and then the code of that function would be just passing the thing to the database or something like that but only do that if you need to do that if you try to generalize too much you might very likely find out later that your abstractions don't fit your use case and will have to throw away the work
Want results from more Discord servers?
Add your server