Skip to main content
added 25 characters in body
Source Link

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to use ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The applicationapplication processing unit is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to use ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The application is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to use ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The application processing unit is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?

added 4 characters in body
Source Link

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to use ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The application is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The application is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to use ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The application is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?

Source Link

Using ThreadLocal in Java

In one of my projects, I have this following use case -

I have a variable, that I need to pass around in many methods. Business logic and object creation in those methods are dependent on that variable. It can be the case I have object creation inside a constructor of another class. For those, I am also propagating this variable so that nested object creation can use it.

This way method contract is very clear. Anyone can look at the method and readily know that is the information that this method is working with. But this leads to a messy propagation of the variable in many places across the codebase. Depending on the variation of the use case, sometimes it has to be stored as a field in many classes.

Another alternative is to ThreadLocal. Where this variable state can be kept. It's clean and easier to implement. The application is single-threaded, so thread safety is not an issue.

But the problem is, it affects the clarity of the methods that will be using it.

What is the recommended approach in this kind of scenario?