
Feedback requested: Literate programming in Java - leksak
In Python it is possible to have named arguments to functions. I have always appreciated this as this forces programmers to encode knowledge in source code at the function call, as opposed to either referring to the function declaration or getting that information supplied through the IDE.<p>One (very generic) example of this would be<p><pre><code>    f(foo=bar) 
</code></pre>
which I would expect to be easier to grok as soon as foo and bar is replaced by something of substance.<p>I have played with emulating this concept in Java, and have included a snippet which shows this concept alongside a more traditional Java-esque snippet of code.<p>The enum declaration MUL showcases the &quot;new&quot; approach and the more classical approach is demonstrated by SUB.<p>(continued in comments)
======
leksak
I think that while the code expressing MUL is harder to read, since there are
a lot more characters strewn about in a small space, it would be more
informative as to what all the parameters are and I would much more prefer to
come back to it three months from now as opposed to the classical approach.

    
    
        MUL(opcode(0x1c), funct(2), mnemonicPattern("iname rd, rs, rt"),
                Example.theMnemonicRepresentation("mul $t1, $t0, $at")
                        .isRepresentedNumericallyAs(0x71014802),
                Example.theMnemonicRepresentation("mul $v0, $a0, $v0")
                        .isRepresentedNumericallyAs(0x70821002)),
    
        SUB(0x00, 0x22, "iname rd, rs, rt",
                Example.from("sub $a0, $zero, $at", 0x12022);
    
        private static String mnemonicPattern(String m) { return m; }
        private static int opcode(int opcode) { return opcode; }
        private static int funct(int funct) { return funct; }
    

​Now, as you can see I did not have to wrangle Java much for the first three
parameters.

The Example class is used so that I do not have to write separate test cases
for my assertions nor store valuable information about truisms in different
files regarding my objects. Using it I can (in both examples) loop over an
Example set and checking that the assertions hold, viz. that my JUnit tests
can check that by inputting "mul $t1, $t0, $at" at the appropriate place
0x71014802 comes out at the other end.

For the MUL example, I just had to create a private Stub class and employ a
Builder-esque pattern to express what is shown in the example.

It has been awhile since I coded alongside other people, and would love to
receive feedback on both the Python keyword emulation as well as your thoughts
on the Example ploy.

