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.