Wednesday, April 17, 2013

One large file or multiple small files

There is an age-old debate that goes back before the age of computers: shall we keep stuff in one large file or in multiple small files? Do you prefer a travel jacket with plenty of pockets or you rather take huge bag with you?


When you want to decide between N x 1 and 1 x N, there are more dimensions to take into account.  There are some practical considerations.  Many programming languages want to treat abstract modules and concrete files interchangeably, having a one-to-one mapping between a module and a physical file.  Revision control systems also work on a per-file basis, so it's more meaningful to have many diffs for more small files, because it gives a more specific location of the change than having a list of changes with large line numbers for a single file.

But we tend to have two modes during development.  We want to get the big picture and navigate to a certain piece of code quickly.  Other times we want to zoom into a method body and hide everything else that clutters the view.  Code folding is one way to switch between these two modalities, at least at the file level.  Modern IDEs try and add more tools to handle this duality, such as a tree view of all modules or a view of variables and functions within a module.  Clicking on a module or a function opens it in another pane, the editor window.


I think I have an answer now, it least for files. It's not a working implementation, just a description of a program I hope someone would implement pretty soon.

The idea is that you keep everything in small files and define views on them. Views are bi-directional. A change in a file is reflected in the view. You can also edit the view and the modifications are propagated back to the original file(s). To make this work, we'll need three types of files,

  • The small files which may be any kind of text files
  • The view file which is a large text file containing relevant snippets from the small files and a little meta-data about the origin of each snippet
  • And a template file which specifies which small files should be processed for a view and what snippets should be included
A template file in its simplest form looks like this


Something to think about
*** find:idea/*.md grep:^# .*
+ add me

It has special lines.

The view file looks like this

Index: languages/ini.js
===================================================================
--- languages/ini.js    (revision 199)
+++ languages/ini.js    (revision 200)
@@ -1,8 +1,7 @@
 hljs.LANGUAGES.ini =
 {
   case_insensitive: true,
-  defaultMode:
-  {
+  defaultMode: {
     contains: ['comment', 'title', 'setting'],
     illegal: '[^\\s]'
   },

*** /path/to/original timestamp
--- /path/to/new      timestamp
***************
*** 1,3 ****
--- 1,9 ----
+ This is an important
+ notice! It should
+ therefore be located at
+ the beginning of this
+ document!

! compress the size of the
! changes.

 It is important to spell
yeah...

No comments:

Post a Comment