October 01, 2019

Are Python dictionaries an important technique?

Python dictionaries are less known technique in creating software. It is basically a luxury version of a linked list. The user doesn't has to handle pointers and hash values, but can store and retrieve data directly into the dictionary. Additionally it's possible to create subdictionary so that large amount of information can be stored in the main memory.

The disadvantages of the idea should be mentioned. A python dictionary is similar to a data class in object oriented programming. The idea is, to create a centralized data storage which holds all the data from the game, and all the classes have access to the storage. The classes are not forced to communicate to each other but they have access to the data as default.

Usually, a centralized data storage is an antipattern in object oriented programming because it avoids OOP at all. The problem is, that the resulting structure will look the following: there is a centralized dictionary with 50 entries. And around the dictionary there are 20 subfunctions with together of 500 lines of code. Technically it will run fine, but nobody likes to bugfix such a sourcecode. Because there is no structure available. It's unclear which function is doing what.

In general, it's a good idea to avoid dictionaries and use python classes as an alternative. The class stores only a subpart of the information and holds also the function for manipulating the data. This is equal to an easy to debug sourcecode.

No comments:

Post a Comment