Junior v. Senior React Code: Using Flags with React Components
Tags: webdev, react, beginners
Published: 2020-09-02 03:03:03

This post is one in a series in which we take React code that a less-experienced developer would write and improve it so that it resembles more what a professional would write. In the first part of the series, [we converted a class component to a function component](https://dev.to/mkinoshita12/junior-v-senior-react-code-class-function-components-1f4j).

**Today we're covering best practices for naming and using flags that you pass into components.** Please check out the first part of the series to see the junior code and the refactored code.

## Passing a boolean into a function
Whenever you pass a boolean as an argument to a function, it is likely that the code can be better. The big reason is that it is hard to understand what effect that argument has on the function. This type of code is called a **code smell**. It is not a bug, but it indicates that there is a deeper problem in the program. 

If you want to learn more about code smells, we would highly recommend reading the book Clean Code. You might disagree with some of the things he argues, but the book is worth reading once in your life. In the book, he lists lots of examples of code smells, and this type is called selector argument. If you encounter a situation where you want to use a boolean as a flag, you probably should split the function into two different functions.

Before I show you how to split this code into two functions, I will explain how you can make the code better if you want to keep using the boolean as a flag.

#### 1. Create a variable
When passing a boolean into a function, one problem is that you don't know what that boolean means. The cheapest fix is to create a variable.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pj8dnnaxvb6jkx9tumgw.png)

#### 2. Use argument destructuring 
es6 lets you unpack the arguments passed into a function, and we can leverage that here. This approach is a slight improvement because it takes one fewer line of code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h51kh5bnpybwxljtvjxv.png)

## Creating separate functions
The above two solutions are okay when the function is super simple. However, a lot of people would argue that we should always have two different functions. They would reason that the `getData` function does two different things depending on whether it is the initial load or not, so it is better to split this function into two.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/endhj26zum4trnuthjqh.png)

In the refactored, senior version of the code we split `getData` into `loadInitialRandomCategoryImages` and `loadMoreSameCategoryImages`. Now it is totally clear what each function is doing!

---

If you liked this post, we recommend you subscribe to the [Antcode blog](https://blog.antcode.dev) and follow us on Twitter at @o_nagen and @mkinoshita12.

If you want to get reviews on your code and learn from reviews on other people's code, we recommend that you check out what we're building at [Antcode](https://antcode.dev) and join our [code review Slack group](https://join.slack.com/t/antcode-workspace/shared_invite/zt-gul4g6de-iPIrB9PiLLab6Rx5wZKZlA).

Happy coding!