Organizing files in a module

The source code for modules is typically organized as multiple source files. Although there is no hard and fast rule about how source files are organized, the following are useful guidelines:

  • Coupling: Highly coupled functions should be placed in the same file. Doing so allows less context switching when editing source files. For example, when you change the signature of a function, all callers of that function may need to be updated. Ideally, you would want to minimize the blast radius and not have to change many files.
  • File size: Having more than a few hundred lines of code in a single file could be a warning sign. If the code inside the file is all tightly coupled, then it may be better to redesign the system to reduce coupling.
  • Ordering: Julia loads the source files in the order in which you include them. As data types and utility functions are usually shared, it is better to save them in a types.jl and utils.jl file respectively and include them at the beginning of the module.

Similarly, the same considerations apply when organizing test scripts.

By now, we have learned how to create new namespaces using modules and submodules. More conveniently, a module is organized in a package so that it can be reused from an application. Once we have created multiple packages, it is unavoidable that they may have to depend on each other. It is important that we know how to handle these dependencies properly; this will be our primary topic in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset