Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+7 votes

For Practice-It question 2.13 firstSecond2, the "simpler solution" the question asks for assigns two variables of identical types on the same line.

Given:
int first = 8; int second = 19; first = first + second; second = first - second; first = first - second;

The first line of the answer field shortened code is

int first = 8, second = 19

which I have never seen before. Knowing this, is assigning multiple variables of the same type on the same line good practice? It seems like this would cause some confusion and wouldn't be an ideal solution.

asked in CSC211_Winter2018 by (1.8k points)
edited by

1 Answer

+2 votes
 
Best answer

I believe what it is doing here is making the code simpler, less redundant and easier to read. The " 'int = " instead of " int = " allows you to define multiple variables in a row without needing to repeat " int = " when you introduce each variable.

answered by (3.8k points)
selected by
+1

Are you saying that the extra apostrophes I put in have something to do with this? Sorry, but that was an error on my part. I was referring to the comma; there are no apostrophes in this problem.

I understand that it would make the code more concise, but is it necessarily good practice to do this? I imagine it might get confusing if someone defines all of their variables in this manner.

+1

While I’m not the expert on “good practice” when using Java, I would think that it is okay. It is clearly a valid way to code it so I suspect there is at least some purpose to listing variables on the same line like that.

...