Pros and cons of using %Variable%MyVar% vs %MyVar%? [SOLVED]

Going through some topics here on the forum I saw that we can use variables without using %Variable in the beginning.

Is there any advantage of doing this? For Local or Instance variables I don't see that this would cause any issues, for example %Local__myName% is clear enough to know it's a local variable. The same for instance variables.

For global variables, though, it can be mistaken for one of the existing tokens? For example we have %TriggerValue%, but what if I ended up creating a global variable caleld TriggeredValue? I would have the %TriggerValue% token as well as the %TriggeredValue% global variable. That particular case seems to be one scenario where using `%Variable%TriggeredValue% would be more appropriate, but other than that, is there any other pros/cons?

The manual probably says it best:

You can also use a short form of just %Variable Name% to include variables as long as the variable exists and has a value and there is no corresponding text token, although generally it is better and clearer to use the longer form %Variable%Variable Name% .

2 Likes

So I guess there is no "hidden" thing...?
It's just a matter of typing fewer words, but for readability purposes, it's always better to include %Variable to make it clear what those are, right?

If so, as I stated, %Local__myName% or %Instance__newClac% seem very obvious and clear, as opposed to using the same approach for global variables.
That being said, I have a snippet for local variables anyway, so I even rarely type the whole thing anyway. The snippet does it for me :wink:

Thanks for clarifying!

Name clashes.

Keyboard Maestro defines a lot of tokens.

Tokens [Keyboard Maestro Wiki]

Clearer at a glance (with the %Variable% prefix) that you are not looking at anything which (deliberately or accidentally) is one of the KM-defined token names.

2 Likes

If you don't already have one, write yourself a macro like this:

I have that in a group only active when the KM Editor is active, and I've assigned it to vv. Yes, that means I can't type "savvy" in the editor, nor use variables with double Vs in them, but I don't do either of those things. And it makes it super simple to put the wrapper in place, with the cursor positioned between the final percentage signs.

-rob.

1 Like

Exactly. That's what I think too.
And having the same workflow is always better for me instead of using %Variable for global variables, but then not using it for Local or Instance variables.

I actually have a snippet that I've been using for a long time. At first I used Raycast, but now I'm using Espanso (discovered a few days ago and been loving that app!)

I've been noticing for a long time that anything that triggers any KM menu or stuff like that, always seems to work in a "sketchy" way so for some things such as KM menus and all that, I use BetterTouchTool. And for snippets I now use Espanso.

So for variables, I always tend to copy the variable, for example Local_myVar and then I place the cursor where I want the variable to go and type %paste and it will automatically create %Variable%Local_myVar%

If I want to just create a variable from scratch I just type %%% and it types %Variable%Local__% and it puts the cursor after the 2 underscores.

I very rarely use global variables so when I do, I don't mind typing the whole thing.

One (very small) advantage is that I will provide support to people who use the leading "Variable", but I hesitate to provide support to people who don't. That's a "hidden" advantage to using "Variable." If they don't use it, I will probably tell them to fix that before I provide further support.