-2

I need something clarified. I have been having a discussion with someone and we have conflicting arguments. Hence, I came here to ask looking for your insights.

Say we have to implement our own stack class and assume we are using arrays to implement it. Say we can use ArrayList class from Java.

And the stack has a size because a stack can grow. We need the size of the stack because the size is needed to perform certain operations.

Should I be declaring a new variable to represent size in my stack class OR should I be using the size attribute from the ArrayList class?

Here is my reasoning:

We should be declaring a new variable to represent size in my stack class because the size of the stack is data that's related to the stack. We should be aiming to group related data together.

It makes it easier to debug if there are updates done to the variable because we only need to look at one class and that is the stack class. Otherwise, we would have to jump into another class, which makes debugging harder.

It also improves readability since related data are encapsulated together and thus makes it easier to understand the code and hence maintain the code.

So, is my above reasoning correct? If it is wrong, can someone please kindly explain to me why it is more beneficial to use the size attribute from ArrayList class? What is the design issue of having our own variable defined in our stack class?

Also, is it considered bad design if I rely on more attributes from ArrayList class rather than have the related data be defined in my own stack class?

Thank you for any insight.

11
  • 2
    You're way overthinking this. Use the technique that best suits your needs or sensibilities. My instinct is to use the ArrayList size; it's there already, after all. But it's your stack, not mine. Nov 30 '21 at 5:42
  • 2
    You're using the words "wrong" and "bad" like you're breaking laws or something. There's no "wrong" or "bad," there is only more or less optimal given a specific set of criteria. Nov 30 '21 at 5:47
  • 1
    I'm sorta wondering why you're not using the Stack class that Java Framework already provides. Nov 30 '21 at 5:54
  • 1
    As a learning exercise, I would probably start with an array. You can always graduate to an ArrayList if you think you need more horsepower, but the array has the virtue of simplicity. Nov 30 '21 at 6:03
  • 1
    Or it could be the other way around; e.g. here's a good exercise: if you start with an array, then you'll have to use an internal variable to keep track of the stack size, because array size is fixed. Then write some code in main() that uses your stack class. Then change the internals to use an ArrayList without changing the calling code from main(). Nov 30 '21 at 6:36
2

One consideration to storing size separately is that it adds an additional point of maintenance. When adding an element, one has to be sure that the size variable gets incremented. When removing an element, one has to decrement size. They must always be in sync, and this is a common source of bugs and errors.

I would exercise caution when storing derived data. By using the List interface's size method, we can always guarantee that the size we return matches the size of the backing ArrayList.

One may also consider "principle of least surprise". If another software engineer is reading your code, they may ask, if size is stored separately, perhaps there's another reason for why? For example, maybe the engineer will question whether it is always the case that the size of the ArrayList is equal to the size of the stack. By deferring to the size of the ArrayList rather than your own variable, this is also made explicit to the reader.

It also improves readability since related data are encapsulated together and thus makes it easier to understand the code and hence maintain the code.

This is a good general software design principle you pointed out; this is called cohesion. However, I would turn this back and ask, "what is the main readability benefit from having a separate variable?" When an element is added to the ArrayList, a reader of the code will know that it means size will be incremented, and vice versa. One must balance cohesion with single responsibility, and attempting to reinvent the wheel will lead to much more complexity and maintenance down the road.

But as mentioned in the comments, there is no objectively "wrong" answer to any question of this form.

1
  • Thanks for the information and insight.
    – Fried_Mind
    Nov 30 '21 at 9:01
1

As far as I can see there is there a misunderstanding.

Your custom stack is build around another datatype - e.g. ArrayList. And the ArrayList would be encapsulated ("hidden") in your stack implementation.

Typically you would define a getter for size in your Stack class. So from a consumer's perspective the size belongs to the stack. What the consumer doesn't need to know are the implementation details. He asks only for size.

Implementationwise you are returning the size from the ArrayList. No need for additional bookkeeping. And if you want to do "any magic" - because "your size" is different from the actual size of the Arraylist - you could do it in the getter. You would have best of both of your approaches.

Any separate variable would introduce potential errors like forgetting to keep the variable in sync with the actual ArrayList's size. I think ArrayList does a good job on bookkeeping its size. There were good reasons needed not to rely on that.

1
  • This makes sense, thank you.
    – Fried_Mind
    Nov 30 '21 at 9:06
0

This is not an answer (coming from OP) but this is the argument from the person I was discussing with and I quote:

"Lets list reasons why it is good, and why it is bad:

Good: None (Please reply to this comment with some examples where this is good).

Bad: Useless, causes many bugs, decrease performance, more code maintenance.

It is not good, therefore not right, therefore wrong. Please tell me reasons why do you think adding a variable is good?"

In conclusion, this person thinks that it is completely wrong and there are no good reasons at all to have your own size variable in the stack class. And I would like to know if this is true for the simple case I have described above.

From what I have learned with the solutions/comments given, there is no "wrong" answer considering that this is a simple case/scenario. There can simply be a more optimal approach compared to other approaches. I also understand the more optimal approaches given by other solutions. But, this person thinks otherwise.

Thank you for your time and for any insight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.