from Hacker News

Smaller Code, Better Code

by jpt4 on 2/4/17, 5:13 AM with 157 comments

  • by arcfide on 2/5/17, 6:44 AM

    As the author of this code in question, I'd like to make the offer to the Hacker News community and anyone at large. I'll do a live screen cast demonstration for interested persons and walk you through the entire compiler in 30 minutes to 1 hour. In the end you won't have a complete understanding of the compiler, but if you have reasonable prior programming experience, I claim that you will have a better, more full, and complete understanding of the compiler than if you had spent the same amount of time learning most other compiler designs. At that point, you would be able to continue your own self-study and would be able to start making contributions to the compiler rather quickly. This is an offer to demystify the code to people so that they have an opportunity to see how it really does make the whole compiler simpler and easier to work with.

    If people express interest, I'll run such a live session and let people judge for themselves what they think of the code and my approach to "simplicity" after they've been introduced personally to the code base.

  • by natch on 2/4/17, 10:38 PM

    From the project:

    ...

    rth,←' A zs;A rs=scl(r.v(0));rr##mf(zs,rs,p);if(c==1){z.v=zs.v;R;}\',nl

      rth,←'  array v=array(z.s,zs.v.type());v(0)=zs.v(0);\',nl
    
      rth,←'  DO(c-1,rs.v=r.v(i+1);rr##mf(zs,rs,p);v(i+1)=zs.v(0))z.v=v;)\',nl
    
      rth,←' DL(zz,if(rr##scl){rr##df(z,l,r,p);R;}\',nl
    
    ...

    No.

    And commit messages like "Hopefully that does it." No again.

  • by burgerdev on 2/4/17, 8:26 PM

    At first I was wondering how he managed to write a compiler in 750 loc. Then I noticed it's for APL, which I would call terse:

      Y0←{⊃,/((⍳≢⊃n⍵)((⊣sts¨(⊃l),¨∘⊃s),'}',nl,⊣ste¨(⊃n)var¨∘⊃r)⍵),'}',nl}
    
    See also https://en.wikipedia.org/wiki/APL_(programming_language)#Exa...
  • by franciscop on 2/4/17, 8:05 PM

    The numbers are nothing like this, but I had a really similar experience to the author when doing Umbrella JS. With exceptions, but I've tried to keep every function down to few lines of code by doing heavy code reuse:

        // src/addclass/addclass.js
        // Add class(es) to the matched nodes
        u.prototype.addClass = function () {
          return this.eacharg(arguments, function (el, name) {
            el.classList.add(name);
          });
        };
    
    While they don't do exactly the same (Umbrella JS is more flexible but jQuery supports IE9), compare that to jQuery's addClass():

        addClass: function( value ) {
        	var classes, elem, cur, curValue, clazz, j, finalValue,
        		i = 0;
    
        	if ( jQuery.isFunction( value ) ) {
        		return this.each( function( j ) {
        			jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
        		} );
        	}
    
        	if ( typeof value === "string" && value ) {
        		classes = value.match( rnothtmlwhite ) || [];
    
        		while ( ( elem = this[ i++ ] ) ) {
        			curValue = getClass( elem );
        			cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );
    
        			if ( cur ) {
        				j = 0;
        				while ( ( clazz = classes[ j++ ] ) ) {
        					if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
        						cur += clazz + " ";
        					}
        				}
    
        				// Only assign if different to avoid unneeded rendering.
        				finalValue = stripAndCollapse( cur );
        				if ( curValue !== finalValue ) {
        					elem.setAttribute( "class", finalValue );
        				}
        			}
        		}
        	}
    
        	return this;
        },
  • by rakoo on 2/4/17, 7:56 PM

  • by finin on 2/4/17, 9:47 PM

    I've found the when teaching, I sometimes work on an example program too much, producing what I think is elegant and compact code, but that the students find hard to understand. I suspect that the same may be true when I am collaborating with others on a program. There can be value in writing code in a straightforward, easy to comprehend style.
  • by jcoffland on 2/5/17, 8:40 AM

    It's interesting to note that the author has written more lines here in this thread than are contained in the compiler in question. The English language is not nearly as concise as APL.
  • by dude01 on 2/4/17, 9:04 PM

    Woah! From the article: "added roughly 4,062,847 lines of code to the code base, and deleted roughly 3,753,677".
  • by skybrian on 2/5/17, 8:41 AM

    It seems like there is a missing explanation of the language this compiler compiles and why someone would want to use it? (Searches on "dfns" and "co-dfns" don't find much.)
  • by fourier on 2/13/17, 7:59 PM

  • by jfoutz on 2/5/17, 12:28 AM

    As pointed out in paip, clarity and concision are at odds. It takes good taste to balance the two.
  • by nattaylor on 2/4/17, 7:41 PM

    >for every one of those 750 lines, I've had to examine, rework, and reject around 5400 lines of code.

    I guess there's no such thing as "good enough" with a compiler?

    Those are staggering numbers to me. Kudos to the author.

  • by arcfide on 2/13/17, 7:45 PM

    The live session is up and running now. You can find more information about the stream and ask your questions at the following post:

    https://news.ycombinator.com/item?id=13638086

  • by known on 2/5/17, 11:45 AM

  • by n0mad01 on 2/5/17, 12:46 AM

    thats roughly 1369 loc added per commit or 1855 loc per day.
  • by edblarney on 2/5/17, 12:44 AM

    Smaller is better, but that does not mean 'fancy pants super dense cryptic code'.

    I think 'simpler' would be a better term than 'smaller'.

    Also - every line of code has cost. A lot of cost. Maintenance of code and complexity is not only expensive, but it adds to the maintenance of other code.

    So less code to solve the problem is almost always better.