-2
\$\begingroup\$

This is not a task, but I want to know what the general consensus is on functional programming. Particularly functional programming in Python.

The two snippets of code I am comparing are the following.

if files:
    if isinstance(files, basestring):
        pattern_good = check_if_exists(files)
    else:
        pattern_good = all(map(lambda file_pattern: check_if_exists(file_pattern), files))

VS

if files:
    if isinstance(files, basestring):
        pattern_good = check_if_exists(files)
    else:
        pattern_good = True
        for file_pattern in files:
            pattern_good = pattern_good and check_if_exists(file_pattern)

Which is more readable and maintainable?

From a personal preference POV, using functional programming is more fun and convenient IMO. It's less characters to type.

\$\endgroup\$

closed as off-topic by 200_success, yuri, IEatBagels, Toby Speight, Gerrit0 Mar 14 at 1:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, yuri, IEatBagels, Toby Speight, Gerrit0
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Not an answer, but what is pattern_good = pattern_good and check_if_exists(file_pattern) meant to do? As soon as check_if_exists() returns False, pattern_good can never become True again (False and True = False) \$\endgroup\$ – match Mar 13 at 15:27
  • \$\begingroup\$ Both of the snippets receive a single file pattern or a list/set/iterable of file patterns and check if all of them are valid. I could breakout of the loop early in the second snippet if one of the file patterns is invalid, but to keep it consistent with the first snippet, I don't(as I don't know how all() is implemented). \$\endgroup\$ – Aditya Mar 13 at 15:31
  • 1
    \$\begingroup\$ @Aditya all breaks out of the loop (is lazy). \$\endgroup\$ – Peilonrayz Mar 13 at 15:52
  • 1
    \$\begingroup\$ The way you have worded the question and the title, it sounds like you are asking for opinions about functional programming in general, with two sketchy code excerpts acting merely as examples. Please reword your question to state in detail what the code accomplishes, so that we can review your solutions rather than giving generic opinions. See How to Ask. \$\endgroup\$ – 200_success Mar 13 at 16:55
  • \$\begingroup\$ You didn't provide the complete working code for us to review. \$\endgroup\$ – Benoît Pilatte Mar 13 at 16:58
1
\$\begingroup\$
  1. You can simplify the function you pass to map.

    pattern_good = all(map(check_if_exists, files))
    
  2. Neither is better, as map is in a state of pseudo-deprecation and the other code is hard to read.

    Instead use iterator based programming, and is what I find to be one of the best parts of Python.

    And so I'd use a comprehension instead of either of these.

if files:
    if isinstance(files, basestring):
        files = [files]
    pattern_good = all(check_if_exists(f) for f in files)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.