Accessing Linear Data Structure


#1

October 20, 2018 (Europe: Paris), October 20, 2018 (America: Los Angeles)
I’m working on Linked Lists Implementation II, full gist linked Here

My code works, however slightly different from the provided solution and I just want to make sure I am not missing a key concept with potentially serious side effects.

current_node = current_node.next_node

vs the Codecademy solution:

current_node = current_node.get_next_node()

So where the Codecademy’s solution is to run a class method to return the value, I am just assigning the value. Is there anything inherently bad with my line of thought?


#2

Hi @chipmaster06973,

In principle, the LinkedList class should use the getters and setters of the Node class to access instance variables of that class. There are some hazards associated with accessing instance variables of another class directly when getters and setters are available to access those variables.

One of the hazards is that revisions of the internal details of a class could break the functionality of other classes that access those details directly. Suppose, for example, the author of the Node class changed the name of the next_node instance variable and revised the getters and setters accordingly. All other classes that access that variable directly would need to be revised. Classes that used the getters and setters would avoid that problem.

Another hazard is that some getters and setters perform other tasks in addition to accessing and assigning values to variables. For example, they might update counters or apply time stamps. Accessing the instance variables directly from another class would bypass that functionality.

In accordance with the above, use getters and setters to access instance variables when those getters and setters are available.