help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Temporary changing the behavior of a function


From: Yuri Khan
Subject: Re: Temporary changing the behavior of a function
Date: Fri, 6 Nov 2015 21:01:03 +0600

On Fri, Nov 6, 2015 at 4:23 PM, Marcin Borkowski <mbork@mbork.pl> wrote:

> so there is this function `foo', which calls the function `bar'.  The
> function `bar' is responsible for asking the user for some value and
> passing that value to the guts of `foo'.
>
> Now I want to call `foo' in the Mafia-mode;-), i.e., it should ask no
> further questions.  What do I do?  AFAIU, `cl-flet' won't help, since it
> is lexical.  The best I can think of is to temporarily advice `bar' with
> :override - but then, instead of a `let'-like, local construct, I have
> to explicitly add and then remove the advice, right?
>
> Any other ideas?

Yes. In other languages, we recognize such a need as a “smell”, a sign
of possibly bad design.

“foo” should have optional arguments that can be passed by the calling
code, and if they are not set, only then ask “bar” for user-supplied
parameters.

Alternatively, “foo” should invoke “bar” and pass its result to
“foo-guts” (we usually call it “foo-impl” or “do-foo”), and
programmatic callers should call “foo-guts” directly with the right
arguments.

If “foo” is in a third-party library you cannot or would rather not
change, only then consider monkey-patching “bar” as a workaround.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]