Knowledgebase

Structuring Larger Programs Print

  • python, database, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Beyond a single file.

WHEN TO SPLIT

When a file becomes hard to navigate When parts are reusable When distinct responsibilities exist

HOW TO DIVIDE

By responsibility, not by type.

WHAT THAT MEANS

Group code that changes together.

WHAT A TYPICAL STRUCTURE SEPARATES

Entry point Core logic Data access Configuration Utilities

WHY SEPARATE DATA ACCESS

So logic can be tested without a database.

WHAT TO KEEP AT THE TOP LEVEL

As little as possible.

Code at module level runs on import, which surprises people.

WHAT TO PUT BEHIND THE STANDARD ENTRY CHECK

Anything that should run only when the file is executed directly.

WHY

So the module can be imported without executing it.

WHAT TO AVOID

Circular dependencies between modules One module knowing too much about others A utilities module that becomes a dumping ground

WHAT TO REVIEW

Whether the structure still reflects what the program does.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot