News for hackers. Stuff that matters.
Re:Ask HN: What are the best programming tricks you know? by xyzal on Monday July 25, 2022 @09:34AM (#32224037)
Would you please provide an example or direct towards some explanation? I can't seem to grasp what you mean exactly.
Re:Ask HN: What are the best programming tricks you know? by substation13 on Monday July 25, 2022 @09:53AM (#32224283)
Suppose I have a procedure that plays a piano:

    const playTwinkleTwinkleLittleStar = (piano) => {
      piano.play('c');
      piano.play('c');
      piano.play('g');
      piano.play('g');
      piano.play('a');
      piano.play('a');
      piano.play('g');
    };
Now I want to have every note played twice. How can I do that?

Usual OOP solution is to have a wrapper on piano:

    const playItTwicePiano = (piano) => {
      return {
        play: (note) => {
          piano.play(note);
          piano.play(note);
        },
      };
    };

    playTwinkleTwinkleLittleStar(playItTwicePiano(piano));
However, if we instead return a description of what to do, such manipulations compose far more easily:

    const twinkleTwinkleLittleStar = () => {
      return [ 'c', 'c', 'g', 'g', 'a', 'a', 'g' ]; 
    };

    const playItTwice = (sequence) => {
      const xs = [];


      for (const x of sequence) {
        xs.push(x);
        xs.push(x);
      }

      return xs;
    };
And it's easier to test:

    it('should start with c', () => {
      const xs = twinkleTwinkleLittleStar();

      assert(xs[0] === 'c');
    });
Wherever you are building a complex mock for testing consider defunctionalization instead.
Re:Ask HN: What are the best programming tricks you know? by b0afc375b5 on Tuesday July 26, 2022 @11:55AM (#32239982)
Ahh I see. So if I may use an analogy:

I have an html page with all my details, etc. Then say I want to create a markdown version of it, or a pdf version, or a docx version.

The first approach would be to manually rewrite my details to each format, as markdown, as pdf, as docx, essentially duplicating work each time.

The second approach, which is akin to defunctionalisation, would be to separate my details (data) from the format. So that would mean I would store my data in json format, then use a cli tool/program to generate it in html format, in pdf format, or in docx format.

I hope I got the basic idea right.

Re:Ask HN: What are the best programming tricks you know? by cutthegrass2 on Tuesday July 26, 2022 @04:58AM (#32235906)
This is a great example, and feel the concept has "clicked" with me because of it. Thank you!
Re:Ask HN: What are the best programming tricks you know? by assbuttbuttass on Monday July 25, 2022 @09:41AM (#32224121)
A simple example might be a function that makes an HTTP request to a server. You could separate out a function that builds the request from a function that sends the request to the server. Then it's easy to unit test that the request is what you expect, without coding up a realistic fake for the server.
Re:Ask HN: What are the best programming tricks you know? by the_gipsy on Monday July 25, 2022 @09:45AM (#32224181)
You can't send a function/closure like data over the wire. You have to limit yourself to what kind of code you want executed in the future, but you can send or store it limitless.