Error handling for an Erlang 2D point class
I am working on a math library for an online game. I want to prevent as many errors as possible and I want to catch all errors as early as possible to simplify debugging in the long run. However, I feel like I drown in details. This is quite different from "let it crash" motto.
point
has {number(), number()}
type or better say {0, 0}
is a point. I started with Dialyzer
specs and guard statements which work as a poor man's assertions.
Here is my point
class.
-module(point).
-author("nt").
-export([is_point/1, distance/2, translate/2, pointToMap/1]).
-type point() :: {number(), number()}.
-export_type([point/0]).
is_point({X, Y}) when is_number(X), is_number(Y) -> true;
is_point(_) -> false.
distance({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
{X1 + X2, Y1 + Y2}.
pointToMap({X, Y} = A) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
#{
x => X,
y => Y
}.
%% Spec
-spec is_point(P :: point()) -> boolean().
-spec distance(A :: point(), B :: point()) -> float().
-spec translate(A :: point(), B :: point()) -> point().
-spec pointToMap(A :: point()) -> #{x:= number(), y := number()}.
{{0, 0}, {0, 0}}
is a rect
.
rect
"class" relies on point
.
-module(rect).
-author("nt").
%% API
-export([is_rect/1, contains/2]).
-type rect() :: {point:point(), point:point()}.
-export_type([rect/0]).
is_rect({{OriginX, OriginY}, {W, H}}) when is_number(OriginX), is_number(OriginY), is_number(W), is_number(H) -> true;
is_rect(_) -> false.
-spec contains(Rect, Point) -> boolean() when Rect :: rect(), Point :: point:point().
contains({{OriginX, OriginY}, {W, H}} = R, {X, Y} = P) ->
case is_rect(R) of false -> error(badarg); _ -> ok end,
case point:is_point(P) of false -> error(badarg); _ -> ok end,
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
Things are getting really massive and daunting. Given the fact there is almost no business logic I wonder how fast codebase will become a complete mess if I continue to check all the required preconditions in all functions.
Update:
I think about removing all the specs and "assertions" from the code. Functions like pointToMap
will be also removed as I don't need to rely on concrete types anymore. Something like tupleToMap
should be used instead.
point.erl
:
-module(point).
-author("nt").
-export([distance/2, translate/2]).
distance({X1, Y1}, {X2, Y2}) ->
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1}, {X2, Y2}) ->
{X1 + X2, Y1 + Y2}.
rect.erl
-module(rect).
-author("nt").
-export([contains/2]).
contains({{OriginX, OriginY}, {W, H}}, {X, Y}) ->
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
error-handling coordinate-system erlang
add a comment |
I am working on a math library for an online game. I want to prevent as many errors as possible and I want to catch all errors as early as possible to simplify debugging in the long run. However, I feel like I drown in details. This is quite different from "let it crash" motto.
point
has {number(), number()}
type or better say {0, 0}
is a point. I started with Dialyzer
specs and guard statements which work as a poor man's assertions.
Here is my point
class.
-module(point).
-author("nt").
-export([is_point/1, distance/2, translate/2, pointToMap/1]).
-type point() :: {number(), number()}.
-export_type([point/0]).
is_point({X, Y}) when is_number(X), is_number(Y) -> true;
is_point(_) -> false.
distance({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
{X1 + X2, Y1 + Y2}.
pointToMap({X, Y} = A) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
#{
x => X,
y => Y
}.
%% Spec
-spec is_point(P :: point()) -> boolean().
-spec distance(A :: point(), B :: point()) -> float().
-spec translate(A :: point(), B :: point()) -> point().
-spec pointToMap(A :: point()) -> #{x:= number(), y := number()}.
{{0, 0}, {0, 0}}
is a rect
.
rect
"class" relies on point
.
-module(rect).
-author("nt").
%% API
-export([is_rect/1, contains/2]).
-type rect() :: {point:point(), point:point()}.
-export_type([rect/0]).
is_rect({{OriginX, OriginY}, {W, H}}) when is_number(OriginX), is_number(OriginY), is_number(W), is_number(H) -> true;
is_rect(_) -> false.
-spec contains(Rect, Point) -> boolean() when Rect :: rect(), Point :: point:point().
contains({{OriginX, OriginY}, {W, H}} = R, {X, Y} = P) ->
case is_rect(R) of false -> error(badarg); _ -> ok end,
case point:is_point(P) of false -> error(badarg); _ -> ok end,
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
Things are getting really massive and daunting. Given the fact there is almost no business logic I wonder how fast codebase will become a complete mess if I continue to check all the required preconditions in all functions.
Update:
I think about removing all the specs and "assertions" from the code. Functions like pointToMap
will be also removed as I don't need to rely on concrete types anymore. Something like tupleToMap
should be used instead.
point.erl
:
-module(point).
-author("nt").
-export([distance/2, translate/2]).
distance({X1, Y1}, {X2, Y2}) ->
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1}, {X2, Y2}) ->
{X1 + X2, Y1 + Y2}.
rect.erl
-module(rect).
-author("nt").
-export([contains/2]).
contains({{OriginX, OriginY}, {W, H}}, {X, Y}) ->
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
error-handling coordinate-system erlang
add a comment |
I am working on a math library for an online game. I want to prevent as many errors as possible and I want to catch all errors as early as possible to simplify debugging in the long run. However, I feel like I drown in details. This is quite different from "let it crash" motto.
point
has {number(), number()}
type or better say {0, 0}
is a point. I started with Dialyzer
specs and guard statements which work as a poor man's assertions.
Here is my point
class.
-module(point).
-author("nt").
-export([is_point/1, distance/2, translate/2, pointToMap/1]).
-type point() :: {number(), number()}.
-export_type([point/0]).
is_point({X, Y}) when is_number(X), is_number(Y) -> true;
is_point(_) -> false.
distance({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
{X1 + X2, Y1 + Y2}.
pointToMap({X, Y} = A) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
#{
x => X,
y => Y
}.
%% Spec
-spec is_point(P :: point()) -> boolean().
-spec distance(A :: point(), B :: point()) -> float().
-spec translate(A :: point(), B :: point()) -> point().
-spec pointToMap(A :: point()) -> #{x:= number(), y := number()}.
{{0, 0}, {0, 0}}
is a rect
.
rect
"class" relies on point
.
-module(rect).
-author("nt").
%% API
-export([is_rect/1, contains/2]).
-type rect() :: {point:point(), point:point()}.
-export_type([rect/0]).
is_rect({{OriginX, OriginY}, {W, H}}) when is_number(OriginX), is_number(OriginY), is_number(W), is_number(H) -> true;
is_rect(_) -> false.
-spec contains(Rect, Point) -> boolean() when Rect :: rect(), Point :: point:point().
contains({{OriginX, OriginY}, {W, H}} = R, {X, Y} = P) ->
case is_rect(R) of false -> error(badarg); _ -> ok end,
case point:is_point(P) of false -> error(badarg); _ -> ok end,
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
Things are getting really massive and daunting. Given the fact there is almost no business logic I wonder how fast codebase will become a complete mess if I continue to check all the required preconditions in all functions.
Update:
I think about removing all the specs and "assertions" from the code. Functions like pointToMap
will be also removed as I don't need to rely on concrete types anymore. Something like tupleToMap
should be used instead.
point.erl
:
-module(point).
-author("nt").
-export([distance/2, translate/2]).
distance({X1, Y1}, {X2, Y2}) ->
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1}, {X2, Y2}) ->
{X1 + X2, Y1 + Y2}.
rect.erl
-module(rect).
-author("nt").
-export([contains/2]).
contains({{OriginX, OriginY}, {W, H}}, {X, Y}) ->
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
error-handling coordinate-system erlang
I am working on a math library for an online game. I want to prevent as many errors as possible and I want to catch all errors as early as possible to simplify debugging in the long run. However, I feel like I drown in details. This is quite different from "let it crash" motto.
point
has {number(), number()}
type or better say {0, 0}
is a point. I started with Dialyzer
specs and guard statements which work as a poor man's assertions.
Here is my point
class.
-module(point).
-author("nt").
-export([is_point/1, distance/2, translate/2, pointToMap/1]).
-type point() :: {number(), number()}.
-export_type([point/0]).
is_point({X, Y}) when is_number(X), is_number(Y) -> true;
is_point(_) -> false.
distance({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1} = A, {X2, Y2} = B) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
case is_point(B) of false -> error(badarg); _ -> ok end,
{X1 + X2, Y1 + Y2}.
pointToMap({X, Y} = A) ->
case is_point(A) of false -> error(badarg); _ -> ok end,
#{
x => X,
y => Y
}.
%% Spec
-spec is_point(P :: point()) -> boolean().
-spec distance(A :: point(), B :: point()) -> float().
-spec translate(A :: point(), B :: point()) -> point().
-spec pointToMap(A :: point()) -> #{x:= number(), y := number()}.
{{0, 0}, {0, 0}}
is a rect
.
rect
"class" relies on point
.
-module(rect).
-author("nt").
%% API
-export([is_rect/1, contains/2]).
-type rect() :: {point:point(), point:point()}.
-export_type([rect/0]).
is_rect({{OriginX, OriginY}, {W, H}}) when is_number(OriginX), is_number(OriginY), is_number(W), is_number(H) -> true;
is_rect(_) -> false.
-spec contains(Rect, Point) -> boolean() when Rect :: rect(), Point :: point:point().
contains({{OriginX, OriginY}, {W, H}} = R, {X, Y} = P) ->
case is_rect(R) of false -> error(badarg); _ -> ok end,
case point:is_point(P) of false -> error(badarg); _ -> ok end,
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
Things are getting really massive and daunting. Given the fact there is almost no business logic I wonder how fast codebase will become a complete mess if I continue to check all the required preconditions in all functions.
Update:
I think about removing all the specs and "assertions" from the code. Functions like pointToMap
will be also removed as I don't need to rely on concrete types anymore. Something like tupleToMap
should be used instead.
point.erl
:
-module(point).
-author("nt").
-export([distance/2, translate/2]).
distance({X1, Y1}, {X2, Y2}) ->
math:sqrt(math:pow((X2 - X1), 2) + math:pow(Y2 - Y1, 2)).
translate({X1, Y1}, {X2, Y2}) ->
{X1 + X2, Y1 + Y2}.
rect.erl
-module(rect).
-author("nt").
-export([contains/2]).
contains({{OriginX, OriginY}, {W, H}}, {X, Y}) ->
((OriginX < X) and (X < (OriginX + W))) and ((OriginY < Y) and (Y < (OriginY + H))).
error-handling coordinate-system erlang
error-handling coordinate-system erlang
edited Dec 20 at 13:39
asked Dec 17 at 14:32
Nik
1073
1073
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209832%2ferror-handling-for-an-erlang-2d-point-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209832%2ferror-handling-for-an-erlang-2d-point-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown