What is the “Execute Around” idiom?

后端 未结 8 1419
不思量自难忘°
不思量自难忘° 2020-11-22 12:39

What is this \"Execute Around\" idiom (or similar) I\'ve been hearing about? Why might I use it, and why might I not want to use it?

8条回答
  •  星月不相逢
    2020-11-22 13:13

    I'll try to explain, as I would to a four year old:

    Example 1

    Santa's coming to town. His elves code whatever they want behind his back, and unless they change things get a little repetitive:

    1. Get wrapping paper
    2. Get Super Nintendo.
    3. Wrap it.

    Or this:

    1. Get wrapping paper
    2. Get Barbie Doll.
    3. Wrap it.

    ....ad nauseam a million times with a million different presents: notice that the only thing different is step 2. If step two is the only thing that is different, then why is Santa duplicating the code, i.e. why is he duplicating steps 1 and 3 one million times? A million presents means that he is needlessly repeating steps 1 and 3 a million times.

    Execute around helps to solve that problem. and helps eliminate code. Steps 1 and 3 are basically constant, allowing for step 2 to be the only part that changes.

    Example #2

    If you still don't get it, here is another example: think of a sandwhich: the bread on the outside is always the same, but what's on the inside changes depending on the type of sandwhich you choose (.e.g ham, cheese, jam, peanut butter etc). Bread is always on the outside and you don't need to repeat that a billion times for every type of sandwhich you are creating.

    Now if you read the above explanations, perhaps you will find it easier to understand. I hope this explanation helped you.

提交回复
热议问题