[Jprogramming] Feedback on beginner's code

neitzel at gaertner.de neitzel at gaertner.de
Mon Mar 14 19:30:35 UTC 2016


> As a fellow beginner (learning for well over a year, on and off), I'll
> show a way I would do it:
> isAnagramOf =: -:&:(-.&' '@:/:~@:tolower)

I'd like to add some advice for J newbies:

	During the first two months (perhaps even two years, or two
	decades), resist any urge to put more than five things to
	the right of an =:

Factor out every small sub-unit you can.  That simplifies debugging A LOT.

Taking the above:

	normalize_string =: -.&' '@:/:~@:tolower
	isAnagramOf =: -:&:normalize_string

You certainly get the idea.  As you can see, the  -.&' '@:/:~@:tolower
has still way more than five words (namely 8) and I'm not really sure
that its author is aware that the ~ refers to not just the /: but
to the whole  -.&' '@:/:  pipeline to its left.

Again, smaller sub-units help to avoid such accidents:

	sort =: /:~
	de_space =: -.&' '
	normalize =: de_space at sort@tolower
	isAnagramOf =: -:&normalize

Almost every time, these tiny sub-units provide something useful even
beyond of the current context ("anagrams").  They are certainly easier
to test/verify.

It also makes reasoning about the ranks of the components easier.
In consequence, it becomes easier to make an educated selection
between  @ vs @:  or & vs &:  -- much better than to stick blindly
to some mysterious rule of thumb.

							Martin Neitzel


More information about the Programming mailing list