Readability: Difference between revisions
m (Adám Brudzewsky moved page Pornography to Readability: Avoid censorship) |
No edit summary |
||
Line 1: | Line 1: | ||
'' | Maintaining readability of APL can take a special effort. It is easy to write very dense code, and the mathematical look of APL can encourage usage of single-letter names. Traditionally, APLers use the term ''pornography'' to describe code that is hard to read, or uses unusual constructs. [[Code golf]] often results in pornographic code. | ||
== Examples == | == Examples == | ||
In [[Books#APL_.E2.80.95_An_Interactive_Approach|APL – an Interactive Approach]], the authors the authors describe the following code as “almost pornographic”:<!--- Intentionally not syntax coloured ---> | === Gilman & Rose === | ||
In [[Books#APL_.E2.80.95_An_Interactive_Approach|APL – an Interactive Approach]], the authors the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:<!--- Intentionally not syntax coloured ---> | |||
:<code>r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5</code> | :<code>r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5</code> | ||
=== IBM === | |||
The [[APL2]] Idiom list includes the following entry: | The [[APL2]] Idiom list includes the following entry: | ||
:{| style=width:100% | :{| style=width:100% | ||
|<source lang=apl inline>X←'line1',0⍴Y←'line2'</source>||<source lang=apl inline>⍝ Pornography. Combining two lines into one.</source> | |<source lang=apl inline>X←'line1',0⍴Y←'line2'</source>||<source lang=apl inline>⍝ Pornography. Combining two lines into one.</source> | ||
|} | |} | ||
This was a common technique before [[Left]] was added to the language: | |||
:<source lang=apl inline>X←'line1' ⊣ Y←'line2'</source> | |||
The Diamond [[statement separator]] (<source lang=apl inline>⋄</source>) provides an alternative means of inlining multiple statements. | |||
=== Morten Kromberg === | |||
[[Morten Kromberg]] asked one of his colleagues to “Please avoid this kind of pornography:” | [[Morten Kromberg]] asked one of his colleagues to “Please avoid this kind of pornography:” | ||
:<source lang=apl inline> | :<source lang=apl inline> | ||
ns(⍎container.⎕NS)←⍬ | ns(⍎container.⎕NS)←⍬ | ||
</source> | </source> | ||
Avoiding the unusual [[modified assignment]] (using as function the 2-[[train]] <source lang=apl inline>⍎⎕NS</source>) makes for a much more readable version would be: | |||
:<source lang=apl inline> | :<source lang=apl inline> | ||
ns←⍎ns container.⎕NS ⍬ | ns←⍎ns container.⎕NS ⍬ | ||
</source> | </source> | ||
This makes it clearer that the name <source lang=apl inline>ns</source> is reassigned to a new [[namespace]] inside the <source lang=apl inline>container</source> namespace, with the new namespace taking the original value of <source lang=apl inline>ns</source> as name. |
Revision as of 16:41, 24 September 2020
Maintaining readability of APL can take a special effort. It is easy to write very dense code, and the mathematical look of APL can encourage usage of single-letter names. Traditionally, APLers use the term pornography to describe code that is hard to read, or uses unusual constructs. Code golf often results in pornographic code.
Examples
Gilman & Rose
In APL – an Interactive Approach, the authors the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:
r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5
IBM
The APL2 Idiom list includes the following entry:
X←'line1',0⍴Y←'line2'
⍝ Pornography. Combining two lines into one.
This was a common technique before Left was added to the language:
X←'line1' ⊣ Y←'line2'
The Diamond statement separator (⋄
) provides an alternative means of inlining multiple statements.
Morten Kromberg
Morten Kromberg asked one of his colleagues to “Please avoid this kind of pornography:”
ns(⍎container.⎕NS)←⍬
Avoiding the unusual modified assignment (using as function the 2-train ⍎⎕NS
) makes for a much more readable version would be:
ns←⍎ns container.⎕NS ⍬
This makes it clearer that the name ns
is reassigned to a new namespace inside the container
namespace, with the new namespace taking the original value of ns
as name.