Coding Style. Using Operator vs Function Call
Operators and Syntactic Shortcuts vs Function Call
- Computer Science.
- Programing Language Syntax Design and Coding Style.
- Wolfram language coding style.
- i used to love the syntactic shortcuts
- e.g.
( #1+#2 &)forFunction[{x,y}, x+y]f /@ xforMap[f,x]f @@ xforApply[f,x]
but now i prefer full function names. even sometimes prefer
SameQ[x,y]insteadx === yAnd[x,y]insteadx && y
Because,
- Full names makes the code understandable to non-experts of the language.
- Also, completion makes it easier to type.
- Also, with brackets, it makes editing the code easier via syntactic units. E.g. You can select or delete a semantic unit via syntax.
(* lots operators *) Graphics3D[ Replace[Map[(Tube /@ Subsets[Tuples@Transpose@{#, # + 1}, {2}] &), CoordinateBoundsArray@Table[{0, 3}, 3], {-2}], v_List :> ((With[{xx = # . #}, If[xx < 0.00001, #, #/xx]]) &)@ v, {-2}], ViewVector -> {-.5, -.2, -1}]
(* less operators, more function call syntax *) Graphics3D[ Replace[Map[ Function[x, Map[ Tube, Subsets[Tuples[Transpose[{x, x + 1}]], {2}]] ] , CoordinateBoundsArray[Table[{0, 3}, 3]], {-2}], v_List :> Function[x, (With[{y = x . x}, If[y < 0.00001, x, x/y]]) ] [v], {-2}], ViewVector -> {-.5, -.2, -1}]
But operators are necessary
with operators, we have
x = (-b + √(b^2 - 4 * a * c) ) / (2 * a)
in pure functional form, we have
assign(x, div( plus(-b, sqrt( plus( power(b, 2), neg(times(4, a, c))))), times(2, a)))
so, isn't operators much easier to read.
so, why should you prefer function form?
i think it comes down to, how frequent are the operation, and how standardized is it.
for arithmetics,
+ - * /
and assignment
=,
they are universal,
you should use the operator form.
now, in Wolfram language, arguably, also the forms mentioned on this page.
Programing Language Operators
- What is Function, What is Operator. (2010)
- Programing Language: Why You Need Operators
- Programing Language: What is the Definition of Operator
- Programing Language: Necessity of Operator Overload
- Programing Language: Emoji and Math Symbols in Function Name, Variable, Operator
- Logical Operators, Truth Table, Unicode (2010)
- Coding Style. Using Operator vs Function Call
- Coding Style: Abuse of Logic Operators (Short-Circuit) as Control Flow (2015)