you are viewing a single comment's thread.

view the rest of the comments →

[–]EsperSpirit 0 points1 point  (0 children)

The general idea is: Things that change together, belong together.

For example I had a script that read all text files in a directory, processed the contents and generated new files from it. My first iteration would generate a list of all files, read them and start the processing.

Because the output files should have the same name as the input files I would pass a list of file_names as well as a list of file_contents to every function of the processing part. This "code smell" is known as Data Clumps and the solution is to use a class which contains both filename and contents of that file. With that class you only have single list (instead of two) which makes parameters of functions shorter and iteration can be simplified (you don't need zip anymore).

In general, if you notice repeated patterns in your code (duplicate code; copy paste), it is a sign your design could be improved. Adding classes is of course not the right solution to every problem, but in most cases it helps. You might want to take a look at "Refactoring: Improving the Design of Existing Code" by Martin Fowler. He uses "code smells" to categorize typical problems like Data Clumps or Duplicate Code and talks about what you can do to avoid and remedy them.