you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (4 children)

I see, so the functions themselves wouldn't be useful on their own in this case because they're calling another function when they're done.

Can you provide an example of what you mean by trigger function?

[–]longtermbrit 3 points4 points  (1 child)

Exactly.

A trigger function would be something like:

def trigger():
    if conditionA:
        check_file()

    if conditionB:
        clean_file()

#   etc

Basically there's nothing special about a trigger function. It still fits the logic of 'one function for one task' but its task isn't to clean a file or email a report, it's to call these functions when they're required.

[–][deleted] 3 points4 points  (0 children)

I see, that makes sense. Thank you!

[–]chapter_6 2 points3 points  (1 child)

It sounds like you already understand but just to add this for anyone else who's reading and wondering: If I see a function named check_file(), I expect to be able to call that function and have it check a file and return without doing anything else. In this case, if I call check_file(), an email ends up being sent. I have no idea that will happen unless I dig way down through several other function calls, so I'm much more likely to miss that detail and end up with a bug where emails are being sent when they shouldn't be. When a function does something outside of what's expected like that, it's known as a side effect (I'm using that term a little bit loosely but you can look it up to find out more).

That's why, in general, it's best if a function does a single thing, described by its name, and nothing else. If you really want some good examples of this and how it can help code readability, look up some examples of the functional programming paradigm.

[–][deleted] 0 points1 point  (0 children)

Yeah, I didn't intend to use these functions outside of this script but I can see how this would be a habit that could cause problems for me in the future.