Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m encountering a lot of methods in my project that have a bunch of out parame

ID: 643361 • Letter: I

Question

I'm encountering a lot of methods in my project that have a bunch of out parameters embedded in them and its making it cumbersome to call the methods as I have to start declaring the variables before calling the methods.

As such, I would like to refactor the code to return a struct instead and was wondering if this is a good idea.

One of the examples from an interface:

void CalculateFinancialReturnTotals(FinancialReturn fr, out decimal expenses, out decimal revenue, out decimal levyA, out decimal levyB, out decimal profit, out decimal turnover, out string message)
and if I was to refactor that, i would be putting all the out parameters in the struct such that the method signature is much simpler as below.

[structName] CalculateFinancialReturnTotals(FinancialReturn fr);
Please advise.

Explanation / Answer

This is pretty much a non-question to me. Yes, what you suggest is most likely an improvement over the current design.

The overall best approach is dependent on the context though and without knowing the broader context - just that isolated example - one cannot recommend much beyond what you have already thought of.

For example, what other methods are there? Do they have some common denominator (I mean similar set of out parameters); can you reuse this struct (good) or would you have to create many different structs (bad)?

And why a struct, not a class? Remember that structs can't be inherited.

The name of this method - CalculateFinancialReturnTotals - is a code smell in its own right to me. My feel is that CalculateTotals should be sufficient. If it's not obvious that it deals with financial returns, the overall design might be flawed somehow. But it's just guessing.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote