1

I have a software project which grows rapidly in an unexpected way, many customers request similar tasks but with slightly different needs and parameters. I start to dislike my current code structure and I have two ideas in mind how to improve my code:

I have a shared library with shared functions in it. This library is imported in each procedure file. One procedure file for each task, the tasks overlap a lot (90 %) but this could change in the future as people change their ideas and requests all the time (scientific project).

Idea A) I have a configuration file with all settings and parameters which can be changed depending on the procedure. In a single procedure file, I use the extracted parameters of the configuration file to set up the system. Example:

import shared_lib
# Get all the parameters and procedure from my configuration file

# This step is the same for each procedure
shared_lib.function1()

# This step depends which procedure we use
if procedure1:
    shared_lib.function2()
elif procedure2:
    shared_lib.function3()

# For this step we need to set a parameter depending on which procedure we use
if procedure1:
    shared_lib.function4(x = 10)
elif procedure2:
    shared_lib.function4(x = 20)

# This step is the same for each procedure
shared_lib.function5()

This works for two procedures but I fear the file will get messy with increasing number of procedure, which I expect in the future. In addition it is very annoying to change the settings in the configuration file every time I change the procedure.

Idea B) Create one file for each procedure and include all the setting and procedure code inside the file. Example:

### File procedure1

import shared_lib

# Set parameters
x = 10

# Start procedure
shared_lib.function1()
shared_lib.function2()
shared_lib.function4(x)
shared_lib.function5()

Example

### File procedure2
import shared_lib

# Set parameters
x = 20

# Start procedure
shared_lib.function1()
shared_lib.function3()
shared_lib.function4(x)
shared_lib.function5()

What I dislike with idea B, is that I will end up with many procedure file (plus many files of the shared code) with a large part of overlapping code.

Can I do better? I am not very convinced with both ideas...

  • 1
    There is no "one size fits all" solution, you need to learn some tools how to build stable, reusable components, which can be reused even when the requirements change. As start, learn about the template method pattern and the strategy pattern. – Doc Brown Jun 16 '18 at 20:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.