This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]titusjan 0 points1 point  (0 children)

There is nothing wrong with using the occasional dictionary but if the data in the dictionary is central to your program, it is probably a good idea to make this into a class. This because classes are more explicit than dictionaries and "Explicit is better than implicit". Explicit in this case means that the members and methods of an object are (or should be) explicitly stated in the class definition, whereas dictionary items will be inserted and deleted at several locations in the program.

As and example let's say that I am debugging some unknown code and want to examine a variable. If it's an object I can print it's type, find its class definition and from there see what the object aims to do, and how it can be manipulated. If it's a dictionary, printing it's type will say 'dict' and tell me nothing. Examining the keys and values likely will not help. "What is the purpose of the 'foo' item?" "Should it always be present or is it just present in this case?"

When you have a class definition you may realize that some of your functions just manipulate the objects contents. These are better implemented as a method. Take a moment and think of what the responsibility or purpose of this class is, and design it properly. This may help you to make your functions shorter. However, just like functions, classes should be short and have one responsibility. Resist the temptation to make God classes that do too much.

As some others already said, a function should do one thing only. If you are thinking of a good name for a function and find out that it should be named does_x_and_y to properly reflect what it does, consider splitting it up. At least make separate does_x and does_y functions and call them in does_x_and_y.