DEV Community

loading...

Discussion on: Stop Using "data" as a Variable Name

Collapse
thebuzzsaw profile image
Kelly Brown

Also, stop using "temp" as parts of names. All variables are inherently temporary. They are local in scope. Telling me that it is "temp" adds no new information. There is always a superior name. Take the traditional swap algorithm:

swapValue = a;
a = b;
b = swapValue;
Enter fullscreen mode Exit fullscreen mode

Knowing the variable is used to contain the swapped value is far more useful information than knowing it's a temporary store.

Collapse
dcwither profile image
Devin Witherspoon Author

Great call out! I don’t see this much in production code, but is super common in interview questions, as well as when people are asking for help. I also like your alternative as a replacement without additional context.

Collapse
thebuzzsaw profile image
Kelly Brown

I kid you not: I have seen tempData before. Best of both worlds!

Collapse
mchaitanya profile image
Chaitanya Malireddy

In this context, if this were JavaScript, I'd dispense with the 'temp' variable altogether :) Also, often it makes sense to chain operations to avoid having to name intermediate states.

[a, b] = [b,a]; // destructuring assignment
Enter fullscreen mode Exit fullscreen mode
Collapse
thebuzzsaw profile image
Kelly Brown

To me, it depends on the complexity of the intermediate state. I used to aggressively pack as many operations into a single line as possible, but more recently, I like capturing operations into local variables both for readability and for debugging (easily mouse over variables to see what the answer was). Obviously, this can be taken too far, so it's judgment call.

Thread Thread
mchaitanya profile image
Chaitanya Malireddy

Yeah I don't like to pack too much into a clever oneliner either - makes it hard to read and debug. I like chaining methods if I can, like say a bunch of array transformations. But you're right it all depends on what you're trying to do - one has to strike a good balance per usecase.

Thread Thread
thebuzzsaw profile image
Kelly Brown

Chain methods are amazing. I can put each call onto its own line, and it becomes a super clear series of steps.