Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

MT2 Question about variables

+17 votes
Would it be better to organize a group of 20-30 variables in a class, all of which are of different types?  Or would you rather put them in a series of 5 or so arrays, and manage those arrays instead?  What would be your reasoning behind the right decision?
asked Oct 26, 2015 in Fall 15-16 by Nick Caputo (100 points)

2 Answers

+4 votes
Significant variable (variable with different type) should not be group in array, it would be so confusing to try to figure out what variable we dealing with in an array, and it horrible to set up getter and setter. Also, array allow mutability, and it really easy to change the structure of the array. What if we think obj[3] is the address but some other method change it the obj[5]?

And, a class should not have that much of variable anyway, it very inelegant to have a class like that
answered Oct 27, 2015 by An Nguyen Dang (100 points)
–1 vote

put them in a series of 5 or so arrays- because it is easier to manage and keep track of the variables

Amending my statement, I misinterpreted the question. I agree that in general An is correct, that a class is better. But arrays allow for lend better to immutablilty because I think it is harder to change  the size of them and traversal is faster.  If you want mutability then a class is great.  As a side note, if you have a large class like that, try to break it into smaller classes, it would help with code readability.

answered Oct 27, 2015 by Kelsey Self (100 points)
edited Oct 27, 2015 by Kelsey Self
Arrays don't generally help with making things immutable... even if the size of the array is fixed, the data within the array can be easily changed by assignment: myArray[3] = "something else";