Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    Loops are the hardest

    There is an old truism in software engineering that 199 out of 200 job applicants can't code. Like, forget fizz buzz, can't even write a loop.

    I don't know if that's true. Guess I got lucky. And I don't interview that much anyway.

    But I do work with a lot of brilliant engineers in my React + D3 workshops. Go into a company, meet with a team who wants to learn some new tech, show them around for a day or two, and create a better team.

    It's great.

    My workshops involve a lot of coding. Practice makes perfect, and I find that a little struggle really helps the learning process.

    You know, code a bit, give them a challenge, let them solve it for a few minutes, show them how it's done. It engages their brain, lets my voice rest a bit, and makes the workshop more interactive.

    People love it.

    Wanna know what all these brilliant, gainfully employed engineers struggle with the most?

    My hardest example is a color swatch. I show them this:

    See the Pen d3 color scale by Swizec Teller (@swizec) on CodePen.

    It's a row of colorful rectangles that’s using D3 to get colors and React to render components in a loop.

    The idea is to show workshop participants how React makes code reuse simple, that D3 doesn't have to be scary, and that all the basic JavaScript and programming concepts work great. It's just code, the same kinda code you write all day.

    Then I ask them to turn this row into a checkerboard.

    See the Pen d3 color checkerboard by Swizec Teller (@swizec) on CodePen.

    And I don't think anyone has ever solved it.

    We're not talking beginners here. These are hard-working, smart engineers. The kind of people who grok Redux in an afternoon. Learn React in a few hours. Keep massive engineering infrastructure working smoothly every day. The kind of software projects that would make me shit my pants. They do it every day. It's easy.

    Systems, processes, really complex fucking things. All easy. Many have forgotten more about software engineering than I ever knew.

    Changing a loop into a nested loop? Impossible.

    I mean, the simplest solution to my challenge is changing this 👇

     render() {
        return (
          <g>
            {d3.range(20).map(i => (
              <swatch color={this.colors[i]} width={this.width.step()} x={this.width(i)} y="0">
            ))}
          </swatch></g>
        )
      }
    

    To this 👇

     render() {
        return (
          <g>
            {d3.range(20).map(j => d3.range(20).map(i => (
              <swatch color="{this.colors[j%2" ?="" i="" :="" this.colors.length-i-1]}="" width={this.width.step()} x={this.width(i)} y={j*20}>
            )))}
          </swatch></g>
        )
      }
    

    Wrap the iteration in another iteration, use that to change the y coordinate. When you're on an even row, take colors from the left, take it from the right on odd rows.

    That's all. A nested loop problem.

    When we solve it together as a class, I usually create a <Row> component and use that in a loop. Makes nesting easier to see and makes benefits of React more apparent.

    Maybe I have to change this example. Maybe loops are hard and I'm just delusional.

    Do people get so used to solving huge engineering problems inside massive systems that they forget how to write code?

    I know designing systems is super hard and takes a special kind of brain. It's a whole different skill. How do you make this random process talk to that other mostly random process? How do you design your architecture so 50 engineers can work together? 1000 engineers? What about 10,000?

    But at the cost of forgetting how loops work? 🧐

    I'm both stumped and intrigued.

    Published on February 15th, 2018 in Front End, Learning, Technical

    Did you enjoy this article?

    Continue reading about Loops are the hardest

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

    Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created by Swizec with ❤️