3 Minutes with Kent

3 Minutes with Kent

By Kent C. DoddsTechnology
Download on the App Store

All 3 Minutes with Kent episodes

  • How do I add tests to existing code

    So the last question in this email has to do with how do you know why where

    to start testing because you're adding testing to an existing project and
    so most of your tests are actually writing tests for code that exists and
    so it's not like there's no test driven development there or anything like
    that.

    And so, how do you know like how far do I go and testing when when can I

    stop and say that this component is tested? Does it have to do with code
    coverage or whatever? And my answer to that is I actually do have a blog
    post how to get started with testing or how to add tests to an existing
    code base.

    Where I talk about with just kind of in general you want to start with a

    single end-to-end test and you add some static testing and then you add the
    tooling for and unit integration and then just add over time and you can go
    take a look at that for a little bit deeper read on that, but if we're
    talking specifically about I have this component and I want to add tests to
    it where do I start and for me it comes down to thinking about use cases
    more than thinking about code coverage.

    And I actually have a blog post ofAbout that as well. But basically the

    idea is I don't really care about code coverage. Lines of code is basically
    irrelevant. All that the the code coverage report tells me is the use cases
    that I'm not covering. That's what I'm thinking about when I'm looking at
    the code coverage report.

    So if there's like an if statement that is not covered then that just says,

    oh, okay, so whatever use case that code exists for that's a use case that
    I'm not testing for because use cases are all that we care about. We care
    about this.Code working for the users whether that be developer users who
    are calling our functions or rendering our components or end users who are
    clicking the buttons were rendering whatever.

    And so you want to think about the use cases that you're covering and then

    you weigh them relative to the confidence that you need for other use cases
    of other components as well as the features that you need to build and bugs
    you need to fix and things like that.

    So it really comes down to what we said before about weighing the return on

    your investment of time for writing.Versus features that you can work on
    Now let's say that you live in a world where it doesn't matter You don't
    need to work on features you can just totally focus on tests.

    There is a stopping point for sure and the stopping point is when you feel

    like you've covered all the use cases. Now for like open source libraries
    and stuff it typically end up covering all of the use cases you get a
    hundred percent code coverage, but for applications. I never find myself in
    a situation where I can just spend a ton of time on tests.

    I need to work on features and that's where you just need to kind of weigh,

    what are the things that I am going to write test for.Versus new features
    or whatever. Hope that helps Ciao.

    3 min
  • Writing "Testable" React Components

    Hey friends this is another answer to that lengthy email from Poeter I'm

    not sure how to say that but properly sorry but the question is how do you
    write code that can be easily tested and they go at length to explain, you
    know, sometimes you write, you know smaller functions and we can test those
    easier but ultimately like, it doesn't always work that way components
    typically grow and splitting them up makes it feel harder to maintain and I
    totally agree with that.

    I have a blog post called when to break up a react component.And I

    typically preferred don't break it up if you can help it avoid breaking up
    your components unnecessarily so yeah, so I prefer having bigger components
    but then like okay, so how do I test this it's got a bunch of use effects
    and whatever else in it and then they also bring up the container or yeah
    the like the smart and dumb components sort of thing or whatever we landed
    on for the macular for that but I actually never liked that pattern either.

    I didn't think that it was a useful.Mechanism for splitting components up

    or anything it didn't make any sense to me. I mean, I understood like what
    the rules were but the rules were stupid. I thought and never got any value
    out of that, so how do we write react components or just code in general
    let's easier to test well.

    I think that if you're testing from a user's perspective and focusing not

    on building like running necessarily testable code, but just code that's
    that's sensible that makes sense like there's you're not breaking things up
    unnecessarily and you're.Associating things across like that are in the
    same vertical I guess like yeah just not breaking things up unnecessarily
    and then you test them from the user's perspective, it really doesn't end
    up mattering how complicated the code within it is necessarily especially
    when you're we're talking about unit or integration tests where you're
    mocking HTTP calls that stuff as long as you have MSW to intercept requests
    and and mock those out and then occasionally maybe mocking a third.

    Party library that's complicated or something from there it's it's just

    rendered the thing and interact with the thing the way that the user does
    and then assert on its output. I never really think too much about making
    my code testable necessarily and maybe that's just the curse of knowledge
    on me just learn to do that naturally and it's something you develop over
    time, but it's not really something.

    I'm thinking about a whole lot. I guess one thing that I do is I I

    typically avoid doing side effects in the root level of a module and I
    always put those in a function and that can help make things a little more
    testable but in general, I'm not really.

    Thinking about it too much. I hope that's sort of helpful bye.

    3 min
  • How to know when you're done testing something

    Hey your friends So I am going to for the next couple days answer some

    questions that I got in an email from somebody. I don't know how to
    pronounce this name. It looks like pejor or push actor. I'm not sure
    they're from the Netherlands. And anyway, they actually wrote me quite a
    nice email about testing JavaScript.com and just had some additional
    questions because the course mostly goes into the how to get things done
    and that doesn't get as much into like some of the philosoph well it
    actually yeah, we we get into that a bit.

    But there's no like specific lesson on how to write your code, so it's

    testable or whatever. So anyway, um, most of that knowledge I share on my
    blog. So if you go to Cansee dots.com slash blog and then type in test then
    you'll see I've got dozens of blog posts about testing and so that's kind
    of where I direct people to for when they have questions like this, but I
    thought I just answered a couple of these on the three-minute podcast.

    So for this first one, the question is what should you write tests for and

    whatnot?To write test for and why? So and they acknowledge that yes you
    want to avoid testing implementation details but how do you know that
    you're done writing tests for a component? So for one thing I actually
    recently had a question from somebody who said like should I make
    assertions that certain things don't show up in my output, you know, so
    we're asserting that the login button shows up and I can click on it but
    like should I also assert that the register button doesn't show up or
    whatever the case may be?

    And,So anyway the answer to this question is everything that you do in

    software is trade-off and it has a cost and that cost is equal for
    everything and the cost is your time. So how much time do you spend working
    on any given problem and and you weigh everything relative to the amount of
    benefit that you get for the time and it isn't exactly a science is a bit
    of an art but you generally just think okay, so I'm gonna write this test
    for this component and the reason that I'm doing it isn't just to write a
    test but it's because what I get out of writing it.

    To test is more confidence And so at every moment of the day or whenever

    you're making it a decision on what you should work on you should think
    okay so I could write some more tests for this component and maybe I've
    been working on this component working on its test or I could go implement
    this new feature or fix this bag or whatever.

    And you just have to decide okay based on the amount of cost that it takes

    from the time aspect versus the amount of benefit with more confidence or
    nice feature for more users or whatever and you just weigh those and make a
    decision on what is most valuable. So you're not just testing because you
    want to tend to.

    Ask but you're testing because you're trying to get something out of it. I

    hope that's helpful. Good luck.

    3 min
  • Rendering react elements with ifs, ternaries, switches, or object properties

    Hey there friends. So today I'm going to answer a question from Lean or

    Leon I'm not sure how to say your name sorry but this is asked in the react
    hooks channel of the discord the KCD discord under the Epic Reactive
    category and it's talking about exercise six where we show it's it's an
    exercise on managing state for asynchrony, so we've got to use effect in
    there and stuff.

    And we have this status status and that is the status of the promise. So

    whether it's idle pending reject,Ed or resolved and we render different
    things based on that. And in my solution, I just do an if statement. So if
    status is idle, then this outsift status is pending then render this also
    status is rejected then do this.

    And that I I was pretty happy with that. But I there are obviously several

    different ways to do this and one natural thing that people sometimes will
    do is a switch statement which is what this individual suggested why don't
    we do the switch statement, but then they got some pushback on that.

    Hat from a co-worker. I think that suggested using an object instead and so

    you can say here's a a object that has the status value so you have idle
    pending rejected results and each one of those is assigned a as a property
    the the value is going to be the react element that should be rendered.

    So I mentioned that I would talk about this. So in the next version of Epic

    React, so if you look in this repo on the next branch, I actually switched
    to not only TypeScript.And but I also switched to using a switch statement
    Just because I think that it makes a little bit more sense in this context.

    Like I said, I often don't use switch statements. I prefer if else or maybe

    even ternaries but in this case, it just seemed to look a little cleaner
    and so I use the switch statement there. Definitely know hard and fast rule
    on that. It's just try them both and pick the one that you dislike the
    least.

    Um, but I did mention in this discord chat that it actually is really not a

    good idea to use the object approach for something like this. And the
    reason is that,Sometimes you're gonna be switching on something like in our
    case we're displaying a Pokemon. And if you're going to create React
    Elements that reference properties of that Pokemon then you you're going to
    be accessing properties that may not exist based on the status like if you
    have idle the Pokemon could be null.

    And so if you're even though those different properties of that object

    won't actually get rendered you do have to access those properties to
    create the React elements. So, that's why you don't want to use the object
    form in this case at all. So, I hopeThat's useful and interesting and I
    hope you have an awesome day.

    Goodbye.

    3 min
  • Write the code first, then make the abstraction

    CORRECTION: I came up with the concept of AHA Programming but I did not come up with the acronym. AHA (pronounced Aha! like you just made a discovery) is an acronym I got from Cher Scarlett which stands for Avoid Hasty Abstractions.

    Hey friends, just something that I've been thinking about recently is this

    acronym that I came up with a few years ago. It's AHA and it's pronounced
    aha and so that's aha programming and it stands for avoid hasty
    abstractions and this is actually a blog post that I wrote with some time
    ago and it talks a lot about how like dry do not repeat yourself as to
    dugmatic and wet programming which is a variant of that is right everything
    twice is also.

    Two dogmatic and more we should be more mindful of the types of

    abstractions that we create and and I talk a lot about in the blog post,
    which I suggest you go take a look at but one part of this and is at kind
    of the micro level and that's what I have been thinking about recently just
    today.

    As you're working on a new feature or something is really really often in

    my programming at least I'm working through something and I start I'm
    thinking about how I'm going to implement something and I instantly decide
    okay, I'm going to need.A function that does this And so often that ends up
    being the wrong decision I end up making and especially with TypeScript
    actually.

    This is a big pain because then you have to there's just a lot of

    boilerplate with making a function. But even even not without TypeScript,
    you have to think about okay, what are the parameters what order are they
    going to be in or is it going to be an object that AD structure?

    And it just adds a lot of stuff that for code that you don't even really

    know what it's going to look like yet and and on top of that if you decide
    oh actually this function needs to change like weWe just add a lot of extra
    work and thought before we've actually written the code just by making a
    function for this thing that we think we might want to function for where
    in actuality.

    I find I'm so much more productive if I just start by writing the code and

    inlining as much as I possibly can even if that means I'm copy pasting from
    one part of the code base to another and just in line everything as much as
    you can make a big long function.

    And then before you commit at that point, you look atThe code that you've

    written and you think okay, so now what are the abstractions that I see now
    that the code has been written? And so, I find a lot of my problem comes
    with when I try to abstract the code that's not there yet.

    And I find a much more productive if I abstract the code that exists after

    I've written it. So anyway, hopefully that's helpful to you next time you
    feel the urge to write a function for code that doesn't exist yet. Maybe
    take a step back and write the code first then make the function.

    Have a wonderful day.

    3 min
  • The future is on the edge

    Hey friends the future is the edge We've got the now-fight edge thing

    CloudFlare workers things we want to be able to deploy our code and have it
    run as close to the user as possible. Before it's always just been static
    files that you put on a CDN like Akamai or Cloudflare or fastify or
    something like that because that's really easy to do.

    You just you deploy and then the provider will send these static files all

    around the world to their servers and and then users can get those files
    really quickly, but anytime you needed to get data you had to go to
    wherever the data.To the center was. And so that like pretty much doing a
    server rendered application would require that at some point you're going
    to be going to to have to go to some central place to to get that data.

    And so it's going to end up being slow. And like in particular for server

    side rendering stuff it. It's not very useful unless you are doing stuff
    with data anyway. So things are changing. We've got these contour workers,
    so you can actually run server rendered.Staff or server side logic on the
    edge.

    So close to the user and then we have fauna DB and I'm sure there are

    others like it that's the only one that I know of where the data lives on
    the edge as well. So when you do a right to the database Fana will push out
    the the right to all of its CDN clients or whatever all all of its servers
    all over the world.

    And so then you're running compute on the edge and your data is right there

    on the edge too.And so it's just outrageously fast and it's really cheap
    like very cheap. Sometimes free if it's a very small sort of thing that
    you're building, but you're latency is going to be very like I'm looking at
    Cloudflare right now.

    It says your actual latency is 16 milliseconds. It's like that's that's my

    latency on this page. I just tested again. It's 15 milliseconds. It's
    outrageous. Oh, how fast this stuff is? And so the future is the edge.
    We're going to have data on the edge. We'll have compute on the edge.

    It'll be very.Very very fast. No cold starts cold starts are you know here

    on the clock for our workers page their support for zero millisecond cold
    starts. We're it's it's basically free it's outrageous and it's an exciting
    future. I'm not saying that you need to rewrite your app so that it works
    in a cloudflower worker or anything but I am saying that this is kind of
    the endgame for remix.

    It's a definitely a target for what remix is trying to do and I'm super

    here for it. Yeah. I hope that's interesting. Have a good day.

    3 min
  • Universal remotes: So many use cases it's useless

    What is up friends so this is another one in the real world programming

    analogies segment I guess so actually a long time ago. I wrote this down as
    something I could do for a blog post but you know TV remotes and how like
    you either have like 30 different remotes to control like your TV and the
    receiver and the the Chromecast, you know, like all the different and
    remotes that you need for that it's a disaster or you can get a universal
    remote.

    And what's interesting about universal remotes is that they're to make them

    actually universal first of all there's a lot of work to make it work with
    all these different technologies, but then they're like 20,000 buttons on
    these things and nobody knows what they do because they have to be generic
    enough to be universal and I was and and so like either one of those is
    isn't great but it just made me think about abstractions and trying to make
    your stuff overly useful or satisfy at.

    Every use case and actually I was listening to syntax recently and I think

    it was last week's article our podcast where Scott mentioned that he was
    trying to use some I think it was like an authentication thing and I think
    it's like account JS or something like that anyway, it supports like pretty
    much anything that you could imagine from a backend perspective and things
    and he realized that he had to do just a ton of work to glue everything
    together and only ended up on using like 10% of.

    What this abstraction offered and realized that he could just write that

    10% himself which he did and ended up being in a much better place and I
    have definitely experienced similar things myself where things are just so
    generic that it is basically not doing anything for you and so like there
    you're you've got both both sides of the coin and coin so like over
    modularity is a problem where you know, it just does so much that it's hard
    to make things or like you have to do a,Lot of work just to make it work
    and then you end up not using all of the features that are supported and on
    top of that maybe the library is going to the client side and so it's
    really big and that's a problem but also bad abstractions are a problem too
    and and so like making something too focused on a specific use case may not
    make it useful for general solutions or general problems and so yeah, like
    it just was something I always thinking about over modularity can be a
    problem just be careful of that and I hope that.

    Is interesting take care.

    3 min
  • Create git-ignored playground files in your project

    So one thing that I do, you know, how you like you do something just you've

    done it forever and then when you show it to somebody they're like blown
    away like, oh man, what a good idea, whatever nobody is said that to me on
    this one but I thought I'd be one of those things that um that I just do it
    naturally in and you might find it to be useful so in my global git ignore
    for my machine.

    I have a pattern for ignoring anything that includes the text dot ignored

    dot and and so then I can create a folder.That's like temp dot ignored dot
    staff or more often I'll create a file that's like temp dot ignored dot js
    and what this allows me to do is I create a file like that within my
    project that I just want to play around with stuff and I don't want to have
    to install all the same dependencies in a different project or whatever and
    I don't want to mess up the other files that I have in my project and then
    I can mess around with things and I do this a lot for interacting with
    third-party APIs, I'm just trying to learn about like what's the code that
    I need to write to consume?

    This API and transform it into whatever I need it to be and then forward

    that on to the rest of my my code base and and then adding types to it and
    everything and so yeah just having the ability to quickly create little
    files that will be ignored by git is really quite useful and and files
    within the project so I can import other files and things and if I want to
    write it all in typescript then I can use TS node to run it but normally I
    just do it in JavaScript because often script.

    Just kind of in the way there especially since I configure type script to

    be unstrict mode and everything I'm just rapidly iterating over some code
    and then when I'm all done with that, I can copy it into where it should be
    in my project and add types to it and everything like that so anyway.

    I think that it can be useful to have a little kind of a playground sort of

    area for you to to experiment with different ideas and stuff if you want to
    take it to the next level there's quokka by Wallaby JS and I do have quokka
    and I use that sometimes but what I'm interacting with third-party APIs, I
    want.

    To be explicit about when I'm rerunning the code and so yeah having just a

    separate file for that can be quite useful. I've done this for years and
    maybe that'll be interesting or useful to you hope you have a great day bye.

    3 min
  • The Diversity of your team is a feature

    Hey friends, so I am at ski resort in Utah right now and they have this

    kind of a funny story that in the bathroom they have the urinals covered up
    like every other one covered up for social distancing and a few weeks ago
    when I brought my son up here he needed to use the bathroom and all of the
    and he's he's kind of small so normal he just uses the pot but all of the
    those were being used and he really needed to go and so I was going to.

    Have him use the urinal but they covered up the only small or short urinal

    so they because they covered up every other one they just happened to cover
    up the only short one and so I I ripped off the garbage bag that was tied
    on to it and so he could go but it just made me think like the the this is
    has something to do with like accessibility and the importance of diversity
    in your team because I imagine that the the person who is assigned to cover
    those up like they didn't tell them which ones to cover up they just
    covered it they chose one and they did every other one and it just happened
    to cover.

    Up the the shorter one but if they'd been apparent then maybe they were a

    parent of small children or something then maybe they would have thought oh
    let's make sure we keep this one uncovered so that smaller or shorter as
    smaller children are shorter people can use this one and that's the sort of
    thing that you just miss out on if you don't have a diverse team so I I
    think that is like it's kind of a silly example, but it is an example of a
    situation where having a broader set of experiences on your team can expose
    you to more needs that.

    Your users might have and so yeah those are some of my mind this morning. I

    just thinking about the importance of diversity. I hope that is interesting
    and useful to you and I hope you have a fantastic day and we'll catch you
    on the other side.

    3 min
  • Avoid the sunk costs fallacy

    Hey friends, so I was just working on something for the last hour or so and

    I got to a point where I decided, you know what this I probably shouldn't
    be doing this like it was an optimization basically. I wanted to be able to
    change content on my site, so I whether it be a blog post or the about page
    or something like that, but not actually trigger a redeploy and I'm using
    GitHub actions right now for this site and there are ways that you can
    determine okay, what was the last successful deploy?

    And what should be the you know what files are changed between that

    deploying this one and if they were only content files then just don't
    deploy them. And so, this is absolutely possible and I was working on it
    and I spent about an hour or so working on this and then I finally decided
    hey, you know what?

    I don't think that I need to worry about this right now. It's an

    optimization so what if I'd redeploy on a content only change? Right now,
    I'm just trying to create the site in the first place. I'm not I'm barely
    even deploying. So yeah, I'm trying to solve a problem that I don't have
    and it's very.

    Possible that when I'm all finished, I'm gonna switch from GitHub actions

    to to Netlify or something completely different and yeah, it just seems to
    me that this is way premature and I should be working on other things just
    to get this site up and going before worrying about you know, am I
    deploying unnecessarily?

    So yeah, that was just a thought that I wanted to share with you all just

    make sure that you when when you realize that what you're doing may or may
    not be used.Full and there are other things that you could be doing that
    would be more useful then stop doing what that thing.

    And I just made a branch and committed to changes that I made I pushed that

    branch up and so I'll have that code if I ever need it but I I don't think
    that I will honestly but in any case I've got it there and I can shift over
    to something that can provide more value and more of what I'm looking for.

    So anyway, I hope that's helpful to you and I also hope you have a

    splendiferous day.

    3 min