Keeping it Simple
Tags: javascript, beginners
Published: 2022-10-11 04:34:06

If you had asked me a month ago to describe coding in three words they would have been something along the lines of direct, independent, and tiresome. I can now confirm only three weeks in that one of those words is accurate and I will let you guess as to which.

---

 Learning to code has been one of the most difficult experiences in my life. This is mainly because in order to learn how to code, you need to think like coder first. Understanding this method of thinking is a strenuous task, especially when you only have a matter of weeks to figure it out. I found myself (and most of my cohort) feeling behind schedule and scared of failing within the first few days of class. Luckily, I had people around me reminding me that this is a common feeling when first learning to code and that I just simply needed to push through and keep trying. So that's what I did and eventually what I was learning and exposing myself to everyday was starting to make sense! Where I really started to understand more, or my "aha moment", was when I realized that coding is abstract.

---

 You are not always going to understand why exactly one line of code works one way and another works a different way. Take this block of code for example:

```
const fetchReq1 = fetch ("http://localhost:3000/data")
.then(res => res.json())
.then(strainArray => {
    strainArray.forEach(strain => {
        buildMenu(strain)
    })
    strainDetails(strainArray[0]);
    setupCart();
})
```
It took me longer then I want to admit to fully grasp that JavaScript automatically knows that **strainArray** is the entire object of data being fetched because of the **.then**. With the **.forEach()**, JavaScript knows that **strain** is the parameter that represents every object within the array being iterated. And on top of that, if you included an undefined function within the **.forEach()** and  pass the **strain** parameter as its argument, it will remember that parameter and its data when you write out the function later in the code. Such a simple concept in hindsight  but because I was so dedicated to know how exactly JavaScript knew to do that, I remained confused for what felt like endless hours. Eventually I just accepted it as part of the language and something I would need to remember. 

---

This is when I started to change my method of thinking when viewing code. It is more important to try to find the simplest solution than just a solution. In order to do this, you have to take a step back and view your project as a whole from time to time. It is incredibly easy to get lost on just a few lines of code that eventually can turn into something ugly. Look at this function:

```
const reset = function() {
    
        document.getElementById('strain-in-cart').innerHTML = 0;
        currentStrain.number_in_bag = 0;
}
```
At one point this function was 15+ lines long because I couldn't figure out why when I tried to clear the value of my "in-cart" number, my code only ended up visually showing a 0 but the original value still existed under the hood. As it turns out all I needed to do was target two separate areas of my code and set it to 0 (my db.json and the html). My mistake here was that I focused too much on adding layers to my code to fix the issue instead of taking a step back and seeing what could be accomplished in as few lines as possible.

---
Thinking about my next three weeks ahead, I have a goal I want to reach by my next blog post. My goal is to get to a point in my code where not only I am able to understand what it says, but am able to explain my code clearly and efficiently to collaborating teams members and instructors. If I start practicing clear communication with my code now, I believe it will help me immensely in my first job as well as prevent any bad coding habits.