|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
is_integer should always be used?
_______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: is_integer should always be used?not norwegian swede wrote:
> when programming erlang, should i always use is_integer if a float > will make the function crash? > > i havent used so much exception-prevention in erlang and it seems the > language needs a lot of extra code stuck it in ebcause of this. > > if some one passes a float to a function that only takes integers, > should i catch that exception or should i let the program crash? is_integer() is likely to make a program crash, if there are no suitable alternatives. so, if passing a float _always_ makes the program crash, then it does not matter where it crashes much most of the time. however, it may constitute a good documentation artifact to have an is_integer() guard around, just to warn the future readers of your code about function's acceptable domain. -- vlm _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: is_integer should always be used?On 2 Jul 2008, at 8:38 pm, not norwegian swede wrote: > when programming erlang, should i always use is_integer if a float > will make the function crash? Think about WHY you want to use it. Do you want to make the function crash? But you've said it will anyway. To make the crash report more helpful? Could be useful. To improve the results from the Dialyzer? Are you using it? What does it say? To express your intentions? Best of all. > > > i havent used so much exception-prevention in erlang and it seems > the language needs a lot of extra code stuck it in ebcause of this. Erlang doesn't need any more error detection code than other languages. Some of what might have been type declarations in other languages might show up as executable tests, but on the whole, Erlang tends to use if anything less. > > if some one passes a float to a function that only takes integers, > should i catch that exception or should i let the program crash? Do read Joe Armstrong's thesis. It is plain language with clear ideas and explains the Erlang design philosophy. Part of that philosophy is "let it crash". To really understand that, read the thesis. But here I'll say that you should only catch an exception if there is something useful you can DO about it. If you can figure out what to do after an exception, why didn't you write the code to do that in the first place? Generally speaking, exceptions should be handled some upper level that doesn't care too much about the details of what went wrong (except for logging) but DOES know what it can do instead. _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: is_integer should always be used?On Wednesday, July 02, 2008 at 19:51, Richard A. O'Keefe wrote:
> On 2 Jul 2008, at 8:38 pm, not norwegian swede wrote: > > when programming erlang, should i always use is_integer if a float > > will make the function crash? > > Think about WHY you want to use it. > Do you want to make the function crash? But you've said it will anyway. > To make the crash report more helpful? Could be useful. > To improve the results from the Dialyzer? Are you using it? What > does it say? > To express your intentions? Best of all. I tend to use the is_integer guard on exported functions that expect an integer because: 1. Have been told Dialyzer will do better with it. 2. It does express my intention better. 3. There might be compiler optimizations that can be done if the compiler knows the argument is an integer. Similar to what you see in Common Lisp. Also, however, I do not use the guard on nonexported functions, because I don't need the check being done more than once. This is kind of in conflict with #3, but my reasoning is that there is overhead to the check, and any sufficiently smart compiler can figure out that only integers come in as the argument based on a Dialyzer-like check, so there should be no need to incur the overhead again. My guess is that there are not, as yet, any compiler optimizations done on integers, but my hope is that there will be in the future. In the meantime, I've still got reasons 1 and 2. Is my reasoning sound, or just fantasy? Cheers, David _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: is_integer should always be used?David Mercer wrote:
> On Wednesday, July 02, 2008 at 19:51, Richard A. O'Keefe wrote: >> On 2 Jul 2008, at 8:38 pm, not norwegian swede wrote: >>> when programming erlang, should i always use is_integer if a float >>> will make the function crash? >> Think about WHY you want to use it. >> Do you want to make the function crash? But you've said it will anyway. >> To make the crash report more helpful? Could be useful. >> To improve the results from the Dialyzer? Are you using it? What >> does it say? >> To express your intentions? Best of all. > > I tend to use the is_integer guard on exported functions that expect an > integer because: > > 1. Have been told Dialyzer will do better with it. > > 2. It does express my intention better. > > 3. There might be compiler optimizations that can be done if the compiler > knows the argument is an integer. Similar to what you see in Common Lisp. > > Also, however, I do not use the guard on nonexported functions, because I > don't need the check being done more than once. This is kind of in conflict > with #3, but my reasoning is that there is overhead to the check, and any > sufficiently smart compiler can figure out that only integers come in as the > argument based on a Dialyzer-like check, so there should be no need to incur > the overhead again. I want to second the arguments given by David. His programming attitude towards the use of guards is worth understanding and following. > My guess is that there are not, as yet, any compiler optimizations done on > integers, but my hope is that there will be in the future. Well, in the native code compiler, there are some optimizations for integers, especially integers which can be proven to be within a bounded range. Kostis _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
||
|
|
Re: is_integer should always be used?not norwegian swede a écrit :
> when programming erlang, should i always use is_integer > if a float will make the function crash? I think not. I think guards should not be used for type checking, but used to write different clauses, when you want to handle floats and ints differently. Of course, I use test-driven programming, so I don't need type checking for correctness. And the tests I write are better at expressing how I intend the function to be used than a mere guard. And adding a guard for optimisation's sake would be premature, without profiling. So, as a general rule, I let it crash, I don't use a guard. > i havent used so much exception-prevention in erlang and > it seems the language needs a lot of extra code stuck it > in ebcause of this. You must not be coding in Erlang the same way as I. I have never had as little exception-handling code as when I code in Erlang. Regards, Dominic Williams http://dominicwilliams.net _______________________________________________ erlang-questions mailing list erlang-questions@... http://www.erlang.org/mailman/listinfo/erlang-questions |
| Free Forum Powered by Nabble | Forum Help |