OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Refactoring several function calls into one function

  • Thread starter Thread starter serviola_29
  • Start date Start date
S

serviola_29

Guest
I have the following code that is repeated several times in a python script:

Code:
if paths["ARCHIVE_PATH"].is_file():
    add_files_to_zip(paths["ARCHIVE_PATH"], paths["COPY_PATH"])
else:
    create_zip_archive(
        paths["ARCHIVE_PATH"],
        paths["ARRIVAL_PATH"],
        os.listdir(paths["ARRIVAL_PATH"]),
    )
delete_files_in_directory(paths["COPY_PATH"], os.listdir(paths["COPY_PATH"]))

The issue is that trying to not repeat myself I am attempting to put this into a function but have found several issues:

  • If functions should really do one thing can I really make this function?
  • If this function is created wont it have to many parameters?

I am trying to just observe best practices and this is an issue I have come against several times. It is just some function calls in a sequence which are repeated but creating a function out of it (I have a feeling) may create worst code. Do you guys have any ideas on how to best manage this?
<p>I have the following code that is repeated several times in a python script:</p>
<pre><code>if paths["ARCHIVE_PATH"].is_file():
add_files_to_zip(paths["ARCHIVE_PATH"], paths["COPY_PATH"])
else:
create_zip_archive(
paths["ARCHIVE_PATH"],
paths["ARRIVAL_PATH"],
os.listdir(paths["ARRIVAL_PATH"]),
)
delete_files_in_directory(paths["COPY_PATH"], os.listdir(paths["COPY_PATH"]))
</code></pre>
<p>The issue is that trying to not repeat myself I am attempting to put this into a function but have found several issues:</p>
<ul>
<li>If functions should really do one thing can I really make this function?</li>
<li>If this function is created wont it have to many parameters?</li>
</ul>
<p>I am trying to just observe best practices and this is an issue I have come against several times. It is just some function calls in a sequence which are repeated but creating a function out of it (I have a feeling) may create worst code. Do you guys have any ideas on how to best manage this?</p>
 

Latest posts

Top