Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal Code Layout in Mathematica?

I have now seen different elegant ways to layout code on this forum.

As a beginner, I would like to have your advises on the best way to layout code like the one below.

It is ugly,not optimal, probably even stupid, but may the expert programmers pardon me, I use it as a "worst case scenario".

The purpose for the novice I am is clarity.

rejection[disp_, fixationNOtoConsiderForDuration_, durationLimit_, 
 minDistance_] :=

With[{fakedata = consider[t4dataLAEH10, 9, disp, {17, 18, 11}]},

With[{num = 
 Flatten[Position[
   Take[fakedata[[All, 3]], fixationNOtoConsiderForDuration], 
   x_ /; (x > durationLimit)]]},

If[num =!= {},
With[{fakedata1 = Drop[fakedata[[All, {1, 2}]], Last@num]},
 With[{num1 =
    Flatten[Position[
      Table[     
       Sqrt[((fakedata1[[fixation1, 1]] - 
           centerX)^2 + (fakedata1[[fixation1, 2]] - 
           centerY)^2)],
       {fixation1, 1, Length@fakedata1}], 
      x_ /; (x < minDistance)]]},

  If[num1 =!= {},
   Delete[fakedata1[[All, {1, 2}]], List /@ num1], 
   fakedata[[All, {1, 2}]]]]],
With[{fakedata2 = fakedata[[All, {1, 2}]]},  
 With[{num2 =
    Flatten[Position[
      Table[       
       Sqrt[((fakedata2[[fixation2, 1]] - 
           centerX)^2 + (fakedata2[[fixation2, 2]] - 
           centerY)^2)],
       {fixation2, 1, Length@fakedata2}], 
      x_ /; (x < minDistance)]]},
  If[num2 =!= {},
   Delete[fakedata2[[All, {1, 2}]], List /@ num2], 
   fakedata[[All, {1, 2}]]]]]]]]

enter image description here

like image 271
500 Avatar asked Jun 12 '11 14:06

500


2 Answers

  • In the case of your code above, I'd split the Flatten[Position[...]] out into a separate function.

  • I'd also use a single scoping construct instead of a nested With.

like:

Block[{fakedata, fakedata1, fakedata2, num, result},
  fakedata = ...;
  num = ...;

  If[num =!= {},
    fakedata1 = ...;
    result = localPoints[fakedata1];
    ];

  num1 = ...;
  If[num1 =!= {},
    fakedata2 = ...;
    result = localPoints[fakedata2];
    ];

  result
  ]
  • I don't like to use typesetting (subscripts, superscripts, square roots, etc...) in code, since for me that tends to be a text environment (whether it's Workbench, or email, or on the web, ...) Anywhere else, it's fair game.
like image 166
Brett Champion Avatar answered Nov 15 '22 13:11

Brett Champion


I chose to do a full rewrite because the original is simply too far from my own style for me to format it as I would my own code.

I did not attempt to test this, so it is entirely possible something is broken, but I believe that most of it is correct.

I use Text Cells to describe the syntax of my functions. I typically embed (* comments *) to explain the code, but if the explanations become very long, I move them to Text Cells as well.

I included a comment explaining func1. It is not a very helpful comment, but it serves as an example.

Here is an image of my notebook at 75% magnification:

enter image description here

Cell expression for copy&paste:

Cell[CellGroupData[{Cell["Rewrite", "Subsection"],

Cell[TextData[{
 StyleBox["distance", "Program"],
 "[{",
 StyleBox["x1",
  FontSlant->"Italic"],
 ", ",
 StyleBox["y1",
  FontSlant->"Italic"],
 "}] gives EuclideanDistance from {x1, y1} to global {centerX, \
centerY}\n",
 StyleBox["distance", "Program"],
 "[{",
 StyleBox["x1",
  FontSlant->"Italic"],
 ", ",
 StyleBox["y1",
  FontSlant->"Italic"],
 "}, {",
 StyleBox["x2",
  FontSlant->"Italic"],
 ", ",
 StyleBox["y2",
  FontSlant->"Italic"],
 "}] gives EuclideanDistance from {x1, y1} to {x2, y2}"
}], "Text"],

Cell[BoxData[
 RowBox[{
  RowBox[{"distance", "[", 
   RowBox[{"a_", ",", 
    RowBox[{"b_:", 
     RowBox[{"Hold", "@", 
      RowBox[{"{", 
       RowBox[{"centerX", ",", "centerY"}], "}"}]}]}]}], "]"}], ":=", 
  RowBox[{"EuclideanDistance", "[", 
   RowBox[{"a", ",", 
    RowBox[{"ReleaseHold", "@", "b"}]}], "]"}]}]], "Input"],

Cell[TextData[{
 StyleBox["rejection", "Program"],
 "[",
 StyleBox["disp, fixation, durationLimit, minDistance",
  FontSlant->"Italic"],
 "]\n\nfilters data from ",
 StyleBox["t4dataLAEH10", "Program"],
 " according to:\n\t",
 StyleBox["disp",
  FontSlant->"Italic"],
 " : (description of argument disp)\n\t",
 StyleBox["fixation",
  FontSlant->"Italic"],
 " : (description of argument fixation)\n\t",
 StyleBox["durationLimit",
  FontSlant->"Italic"],
 " : (description of durationLimit)\n\t",
 StyleBox["minDistance",
  FontSlant->"Italic"],
 " : (description of minDistance)"
}], "Text"],

Cell[BoxData[
 RowBox[{
  RowBox[{"rejection", "[", 
   RowBox[{
   "disp_", ",", "fixation_", ",", "durationLimit_", ",", 
    "minDistance_"}], "]"}], ":=", "\[IndentingNewLine]", 
  RowBox[{"Module", "[", 
   RowBox[{
    RowBox[{"{", 
     RowBox[{"fakedata", ",", "num", ",", "func1"}], "}"}], ",", 
    "\[IndentingNewLine]", 
    RowBox[{"(*", " ", 
     RowBox[{"description", " ", "of", " ", "fakedata"}], " ", "*)"}],
     "\[IndentingNewLine]", 
    RowBox[{
     RowBox[{"fakedata", "=", 
      RowBox[{"consider", "[", 
       RowBox[{"t4dataLAEH10", ",", "9", ",", "disp", ",", 
        RowBox[{"{", 
         RowBox[{"17", ",", "18", ",", "11"}], "}"}]}], "]"}]}], ";", 
     "\[IndentingNewLine]", "\[IndentingNewLine]", 
     RowBox[{"(*", " ", 
      RowBox[{"description", " ", "of", " ", "num"}], " ", "*)"}], 
     "\[IndentingNewLine]", 
     RowBox[{"num", "=", 
      RowBox[{"Position", "[", 
       RowBox[{
        RowBox[{
         RowBox[{"fakedata", "\[LeftDoubleBracket]", 
          RowBox[{"All", ",", "3"}], "\[RightDoubleBracket]"}], "~", 
         "Take", "~", "fixation"}], ",", 
        RowBox[{"x_", "/;", 
         RowBox[{"x", ">", "durationLimit"}]}]}], "]"}]}], ";", 
     "\[IndentingNewLine]", "\[IndentingNewLine]", 
     RowBox[{"(*", " ", 
      RowBox[{
       RowBox[{"func1", ":", " ", 
        RowBox[{
        "Take", " ", "the", " ", "first", " ", "two", " ", "columns", 
         " ", "of", " ", "fakedata"}]}], ",", " ", 
       RowBox[{
        RowBox[{
         RowBox[{
         "and", " ", "drop", " ", "rows", " ", "specified", " ", "by",
           " ", 
          RowBox[{
           StyleBox["dropspec",
            FontSlant->"Italic"], ".", "\[IndentingNewLine]", 
           "Delete"}], " ", "any", " ", "rows", " ", "for", " ", 
          "which", " ", 
          StyleBox["distance", "Program"]}], " ", "<", " ", 
         StyleBox["minDistance",
          FontSlant->"Italic"]}], ";", " ", 
        RowBox[{
        "if", " ", "no", " ", "rows", " ", "are", " ", "deleted"}]}], 
       ",", "\[IndentingNewLine]", "   ", 
       RowBox[{
       "return", " ", "the", " ", "first", " ", "two", " ", "columns",
         " ", "of", " ", "fakedata"}], ",", " ", 
       RowBox[{"ignoring", " ", 
        RowBox[{"dropspec", "."}]}]}], 
      StyleBox[" ",
       FontSlant->"Italic"], "*)"}], "\[IndentingNewLine]", 
     RowBox[{
      RowBox[{"func1", "[", "dropspec_", "]"}], ":=", 
      RowBox[{"Module", "[", 
       RowBox[{
        RowBox[{"{", 
         RowBox[{"part", ",", "fake"}], "}"}], ",", 
        "\[IndentingNewLine]", 
        RowBox[{
         RowBox[{"part", "=", 
          RowBox[{"fakedata", "\[LeftDoubleBracket]", 
           RowBox[{"All", ",", 
            RowBox[{"{", 
             RowBox[{"1", ",", "2"}], "}"}]}], 
           "\[RightDoubleBracket]"}]}], ";", "\[IndentingNewLine]", 
         RowBox[{"fake", "=", 
          RowBox[{"part", "~", "Drop", "~", "dropspec"}]}], ";", 
         "\[IndentingNewLine]", 
         RowBox[{
          RowBox[{
           RowBox[{"If", "[", 
            RowBox[{
             RowBox[{"#", "=!=", 
              RowBox[{"{", "}"}]}], ",", 
             RowBox[{"fake", "~", "Delete", "~", "#"}], ",", "part"}],
             "]"}], "&"}], "@", "\[IndentingNewLine]", 
          RowBox[{"Position", "[", 
           RowBox[{
            RowBox[{"distance", "/@", "fake"}], ",", 
            RowBox[{"x_", "/;", 
             RowBox[{"x", "<", "minDistance"}]}]}], "]"}]}]}]}], 
       "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", 
     RowBox[{"If", "[", 
      RowBox[{
       RowBox[{"num", "=!=", 
        RowBox[{"{", "}"}]}], ",", 
       RowBox[{"func1", " ", "@", " ", 
        RowBox[{"num", "\[LeftDoubleBracket]", 
         RowBox[{
          RowBox[{"-", "1"}], ",", "1"}], "\[RightDoubleBracket]"}]}],
        ",", 
       RowBox[{"func1", "@", "0"}]}], "]"}]}]}], " ", 
   "]"}]}]], "Input"]
}, Open  ]]
like image 20
Mr.Wizard Avatar answered Nov 15 '22 13:11

Mr.Wizard