# Refactoring to remove passthrough variables

DevFeed: [Refactoring to remove passthrough variables](<https://devfeed.tech/articles/refactoring-to-remove-passthrough-variables-52550.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/refactoring-to-remove-passthrough-variables/>)

Published: 2020-05-19T00:00:00Z

Content type: article

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [Test-driven development](<https://devfeed.tech/topics/tdd.md>), [class](<https://devfeed.tech/topics/class.md>), [constructor](<https://devfeed.tech/topics/constructor.md>)

Tags: [passthrough](<https://devfeed.tech/tags/passthrough.md>), [react](<https://devfeed.tech/tags/react.md>), [refactoring](<https://devfeed.tech/tags/refactoring.md>), [typescript](<https://devfeed.tech/tags/typescript.md>)

## AI overview

A passthrough variable travels through methods or classes before reaching the place that uses it. In the Puppeteer example, BrowserContext created a TaskQueue and passed it through Target to Page, although only Page needed it. Moving creation to Page removed the unnecessary instance variables and constructor arguments, reduced code, and limited which classes needed to know about TaskQueue. The author recommends this when the value can be created where it is used. Passthrough variables can still be the clearer choice when creation is constrained: alternatives such as global state or React Context add indirection, so the tradeoff depends on the case.

## Source excerpt

I've been working recently on Puppeteer and migrating it to TypeScript. This has presented the opportunity to do some refactoring and today I want to share a recent refactoring I did to remove passthrough variables. What is a passthrough variable? A passthrough variable is a variable that gets passed through multiple method calls before it gets given to the actual place in which it's used. Normally these happen either because: the object that needs the data is unable to create it the object that creates the data (in the above example, A), used to need it, but now doesn't due to a change in functionality or behaviour. Whilst we're not specifically talking about React in this post, you see this happen a lot with React props. This is known as "prop drilling" and is also something you should be wary of. Dealing with passthrough variables It's important to note that passthrough variables are not always avoidable, and often they are the preferred solution. The fix for passthrough variables can be simple - moving the creation of the value to the place where it's needed is the easiest fix - but if you're constrained often the explicitness of passthrough variables is preferable to any other solution. Whilst it makes you jump through a hoop or two, the below code is explicit and does tell the full story about what's happening: class A { constructor() { this.value = new SomeValue() this.b = new B(this.value) } } class B { constructor(value) { this.c = new C(value) } } class C { // somewhere in C we use the value } It's definitely not the nicest code you've ever seen but it can be methodically followed. Any solution that creates a method for C to access the variable without the explicitness of passing the values through will introduce some indirection for a developer to follow. For example, if you chose to put the value on the global scope (I do not recommend this, but it's a useful example!), you have to figure where that value comes from: class C { doSomething() { // woah, wh