Learning To love Code

Or at least how I do it.

Intellectual growth should start at birth and only cease at death

Albert Einstien

There are a million articles on learning a new skill and even more of those 5 things I wish I knew before XYZ articles. I'll do my best to avoid letting this become one of those.

When you first start off learning to program you are climbing two mountains at once. You are learning your chosen languages syntax as well as basic programming concepts. Most tutorials focus on one or the other. For instance, you may learn how to write a for loop.

This is one in JavaScript

// For Loop for ( let iterator = 0 ; iterator < 5 ; i++ ) { // This is some action thats repeated 5 time }

But why Do programmers use For Loops.

It seems trivial to someone experienced with programming. From another perspective however it may seem trivial to a designer with 1000's of hours of practice that one font pairs well with another. Can you really say a beginner would be able to make that same distinction?

While we are on the subject lets talk about loops in programming. Lets say you are planning a surprise birthday party in the 90's back before all this fancy Facebook stuff and everyone had a E-mail. You want to mail invites to all the guest so you have some very simple steps to follow.

  1. Write name.
  2. Write Date.
  3. Write Location.
  4. Draw Funny Stickman figure.
  5. Put Letter in envolope.
  6. Lick envelope.
  7. Attach Stamp.
  8. Put in pile with the other

Now imagine you are inviting 30 people. If you had to type this list thirty times, well I can imagine worse things, but you would wish for a easier way. luckily programmers also had that same desire lets look at what something like this would look like as code

// 3 instead of 30; let names = ["peter","bruce","clark"]; let bigStackOfInvites = []; for(let i = 0 ; i < names.length ; i++){ let invite = {}; invite.name = names[i]; invite.number = i; invite.location = "The Bat Cave"; bigStackOfInvites.push(invite); } console.log(bigStackOfInvites); /* [ { name: 'peter', number: 0, location: 'The Bat Cave' },  { name: 'bruce', number: 1, location: 'The Bat Cave' },  { name: 'clark', number: 2, location: 'The Bat Cave' } ]  */

Now lets look at whats going on here. On line 2, we are creating a array of names we are going to send invites to. Next in our story we are clearing off a spot on our desk for our BIG stack of invites. Then we come to the for loop. We are starting by declaring our loop will start at 0. Then we are saying that we want the loop to run through each name once. Then we are simply adding the instructions we want to repeat inside the loop.

Seems pretty simple right? Now try to imagine something in real-life you do that is similiar. Some repetitive task like cutting carrots or picking up oranges one at a time from the store and imagine how that would look as a loop.

Now try and write a simplfied version of that process in your language of choice. Once you have done this a few times, You will hopefully internalize the pattern of when to use loops in your own codebase!

Hopefully this was helpful and I'll be trying to put out more of these in the future. Thanks for reading !

Website Developed By Michael Walker.
Michael Walker 2021 © This site proudly uses Gatsby, Contentful, FormSpree