What to include in a function /u/hmartin8826 Python Education

This question is not really specific to Python, but that’s what I’m working in at the moment. As a long-time scripter, but not a professional programmer, I struggle with balancing complexity (e.g. lots of little functions) and the “single responsibility principle.” Let me use a simple example. I currently have a single function that reads in a file, applies some filtering based on parameters passed to the function and returns a list of class instances. The function has to open the source file, read the contents of the file, process each line to determine if it should be included in the output, and once all lines of the input file have been processed, output the list of objects.

The function always returns a list, but it will be an empty list if no matching data was found. Any I/O errors raise an exception.

Is this too much for a single function? I almost always use functions for re-use rather than separation of duties. How are you guided by this?

submitted by /u/hmartin8826
[link] [comments]

​r/learnpython This question is not really specific to Python, but that’s what I’m working in at the moment. As a long-time scripter, but not a professional programmer, I struggle with balancing complexity (e.g. lots of little functions) and the “single responsibility principle.” Let me use a simple example. I currently have a single function that reads in a file, applies some filtering based on parameters passed to the function and returns a list of class instances. The function has to open the source file, read the contents of the file, process each line to determine if it should be included in the output, and once all lines of the input file have been processed, output the list of objects. The function always returns a list, but it will be an empty list if no matching data was found. Any I/O errors raise an exception. Is this too much for a single function? I almost always use functions for re-use rather than separation of duties. How are you guided by this? submitted by /u/hmartin8826 [link] [comments] 

This question is not really specific to Python, but that’s what I’m working in at the moment. As a long-time scripter, but not a professional programmer, I struggle with balancing complexity (e.g. lots of little functions) and the “single responsibility principle.” Let me use a simple example. I currently have a single function that reads in a file, applies some filtering based on parameters passed to the function and returns a list of class instances. The function has to open the source file, read the contents of the file, process each line to determine if it should be included in the output, and once all lines of the input file have been processed, output the list of objects.

The function always returns a list, but it will be an empty list if no matching data was found. Any I/O errors raise an exception.

Is this too much for a single function? I almost always use functions for re-use rather than separation of duties. How are you guided by this?

submitted by /u/hmartin8826
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *