Formatting style?

Ulf Wiger <>
Wed Jan 3 10:41:17 CET 2001


To begin with, Emacs would use the following indention:


trade_with_agent(Agent, #trade_task{mode=buy, market=Market}, Bid) ->
    Seller = Market:bid_agent_id(Bid),
    Id = make_trade(Agent, Seller, Market, Bid),
    io:format("Agent ~p buy in ~p~n", [agent:id(Agent), Id]),
    agent:send(Seller, {trade, buy, Id});
trade_with_agent(Agent, #trade_task{mode=sell, market=Market}, Bid) ->
    Buyer = Market:bid_agent_id(Bid),
    Id = make_trade(Buyer, Agent, Market, Bid),
    io:format("Agent ~p sell in ~p~n", [agent:id(Agent), Id]),
    agent:send(Buyer, {trade, sell, Id}).


In order to make it clear that it's one function, I'd make sure never
to put two different functions back-to-back, with no whitespace
between them. Thus, the fact that the second function clause follows
with no whitespace is a good indicator that it's the same function.

However, sometimes, whitespace between function clauses makes it
easier to read the code. Then, I would use a single blank line.

You can also use comments to delineate functions. A good convention, I
think, is to put something like this ahead of the first function
clause:

%% trade_with_agent/3 -> ok.
%%
trade_with_agent(Agent, #trade_task{mode=buy, market=Market}, Bid) ->
    Seller = Market:bid_agent_id(Bid),
    Id = make_trade(Agent, Seller, Market, Bid),
    io:format("Agent ~p buy in ~p~n", [agent:id(Agent), Id]),
    agent:send(Seller, {trade, buy, Id});


Sometimes, one would like to add a description about a function:

%% trade_with_agent/3 -> ok.
%%    Description....
%%
trade_with_agent(Agent, #trade_task{mode=buy, market=Market}, Bid) ->


If I also want to put a comment for each function clause, I'd put it
inside the function:


%% trade_with_agent/3 -> ok.
%%    Description....
%%
trade_with_agent(Agent, #trade_task{mode=buy, market=Market}, Bid) ->
    %% To buy...
    Seller = Market:bid_agent_id(Bid),
    Id = make_trade(Agent, Seller, Market, Bid),
    io:format("Agent ~p buy in ~p~n", [agent:id(Agent), Id]),
    agent:send(Seller, {trade, buy, Id});
trade_with_agent(Agent, #trade_task{mode=sell, market=Market}, Bid) ->
    %% to sell...
    Buyer = Market:bid_agent_id(Bid),


Of course, if you use Emacs, function heads, comments, and other
interesting language elements will be highlighted to your liking.


/Uffe

On Tue, 2 Jan 2001, Shawn Pearce wrote:

>Hey, I'm just curious, how do the ``expert'' Erlang programmers
>format this code:
>
>trade_with_agent(Agent, #trade_task{mode=buy, market=Market}, Bid) ->
>	Seller = Market:bid_agent_id(Bid),
>	Id = make_trade(Agent, Seller, Market, Bid),
>	io:format("Agent ~p buy in ~p~n", [agent:id(Agent), Id]),
>	agent:send(Seller, {trade, buy, Id});
>trade_with_agent(Agent, #trade_task{mode=sell, market=Market}, Bid) ->
>	Buyer = Market:bid_agent_id(Bid),
>	Id = make_trade(Buyer, Agent, Market, Bid),
>	io:format("Agent ~p sell in ~p~n", [agent:id(Agent), Id]),
>	agent:send(Buyer, {trade, sell, Id}).
>
>I find it difficult to read that they are the same function, just
>different varients of trade_with_agent/4.
>
>I'm just talking about white space and formatting conventions,
>however comments about the function's arguments are certainly
>welcome as well.
>
>--
>Shawn.
>
>  ``If this had been a real
>    life, you would have
>    received instructions
>    on where to go and what
>    to do.''
>

-- 
Ulf Wiger                                    tfn: +46  8 719 81 95
Senior System Architect                      mob: +46 70 519 81 95
Strategic Product & System Management    ATM Multiservice Networks
Data Backbone & Optical Services Division      Ericsson Telecom AB





More information about the erlang-questions mailing list