-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fd1315
commit 1534cb6
Showing
8 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-module(col_width). | ||
|
||
-record(rec, {field1, field2, field3}). | ||
|
||
-export([bad/2, good/2]). | ||
|
||
%$ @doc too wide | ||
bad([#rec{field1 = FF1, field2 = FF2, field3 = FF3}, #rec{field1 = BF1, field2 = BF2, field3 = BF3} | Rest], Arg2) -> | ||
other_module:bad(FF1, FF2, FF3, BF1, BF2, BF3, bad(Rest, Arg2)). | ||
|
||
%% @doc good (< 80 chars) | ||
good([Foo, Bar | Rest], Arg2) -> | ||
#rec{field1 = FF1, field2 = FF2, field3 = FF3} = Foo, | ||
#rec{field1 = BF1, field2 = BF2, field3 = BF3} = Bar, | ||
other_module:good(FF1, FF2, FF3, BF1, BF2, BF3, good(Rest, Arg2)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-module(existing_style). | ||
|
||
-export([bad/0, good/0]). | ||
|
||
bad() -> | ||
% existing code | ||
List = [ {elem1, 1} | ||
, {elem2, 2} | ||
% new code (not respecting the format) | ||
, {elem3, 3}, {elem4, 4}, | ||
{elem5, 5} | ||
], | ||
other_module:call(List). | ||
|
||
good() -> | ||
% existing code | ||
List = [ {elem1, 1} | ||
, {elem2, 2} | ||
% new code (respecting the format) | ||
, {elem3, 3} | ||
, {elem4, 4} | ||
, {elem5, 5} | ||
], | ||
other_module:call(List). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
%%% @doc Mixing priv and public functions | ||
-module(bad). | ||
|
||
-export([public1/0, public2/0]). | ||
|
||
public1() -> private3(atom1). | ||
|
||
private1() -> atom2. | ||
|
||
public2() -> private2(private1()). | ||
|
||
private2(Atom) -> private3(Atom). | ||
|
||
private3(Atom) -> Atom. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
%%% @doc associated functions are closer | ||
-module(better). | ||
|
||
-export([public1/0, public2/0]). | ||
|
||
public1() -> | ||
case application:get_env(atom_for_public_1) of | ||
{ok, X} -> public1(X); | ||
_ -> throw(cant_do) | ||
end. | ||
public1(X) -> private3(X). % This is a private function but it's obviously related just to the one before | ||
|
||
public2() -> private2(private1()). | ||
|
||
private1() -> atom2. | ||
|
||
private2(Atom) -> private3(Atom). | ||
|
||
private3(Atom) -> Atom. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-module(good). | ||
|
||
-export([public1/0, public2/0]). | ||
|
||
public1() -> | ||
case application:get_env(atom_for_public_1) of | ||
{ok, X} -> private3(X); | ||
_ -> throw(cant_do) | ||
end. | ||
|
||
public2() -> private2(private1()). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PRIVATE FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
private1() -> atom2. | ||
|
||
private2(Atom) -> private3(Atom). | ||
|
||
private3(Atom) -> Atom. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-module(indent). | ||
|
||
-export([bad/0, better/0, good/0]). | ||
|
||
%% @doc inconsistent | ||
bad() -> | ||
try | ||
ThisBlock = is:indented(with, two, spaces), | ||
that:is_good(ThisBlock) | ||
catch | ||
_:_ -> | ||
this_block:is_indented(with, four, spaces) | ||
end. | ||
|
||
%% @doc consistent, but with 4 spaces | ||
better() -> | ||
receive | ||
{this, block} -> is:indented(with, four, spaces); | ||
_That -> is:not_good() | ||
after 100 -> | ||
but:at_least(it, is, consistent) | ||
end. | ||
|
||
%% @doc good | ||
good() -> | ||
case indentation:block() of | ||
{2, spaces} -> me:gusta(); | ||
{_, _} -> not_sure:if_gusta() | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-module(nesting). | ||
|
||
-export([bad/0, good/0]). | ||
|
||
bad() -> | ||
case this:function() of | ||
has -> | ||
try too:much() of | ||
nested -> | ||
receive | ||
structures -> | ||
it:should_be(refactored); | ||
into -> | ||
several:other(functions) | ||
end | ||
catch | ||
_:_ -> | ||
dont:you("think?") | ||
end; | ||
_ -> | ||
i:do() | ||
end. | ||
|
||
good() -> | ||
case this:function() of | ||
calls -> | ||
other:functions(); | ||
that -> | ||
try do:the(internal, parts) of | ||
what -> | ||
was:done(in) | ||
catch | ||
_:the -> | ||
previous:example() | ||
end | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-module(smaller_functions). | ||
|
||
-export([bad/0, bad/1, good/0, good/1]). | ||
|
||
%% @doc function with just a case | ||
bad(Arg) -> | ||
case Arg of | ||
this_one -> should:be(a, function, clause); | ||
and_this_one -> should:be(another, function, clause) | ||
end. | ||
|
||
%% @doc usage of pattern matching | ||
good(this_one) -> is:a(function, clause); | ||
good(and_this_one) -> is:another(function, clause). | ||
|
||
|
||
%% @doc function with an internal case | ||
bad() -> | ||
InitialArg = some:initial_arg(), | ||
InternalResult = | ||
case InitialArg of | ||
this_one -> should:be(a, function, clause); | ||
and_this_one -> should:be(another, function, clause) | ||
end, | ||
some:modification(InternalResult). | ||
|
||
%% @doc usage of function clauses instead of an internal case | ||
good() -> | ||
InitialArg = some:initial_arg(), | ||
InternalResult = good(InitialArg), | ||
some:modification(InternalResult). |