Refactoring recommendations with local variables

I spent some time refactoring my C # code, and I was struck by how large my parameter lists for local variables are, especially when you start getting several levels deep and need to skip local variables above the call stack.

As an example, I have a rather complicated code that uses Linq for sql. I create a data context at an early stage of the procedure and use it throughout the procedure. However, after refactoring, I found that I pass this DC through all my sub-methods along with various other state variables.

One solution, of course, is to turn these local variables into member variables, but that makes the whole class safe for streaming, and when working with asynchronous I / O, it means distorting things with locks and mutexes to make their safe.

What are your best practices for factoring against local variables? Do you give up and make them members? or do you carry state baggage? Or are you doing something else?

EDIT: I'm not sure you need more. I do not want to dump a bunch of code, because by nature, to illustrate this, I need to show a very complex set of procedures.

I have a number of local variables, such as Linq to Sql DC, various processing steps, various stages of updating a large amount of raw data processed and written to the database.

I thought about creating a context condition and its transmission, but for me it seems to be a hacker, though, I believe that is exactly what the Linq to SQL dc.

+3
source share
3 answers

Potentially make them members of a different type. It also works well nested types. It makes a lot of sense, if they form a logical group, and allows you to write several methods of using the same type.

In addition, we will need to learn more about what you are doing, to comment further, I suspect.

+3
source

-/. , , - , , .

, , .

+4

. , .

, , . 1 2 , , , , .

+2

Source: https://habr.com/ru/post/1702998/


All Articles