As a package size grows, the readability of the code decreased. The best practice is to separate the package into different files according to their functions. Sometimes, the files need variables and functions of each other.
Example:
foo.el
(defvar foo-data-path "~/foo/data/")
(foo-log-write 'somedata)
foo-log.el
(defvar foo-logfile (concat foo-data-path "foo.log"))
(defun foo-log-write (data)
;; Write to log file
)
As foo.el
use function from foo-log-write
, I put (require 'foo-log)
in foo.el
.
In foo-log.el
, it uses foo-data-path
from foo.el
, should I also put (require 'foo)
in it?
The syntax checker always complains reference to free variable
if I don't add (require 'foo)
.
There are two techniques I've seen to handle the situation you describe. It should be noted that you can get into a circular dependency problem is your not careful. While you can have require in a file multiple times, if they are mutually dependent and if your 'provide lines are at the end, you can easily end up in a loop.
The two solutions I've used successfully are
(Preferred). Factor out the mutually dependent code into a 3rd file and have the other two require that file.
When all else fails and you cannot factor things out and cannot avoid the mutual dependency, make sure that at least one of the files has the provide statement at the beginning. This is not normally a good idea because if your file errors out before it finishes loading, you will appear to have satisfied the require, but not everything you expect to have been loaded will be.
The second technique helps break the infinite loop because you have satisfied one of the provide requirements. The problem with having the provide at the end of the file (where it normally should be) is that you do not satisfy the require request until the file has been fully loaded. However, if the file has a require for another file, it will first load that file before continuing to load the first file. If that second file also has a require and that require is to the first file, emacs will start loading the first file again until it hits the require for the second file and around the loop we go.
by putting the provide at the beginning of the file, the next time it hits a require for that file it will not try to load it as it will believe it has already been loaded. the loop is broken.