So, the last part of my presentation is really a question. Again, I'm going back to the point where you have to ask questions all the time. So my top question usually is, who is the reader? And when we are writing fiction books, I guess this question is kind of debatable, I guess, for writers fiction, but for software developers, I think it's really clear that the code that we are writing is not just for us. It's mainly for other people to consume it, to read it, to maintain it, to extend it. So, it's important to write for others, for other people. And here's what Stephen King said, write with the door closed, rewrite with the door open. I'm always remembering this quote when I'm going through my pull requests before announcing it to my colleagues, because even Hemingway said that the very first draft is always shit, I'm quoting, but then after that, you have to go back, you have to ask, is this part of the story all the time? So, you have to write it in a way that it's simple for other people to understand, it's simple for other people even to make the code review. If someone spends like an hour reviewing your 50 lines of changes, it means that it's not really good. So, if you take this example, for now, don't focus on this area, the action prop of the form component. Let's say that this is a component about, it's called get email, we have an input field called we type email. When we change something, we store the value into a local state called value. And we have a button submit, which probably, if we press it, we go into the action of the form component and we register the user by using the email. So now, this I consider a draft. And I could make a couple of improvements. The way of how I refactor my components is first to look at the components from the outside, like the API, which means the name of the component and the props. And in this case, I know that this component is registering the user. It's not about getting the user from getting the email from the input field only. It's about really registering the users, so we better just name it register. We have a prop called callback. For sure callback is a function, but it doesn't really talk about why this function is fired later. So onSuccess, I guess, is better. Error doesn't even work like a function. So we have no idea from the outside that we have to provide a function, so we better call it onError. By doing these small changes, even though it's just renaming, we help other people understand better our component, even without going inside the implementation. This is my approach. Initially, I start from the outside, I look at the API, and I usually just rename stuff. Sometimes I've found props which shouldn't be there. They are not part of the story, for example. When you move forward, inside, the value is actually an email, so why not just call it an email? It would be easier when you're reading the file from top to bottom, and when you hit the email, you just actually see that this is an email. And now, this piece of logic, which is about making the requests using this API.users.register, if this fails, then we check the type of the error.
Comments