RE: Programming style: postfix/prefix vs. functional
- To: mathgroup at smc.vnet.net
- Subject: [mg70619] RE: [mg70587] Programming style: postfix/prefix vs. functional
- From: "David Park" <djmp at earthlink.net>
- Date: Sat, 21 Oct 2006 05:14:13 -0400 (EDT)
Will, 'Good' style depends on your purposes. If you want to communicate with other people, or read the notebook much later, then you want to go for clarity and elegance. This probably means that you do things in reasonably small steps and add textual explanation using Text cells. The function programming constructs such as '/@', '@', '@@', '//', '/.', '//.' are pretty standard and most users would be familiar with them. But if you combine a lot of these operations, probably with parentheses to establish proper precedence, in one statement, so-called 'fabulous one liners', then I think that is not good style. They are not easy to understand or debug. One thing you can do is use multiple statements in one cell, each on a separate line. Each statement will carry out a single operation, usually operating on the output of the previous statement in the cell. This way you can develop a calculation or derivation or transformation step-by-step in one cell. You can keep modifying and reevaluating as you develop the calculation. You can even intersperse Print statements between each step to annotate what you are doing. You can safely use the % and %% reference method within one cell, but it is probably bad style to use it between cells. It is very useful to learn how the notebook structure works, and how to enter the various cell types such as Title, Section, Subsection, Text, etc. I think it is a mistake to think of Mathematica as a 'calculator' or as a 'programming language', although it is partly these. I like to think of it as a piece of paper on which you are writing your mathematical ideas. It is a magical piece of paper because of its memory and active manipulation, but still a piece of paper. If you were to close up the Input cells (so they were thin blank cells) then your evaluated notebook should look like part of a well written textbook or research paper. So, in my opinion: organization, textual explanation, clear steps; that's what good style is. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Will Robertson [mailto:wspr81 at gmail.com] To: mathgroup at smc.vnet.net Hello, As a newcomer to Mathematica, I'm a little unsure on what "good style" would be in this programming language. I notice that several functions have prefix and postfix notations such as //. for ReplaceRepeated, /@ for Map, and so on. Clearly using these forms makes the code more compact, but sacrifices some level of readability. Are there guidelines or suggestions that have built up over the years of whether these are "good" or "bad" to use? If it's simply personal preference, what do you like to use? -- Many thanks, Will Robertson