Defensive programming

orbitz <>
Wed Mar 29 02:05:23 CEST 2006


I would say so.  In your defensive code, if your accept fails, what  
do you do? You the process.  So how is this different than just  
letting the a badmatch happen and failing?  Your defensive code is  
much more complex with no benefit.  The supervisor will handle the  
error for you and restart or stop it depending on what you told it to  
do.  Much simpler.


On Mar 28, 2006, at 5:36 PM, Pupeno wrote:

> Hello,
> I am used to defensive programming and it's hard for me to program  
> otherwise.
> Today I've found this piece of code I wrote some months ago:
>
> acceptor(tcp, Module, LSocket) ->
>     case gen_tcp:accept(LSocket) of
>         {ok, Socket} ->
>             case Module:start() of
>                 {ok, Pid} ->
>                     ok = gen_tcp:controlling_process(Socket, Pid),
>                     gen_server:cast(Pid, {connected, Socket}),
>                     acceptor(tcp, Module, LSocket);
>                 {error, Error} ->
>                     {stop, {Module, LSocket, Error}}
>             end;
> 	{error, Reason} ->
> 	    {stop, {Module, LSocket, Reason}}
>     end;
>
> is that too defensive ? should I write it this way
>
> acceptor(tcp, Module, LSocket) ->
>     {ok, Socket} = case gen_tcp:accept(LSocket),
>     {ok, Pid} = Module:start()
>     ok = gen_tcp:controlling_process(Socket, Pid),
>     gen_server:cast(Pid, {connected, Socket}),
>     acceptor(tcp, Module, LSocket);
>
> ?
>
> Thanks.
> -- 
> Pupeno <> (http://pupeno.com)




More information about the erlang-questions mailing list