Subroutine FTW

KM has very limited support for what programmers would call subroutines or functions.
I tend to think of Sub-Macros as more like a code library, than I do of internal functions.
But the way I mange that is:

  • If the Sub-Macro is designed for use by only the macros of a Macro Group, then I store the Sub-Macro in that Group.
  • Else I store the Sub-Macro in a "library-like" Group I call, well, "Sub-Macros".
  • I try to give the Sub-Macro a unique name, or at least contain a unique keyword/ID, so that I can find all instances of its use by a KM Editor Global Search.
  • I have a Macro/Script that will open the Sub-Macro used in a selected Execute Macro Action in another Editor window.

As I'm sure you know software design is almost always a matter of balance, or trade-offs. So, if I have a macro that uses the same 2-3 Actions as a set, then I would likely first put them in a Group Action to easily identify and maintain, and just copy the Group Action to a new location within the Macro when I need it.
If it looks like I am going to use this Action set a lot, then I will create a Sub-Macro with those actions. This Action Gear Option to engroup with a "Macro" makes that easy.

One final note on reuse of common Actions: I use MACRO: [KMFAM] Favorite Actions and Macros by @DanThomas extensively. This includes putting multiple Actions, usually as a Group, as one KMFAM item.

Here's a good KMFAM example:

It works like a good function library when you copy (rather than reference) the "function" to you current code (Macro). It is easy to update the KMFAM item, but it does not, of course, change any of the existing copies in my Macros.

1 Like