[erlang-questions] preferred looping style

Icarus Alive <>
Wed Jun 22 20:13:00 CEST 2011


Thanks folks - @Daniel, @Jesse and @Sergej.
I see the point now.

On Wed, Jun 22, 2011 at 11:28 PM, Daniel Luna <> wrote:

> 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 <> 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
> > 
> > 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110622/13d00dfd/attachment.html>


More information about the erlang-questions mailing list