Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad coding style to not assign a new instance to a variable?

At the top of my program I have a code segment that looks like this

var XXXAssembler = new XXXAssembler(ctx);
XXXAssembler.LoadXXX();

var YYYAssembler = new YYYAssembler(ctx );
YYYAssembler.LoadYYY();

var ZZZAssembler = new ZZZAssembler(ctx);
ZZZAssembler.LoadZZZ();

In the above logic I use each varaible once to call the respective loader, and I don't use the variables anywhere else.

I can change the code to this

new XXXAssembler(ctx).LoadXXX();
new YYYAssembler(ctx ).LoadYYY();
new ZZZAssembler(ctx).LoadZZZ();

This reduces the size of the code, but I'd like to think it simplifies it as well. I could see the usefulness of variables for debugging, but I don't think that's necessarily a good reason. Others may disagree.

Is the non-varaible version considered bad coding style?

like image 323
Don Avatar asked Dec 05 '22 21:12

Don


1 Answers

Unless you're going to use the object assigned to the Assembler variable, then there's no need for it.

I'd say get rid of it, clean up the code, and then if you need it later you can bring it back.

like image 198
Justin Niessner Avatar answered May 19 '23 16:05

Justin Niessner