[erlang-questions] preferred looping style
Daniel Luna
daniel@REDACTED
Wed Jun 22 19:58:46 CEST 2011
I'm not sure I understand what you mean with an extra stack frame.
Version 2 has a tail call at the end of loopInit so will not use any
extra memory. While version 1 adds an extra comparison operation for
every time around the loop.
Besides that, if you do any work at all within the loop, that will not
matter at all.
Go with the readable version. It's faster, uses less memory, and (most
importantly) is easier to read.
(If you want to be able to do hot code loading on this code you should
use ?MODULE:mainLoop(), but most likely just convert whatever you do
into a gen_server, supervisor, or gen_event.)
/Daniel
BTW. Erlang naming convention would have loop_init. CamelCase is for
variables, while underscore (_) is used for function names.
On 22 June 2011 19:44, Icarus Alive <icarus.alive@REDACTED> wrote:
> Hi,
>
> Of these 2 styles, which one is preferred, and why ?
> To me, the readability of 2nd style is better, tough it consumes 1 stack
> frame extra.
> Are there some cases where style-1 might be preferred, or be the only option
> ?
>
> %% style-1 -----------------------------------------
> mainLoop(InitialRun) ->
> if InitialRun == true ->
> % do some initialization,
> mainLoop(false);
> true ->
> % do routine loop tasks,
> mainLoop(InitialRun)
> end.
>
> %% style-2 -----------------------------------------
> loopInit() ->
> % do some intialization,
> mainLoop().
>
> mainLoop() ->
> % do routing loop tasks,
> mainLoop().
>
>
> cheers,
> Icarus
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
--
http://havebackpack.com - Exploring the world with a 30L backpack.
Open to fun challenging assignments anywhere in the world.
More information about the erlang-questions
mailing list