Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a large collection of macros that do different things but all eventually use the same macro internally (call this macro \tasks). Sort of like this, but with better macro names:

\newcommand{\aa}{... \tasks}
\newcommand{\bb}{... \tasks}
...
\newcommand{\zz}{... \tasks}

The \tasks macro has several parts which should be executed conditionally. This could be done using etoolbox or just plain \if, something like this:

\newbool{subtaska}
\newbool{subtaskb}
...
\newcommand{\aa}{... \booltrue{subtaska}\boolfalse{subtaskb} ... \tasks}
\newcommand{\bb}{... \booltrue{subtaska}\booltrue{subtaskb} ... \tasks}
...
\newcommand{\tasks}{
    ...
    \ifbool{subtaska}{...}{}
    \ifbool{subtaskb}{...}{}
    ...
}

But all of this could also be performed using \let.

\newcommand{\subtaska}{...}
\newcommand{\subtaskb}{...}
...
\newcommand{\aa}{... \let\xsubtaska\subtaska\let\xsubtaskb\relax ... \tasks}
\newcommand{\bb}{... \let\xsubtaska\subtaska\let\xsubtaskb\subtaskb ... \tasks}
...
\newcommand{\tasks}{
    ...
    \xsubtaska
    \xsubtaskb
    ...
}

I know that the approach using \let won't work if arguments need to be passed to the subtasks, since \relax (or \@empty) wouldn't be happy with that, but that isn't an issue for me.

Is there any advantage in taking the conditional approach versus the \let approach (ignoring the issue of arguments with \let)? For example, are there any performance benefits (yes, avoid premature optimization...) or special additional pitfalls one way or the other?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

If you wanted to eat arguments you could still use \let but use \@gobble (or similar for more arguments) rather than \relax. It's clear that \let version is more efficient after that it's just a matter of personal style I think. The main reason etoolbox \ifbool is less efficient is that it does some sanity check that the first argument is a declared boolean switch. But if you are sure you won't mis-type and you always \let assign the intended thing then that's probably what I'd do.

share|improve this answer
1  
On the other hand, the approach with booleans allows for \ifboolexpr which can be handy in many situations. –  egreg Jun 21 '12 at 16:33
    
Certainly in a few situations. I have discovered that 99 per cent of the calls to the costly \ifboolexpr are not necessary. It could almost always be done more efficiently. You may wish to give some contra-indications here. –  Ahmed Musa Jun 21 '12 at 19:33

As a practical matter, it may depend on how complex your conditionals are. In your simple situation, subtaska and subtaskb are independent of whether they appear in the context of \aa or \bb; however, you may need to make further decisions based on which of these macros was expanded, and then it's probably best to have a conditional that says "we are in this case".

More philosophically, you may be in the design situation where you are not yet sure of the structure of your tasks; then, you can model your own mental processes better using conditionals. Using the \let method is extremely rigid in terms of code structure. Once the code stabilizes you may find opportunities for streamlining it by using named subtasks.

And most philosophically, there is the question of whether the different code branches are really "doing the same thing" but in different ways, or are doing basically different things. The first situation sounds like a candidate for named subtasks; the second sounds like a case for conditionals.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.