# Branching on algorithmic options - advice on code organization

**URL:** <https://discourse.julialang.org/t/branching-on-algorithmic-options-advice-on-code-organization/67800>\
**Category:** New to Julia\
**Created:** [September 7, 2021, 10:38am UTC](https://discourse.julialang.org/t/branching-on-algorithmic-options-advice-on-code-organization/67800 "2021-09-07T10:38:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snir\_Gazit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snir_gazit/32/11027_2.png) [@Snir\_Gazit](https://discourse.julialang.org/u/Snir_Gazit)\
**Post date:** [September 7, 2021, 10:38am UTC](https://discourse.julialang.org/t/branching-on-algorithmic-options-advice-on-code-organization/67800/1 "2021-09-07T10:38:27Z")

</div>

A situation I encounter pretty frequently is the need to modify the behavior of an algorithm according to some boolean (or more general) “options” parameters.  
A plain if-else statement evaluated at runtime can be costly for a critical portion of the code (although in practice, I am not if this is indeed the case) that gets evaluated many times.

My question (more of advice) is which coding approach for the above is preferable in terms of readability, reusability, and performance.

Two approaches that come to mind are:

1. Parametric types – the branching is (presumably) evaluated at compile time since the type parameter is known and multiple dispatch can be utilized. One issue with that is the need to “fully” specify all parameters at invocation, which might be hard to maintain when parameters are removed or added in a future version of the code.

2. Encoding the parameter as a field object of a composite type and then branch according to its values.  
Potentially slower, it seems more straightforward to implement.

I hope my situation is clear and not too abstract.

Is there a preferred “Julian” approach?

---

<div class="post-metadata">

**Author:** ![Derek\_Vetsch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derek_vetsch/32/8552_2.png) [@Derek\_Vetsch](https://discourse.julialang.org/u/Derek_Vetsch)\
**Post date:** [September 7, 2021, 4:51pm UTC](https://discourse.julialang.org/t/branching-on-algorithmic-options-advice-on-code-organization/67800/2 "2021-09-07T16:51:23Z")

</div>

Could you maybe provide an example of something you are trying to optimize please?

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [September 8, 2021, 1:45am UTC](https://discourse.julialang.org/t/branching-on-algorithmic-options-advice-on-code-organization/67800/3 "2021-09-08T01:45:47Z")

</div>

If it’s just a Boolean for choosing different options, and the Boolean doesn’t change during the computation, the microprocessor’s branch predictor will quickly learn the correct branch and there won’t be a performance impact.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [September 8, 2021, 2:48am UTC](https://discourse.julialang.org/t/branching-on-algorithmic-options-advice-on-code-organization/67800/4 "2021-09-08T02:48:40Z")

</div>

[Keyword Arguments](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) are an easy and common way to add options to a function.

You can also sort of combine #1 and #2 as illustrated [here](https://discourse.julialang.org/t/application-code-organization/53224/15).
