Categories
JavaScript Network Automation

Pseudocode

It didn’t take long for me to realize the benefits of writing pseudocode. When I first started, I would just fire up the command prompt and do what I call, freestyle coding; coding off the top of my head without a game plan. I made this mistake several times.

There were times where I was patient enough to write out some pseudocode, and nine times out of ten, I was able to complete the task faster and more efficiently. I’ve applied both the ‘freestyle’ and pseudocode methods so many times that it was difficult for me to not see how beneficial pseudo coding is.

So what is pseudocode?

Pseudocode is a technique that programmers use to simplify the details of the project. When writing pseudocode, you omit any variables and/or function declarations, no syntax related information; just plain’ol laymen’s terms sentences. You can write/type out pseudocode anywhere:

  • Pencil and paper
  • Paper and pen
  • Notepad ( on your computer )

TIP:

The above ways of executing are great ways to bang out some pseudocode. In addition to the above, it is a great idea to carry a small notebook, that way you can write down ideas while you’re away from your desk. Your brain can only regurgitate so much information at a time. Some of the best ideas come-to-mind while you’re away from your computer, they can come to you while taking a shower, playing videogames, eating, exercising, etc.

While writing out pseudocode, don’t pay too much attention to grammar and punctuation, it needs to be written so you can understand it, and better yet, explain it to others.

Here is some pseudocode I scribbled out while completing freecodecamp challenge:

Some scribble while I was riding the subway….Sometimes you gotta write down whatever you have in your head onto paper at the moment you have it. Otherwise, you risk losing valuable information.

I know it looks like crap, but it was something that I understood clearly, so clear that I executed the project within a day.

There is one huge caveat to all of this; you need to understand what the problem is before you can even begin writing pseudocode! You gotta do your homework. If you don’t understand what the problem is, then you’re essentially stuck. Every time I became stuck, 9 times outta 10 it was because I didn’t fully understand the problem.

If you become stuck, here are a few tips you can use to move forward:

  • Search google and youtube to understand the subject. For instance, if your program converts Celsius to Fahrenheit, then you need to research that what those two elements are and what formulas are involved.
  • Step away from the computer and write the problem out on a piece of paper, do it over and over again in different formats if you need to.

Attention, rant ahead!

Most people say go on forums and ask for help on how to solve a problem. However, I would use this as a last resort. Why? Because more often than not people on the internet tend to give away the answer instead of to help steer you in the right direction; this doesn’t help you. Now, you are probably saying:

“Jeff, what are you talking about? I have the answer, so of course, it helps; problem solved!”  

My response to that is:

“Yea, problem solved, you can now move on. But lemme ask you, did you solve the problem or did they solve the problem? And more importantly, do you understand the problem?”

In my humble opinion, the only time accepting an answer from someone is if you take the time to understand his or her solution, do not just copy and paste. I made this mistake one too many times. Yea folks, I know what it’s like. You’re on a time crunch and you need results, fast. So you grab a more experienced person and they gift you the answer. It happens in many of the situations where you’re stuck. Then one day you look up and you realize that you understand very little; you have become too reliant on others to move you forward.

So should you accept answers from someone else? Only if you’re willing to analyze their solution at a later date so you can solve that problem or a similar one the next time it comes around, you will be doing both your team and yourself a disservice if you don’t.

Rant over

Let’s finish up this blog with some examples of pseudocode, yea?

I will first provide you with the pseudocode and then the answer, below it. I encourage you to quickly script out the code yourself before you scroll to look at my answer (keyword: my answer; as yours might be different).

Example one:

  • “I need to accept input from the user. The input needs to be a number, not a string”
  • “Once accepted, I need to add 100 to their number before printing out a result.”
function addEmUp(number){
      const sum = number + 100;
      console.log(sum)
};

Example two:

  • “ I need to print out numbers 1 through 20”
  • “use a for loop to get this done.”
  • “on each iteration, the number will be incremented and printed out as long as it is not greater than 20 and less than 1. The  number should only be increment after each iteration, not before.”
for (let num=1; num !== 21; num++){
      console.log(num)
}

Example three:

  • “I need to convert the alphabet from lowercase to uppercase.”
  • “I gotta, first, generate the alphabet in lowercase. Should be a string.”
    • My solutions of choice:
      • For loop.
      • Recursion ( will use recursion to generate lowercase version);
  • “Within recursion, I will probably use the String method ‘fromCharcode’ to find out what UTF-16 code corresponds to a given letter in the alphabet.”
  • “I know that 97 is the UTF-16 code for lowercase ‘a’ and there are 26 letters in the alphabet, so UTF-16 code (97)   +  26 is 122.  The makes lower case ‘z’ UTF code  122.”
  • “On each iteration, I will increment the utf code add the corresponding letter to a string.”
  • “After generating the alphabet, I will use another function to convert it from lowercase to uppercase. I will likely use the uppercase String method to get this done. Print out the results in the console.”
convertToUpperCaseAlpha();

function alphaBet(char){
      if ( char > 122 ) return '';
      return `${String.fromCharCode(char)}` + `${alphaBet(++char)}`
}

function convertToUpperCaseAlpha(){
      const convert = alphaBet(97).toUpperCase();
      return convert;
}

If this article was helpful, let me know in the comments, below. If not, let me know in the comments below. I want to hear from both sides for the fence :0).