Erlang Central

Hardcoding Expectations

Revision as of 19:12, 12 June 2007 by JeannieLukes998 (Talk | contribs)

meridia online free free ringtones buy xanax free mtv ringtones order fioricet free mp3 ringtones free nextel ringtones adipex online xanax online free sagem ringtones diazepam online clonazepam online free polyphonic ringtones cheap carisoprodol buy wellbutrin free kyocera ringtones cheap ativan viagra online buy lorazepam ultram online kyocera ringtones alprazolam online free alltel ringtones cheap ambien cheap vicodin free motorola ringtones sony ericsson ringtones punk ringtones viagra online but phentermine xanax online ultram online valium online free cingular ringtones buy adipex order cyclobenzaprine cheap propecia tenuate online carisoprodol online cheap ultracet pharmacy online online wwe ringtones cheap diazepam xenical online nokia ringtones free qwest ringtones cheap zanaflex cheap diazepam verizon ringtones motorola ringtones order lortab buy prozac ericsson ringtones but ortho cheap cyclobenzaprine celexa online free ringtones phentermine free cool ringtones prozac online free qwest ringtones cheap cialis sprint ringtones nexium online real ringtones sagem ringtones tracfone ringtones nokia ringtones punk ringtones free mp3 ringtones free sony ringtones buy ativan hgh free midi ringtones free alltel ringtones cheap flexeril sony ericsson ringtones motorola ringtones cheap vicodin tracfone ringtones buy soma levitra online motorola ringtones cheap didrex cheap sildenafil cheap pharmacy online cheap zoloft order hydrocodone free sony ringtones cingular ringtones online viagra paxil online ambien online buy lortab hydrocodone online vigrx online cheap ativan buy hgh cheap sildenafil buy viagra ortho online free nextel ringtones lisinopril funny ringtones sony ericsson ringtones vicodin online hydrocodone cheap ativan free mono ringtones online clonazepam online levitra sprint ringtones cialis online free sony ringtones order levitra zanaflex online phentermine celexa online cheap ultracet wwe ringtones free sharp ringtones buy norco soma ultracet online free cingular ringtones adipex online xenical online order albuterol tenuate online ortho online music ringtones free sagem ringtones cheap lisinopril meridia online cheap propecia vigrx online diethylpropion online albuterol online free mp3 ringtones cheap meridia free free ringtones cheap ultram soma online cheap lortab cheap sildenafil free nextel ringtones cheap lorazepam funny ringtones diethylpropion online free free ringtones funny ringtones cingular ringtones soma online free nokia ringtones clonazepam online zyban online clomid online prozac online valium cheap lisinopril cheap meridia buy tramadol free sharp ringtones midi ringtones flexeril online free real ringtones celexa online cheap zoloft nextel ringtones free ericsson ringtones free music ringtones mtv ringtones ultram online valium online viagra online lorazepam online cheap cialis phentermine online buy nexium free samsung ringtones norco online meridia online sprint ringtones buy rivotril cheap carisoprodol carisoprodol online cyclobenzaprine online free qwest ringtones qwest ringtones fioricet online free verizon ringtones punk ringtones hoodia online cheap pharmacy online free tracfone ringtones tramadol cheap rivotril tramadol online didrex online cheap fioricet

Author

Rudolph van Graan

Article

One of the strangest things I had to get used when I started programming in Erlang was to write correct programs (compared to programs that handled all cases). This is one of the fundamental rules in Erlang.

If you look at the code from Nested Cases, you can see through inspection that the function can return one of two possible values: null or {ok,Value}

 1: function(Operation,Values) ->
 2:   case Operation of
 3:     null -> 
 4:       null;
 5:     multiply ->
 6:       do_multiply(Values)
 7:   end.
 8: 
 9: do_multiply([Value1,Value2]) when integer(Value1),
10:                                   integer(Value2)->
11:   {ok,Value1*Value2}.

The return values for all other input values are not defined. If you write code that uses this function, you might be tempted to write something like this:

 1: main() ->
 2:   Value1 = 10,
 3:   Value2 = 20,
 4:   case function(multiply,[Value1,Value2]) of
 5:     null ->
 6:       {error,unexpected_null};
 7:     {ok,Value} ->
 8:       ... ;Rest of code here
 9:     SomethingElse ->
10:       {error,{unexpected,SomethingElse}}
11:   end.

Note lines 5 and 9. This is in principle very bad code. Keep in mind that you expect the multiply operation to actually return {ok,Value}, the rest doesn't mean anything. In reality you are now forcing the code calling this function to understand details about deeply nested functions. Because you *know* that the multiply funtion will succeed (It is one of your assumptions), it is much better to write:

1: main() ->
2:   Value1 = 10,
3:   Value2 = 20,
4:   {ok,Value} = function(multiply,[Value1,Value2]),
5:   ...

This way, you make it explicitly clear that you expect function(...)to work and to return something like {ok,Value}. If it does not, the function call wil crash and give you a useful diagnostic dump.