DEV Community 👩‍💻👨‍💻

Cover image for Don't Overthink Things
Cory Dorfner
Cory Dorfner

Posted on • Updated on

Don't Overthink Things

One of the most common traits of a person is the ability to overthink things. Whether that be something as trivial as where to go for lunch or something more convoluted, such as what to do with your life. Regardless of the situation, it's important to remember not to overthink things. Now, don't get me wrong, spending time thinking about issues/concerns/problems in your life is very important, but be careful not to get sucked into a downward spiral of thought without action. This is almost never helpful and, in fact, can lead to some very devastating results for both yourself and others. Imagine if the inventors of our past never took action on their ideas and instead spent all of their time thinking about how to perfect them. It was only because of their actions that we have the many pleasures and simplicities of life that we enjoy every day.

I want to share with you an issue I recently experienced with an application I was building where I drastically overthought a solution to a problem. My task was to build a system where a user could enter details into an input field, click the submit button, and see their answer appear in a text area field. When I was designing the application, my initial idea was to store the text area data in a nested list of objects to make it easier to display on the user interface:

answers: [
  {
  id: 1,
  answer: 'Test 1',
  childElement: [{
    id: 2,
    answer: 'Test 2',
    childElement: [{
      id: 3,
      answer: 'Test 3',
      childElement: [{
        id: 4,
        answer: 'Test 4',
        childElement: [],
      }],
    }],
  }],
  }
]
Enter fullscreen mode Exit fullscreen mode

This worked great at the start of the project but, as the needs of different features were being implemented, I started to notice the complexity that this data structure was bringing to my application. While the nested list worked out great to display the data, it was not so perfect when it came to deleting or updating information afterward. I found myself stressing out for days trying to solve the issue of traversing through this nested list to perform simple operations on each item. I was pulling my hair out to make it work but every solution I tried implementing just seemed to cause an issue with a different part of the application. Any attempt to delete or update the contents of an li element nested within the list only caused issues with the id property of the object, making the numbering of the list become unordered or the li element not being indented properly. I was nearly ready to give up on the program when it suddenly dawned on me while taking a walk that I was greatly overthinking the problem.

Picard facepalm

By modifying the nested list to a flat array of objects, all of the other features I was trying to implement were suddenly much easier to work with. The only issue now was how to make it appear like a nested list on the user interface. Well, it turns out, the solution was incredibly simple. All that was needed was to include a div at the start of each ul with a simple formula to increase the margin based on the current item number.

By changing the data structure as follows:

answers: [
  {
  id: 1,
  answer: 'Test 1',
  },
  {
  id: 2,
  answer: 'Test 2',
  },
  {
  id: 3,
  answer: 'Test 3',
  },
  {
  id: 4,
  answer: 'Test 4',
  }
]
Enter fullscreen mode Exit fullscreen mode

and including the below formula in the rendering of my React component:

render() {
  let nodes = this.state.answers.map(function (element) {
    let divStyle = {
      marginLeft: `${(element.answer_id-1)*4}%`
    };
    return (
      <Row key={uuidv4()}>
        <Col style={divStyle}>
          <Answer 
            uuid={uuidv4()} 
            node={element} 
            handleDelete={() => this.props.handleDelete(element)} 
            handleEdit={() => this.props.handleEdit(element)} 
          />
        </Col>
      </Row>
    );
  }, this);

  return (
    <ul className={[styles.whyUl1]}>
      {nodes}
    </ul>
  );
}
Enter fullscreen mode Exit fullscreen mode

Where the Answer component simply renders a li element with the information passed to it displayed inside the tag. With this minor bit of code, I was able to solve my issue and create a user-friendly text area with a nested list:

Text Area Screenshot

That's it! That's all I had to do to solve my issue and move forward with the application. Now, I know this is a very simplistic example to use when talking about not overthinking things but the message is still the same. Whether you're overthinking about something small like a feature of your application or something larger like the next step of your life, it's important to ensure you take time to breathe and relax. The solution will come to you at the least likely time and you'll be able to jump on it quickly with a stress-free mindset.

If you enjoyed the post, be sure to follow me here and on social media too! The links to my social media accounts can be found on the contact page of my personal website. Thank you and I look forward to your comments below!👋

Top comments (0)

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.