What do you do when it seems unavoidable to have a single function perform multiple tasks? via /r/learnprogramming


What do you do when it seems unavoidable to have a single function perform multiple tasks?

Since I began learning, I've heard time and again that a function should do one thing and one thing only. It should have a name like do_foo() and it should do foo and nothing more.

But it seems like I frequently run into issues where it just seems unavoidable to make the function do_foo_and_do_bar(). For instance, I'm programming for the gmail API and I need to get unread emails and mark them as read. Based on the above, the 'right' way to do this is to make get_all_unread() and mark_unread_as_read(). But that would be pretty inefficient, since you're finding each unread email twice, when you could just find it and mark it as read in one go.

Is it better to accept that inefficiency or to just call the function get_unread_and_mark_read() and move on with my life?

Submitted July 12, 2017 at 12:14AM by Endome
via reddit http://ift.tt/2t2ULXT

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s