How To Code

Things to remember:

  1. This isn't magic; it's not beyond you.
  2. Running away from 'the scary' NEVER helps.
  3. You actually do stuff like this in everyday life without realising.
    • Everytime you organise something
    • Everytime you work out the best way to go on a journey
  4. EVERYONE (including me!) feels like an imposter
  5. You don't have to know everything
  6. The code you first write may be REALLY BAD. That's absolutely fine. (re-factor it once you've got it working)
  7. No one feels like a programmer until they've been doing it for a while and realise that fact.
  8. Keep calm and believe. Just walk through it step by step

Types of problem

  • In coding there are basically 3 types of problem you'll encounter
    1. Mechanical - very basic problems, like 'reverse a string'
      • You can google answers to this
      • Try to think through on your own first though
      • You will learn these as you age as a dev
    2. Algorithm - a series of mechanical problems, e.g. "Is string1 an anagram of string 2?"​​
      • as with single mechanical problems but we'll look at coping strategies
    3. Modelling - where we're modelling real life

Mechanical

Take 'reversing a string'

  1. What are you trying to do to the string?
    • Change the order of the letters
    • Ah HA! order === array (but first  let's...)
  2. Check if there is already a method
    • there is not because strings are immutable
  3. OK, so turn the string into an array
    • str.split('');
  4. Now I need to reverse it, so
    • arr.reverse();
  5. Now I need to turn it back into a string
    1. arr.join('');

Seems so easy when I describe it!
Try to think it through and if you're lost then google and remember for next time!

Algorithm Problems

  • The key with problem solving [in the beginning] is to sketch it out in your head/on paper IN PSEUDOCODE - then convert it to code.
     
  • e.g. (Typical codewars question): Write a function that determines if a word is a palindrome (is the same read forwards as backwards, e.g. civic, level, etc.)
     
  • Steps:
    1. I need to compare the string to a reverse of itself
    2. so I need to make a string which is the reverse
    3. Strings don’t have a reverse method (because immutable)
    4. What does? An array!
    5. So, turn the string into an array (ACTION)
    6. then reverse it (ACTION)
    7. So then I have a string and an array
    8. I need to turn the array back into a string (ACTION)
    9. Finally I can compare the original to the reverse (ACTION)

Now in code add the actions as comments:

function checkPalindrome(possiblePalindrome){
  // turn the string into an array

  // then reverse it

  // turn the array back into a string

  // compare original to reverse
}

 Now put the relevant code to achieve that underneath

function checkPalindrome(possiblePalindrome){
  // turn the string into an array
  let reverseString = possiblePalindrome.split("");

  // then reverse it
  reverseString.reverse();

  // turn the array back into a string
  reverseString = reverseString.join("");

  // compare original to reverse
  return possiblePalindrome === reverseString;
}

Modelling

How do I program, James?!

Look at the problem and split it up into:

  • NOUNS
    • including 'types' of nouns (vehicles, chairs, people, etc.)
  • VERBS (or 'actions taken')
  • CHOICES
    • and what actions happen as a result of those choices

For each noun

  1. If it is, and can only be, a single piece of information, like the passcode/constant, then save it as a 'variable': Make sure that it is declared where you need it (scope)
  2. Const everything then
  3. If that single value is being monitored and likely to change in your program (aka it represents part of the current 'state' of your app) then save it with a let
  4. Having loose variables makes it difficult to track: Wherever possible keep them in an object, like 'settings', etc.
  5. FOR THE MOST PART:
    1. 'thing's are multi-faceted and therefore NOUNS ARE OBJECTS
    2. As soon as you need to group information about a single thing, it's objects
    3. Even if it seems like they're not, it's generally only a matter of time before they become objects
  1. This is an ability to do 'something' - some sort of procedure...
  2. We may know all the information required at the time of writing; we may not
  3. VERBS ARE FUNCTIONS
  4. Write what you want it to do in comments, then write code step-by-step to accomplish
  5. ONE FUNCTION SHOULD DO ONE THING/TASK  ('S' in SOLID)
  6. IF A NOUN performs an ACTION/VERB then the verb is a METHOD. It goes on the object as an instance method
  7. Loose functions are also difficult to track too, so if it is to do with a particular type of thing (class) then you put it as a static method of that class (like Array.isArray)
  8. IF, in the process you do something repeatedly, that is a loop
    1.  while - uncertain n of iteration ; for..of for EVERYTHING ELSE (for number ranges look here)

For each verb

  1. Choices are made during the course of an action
  2. They are therefore achieved inside a function
    1. Even if that is the main function that JavaScript runs for you!
  3. For each choice you need to determine what:
    • is the condition on which you would decide one way or another
      • You then need to make an expression (that resolves to true/false) 
    • actions you'd take once decided
      • function executions or blocks of code
  4. CHOICES are made by a combination of control flow (if/else, switch, etc.) and expressions (age > 18) (inc inspection of object properties (person.hasPassport))

For each choice

Grouping nouns/verbs together to make 'models'

  • When we want to represent things in life we make 'models'
  • When we want to consistently make models we use a template for 'types of things' that we call a class
  • When the objects are made with classes, this is what we call Object-oriented Programming (OOP) - 'We represent things with objects that encapsulate data (properties) and behaviour (methods)'

Classes

  1. Classes represent the blueprint for objs which are a type of thing
  2. Classes are there to promote
    • Data Consistency
      • See checking data for correct type/value in constructor
    • Code re-use
      • e.g. we create a class for our program so that it can be used multiple times
      • We share functionality by extending/inheriting

Object Properties

  1. When your objects have attributes - details that describe them, like 'height' on a person, set them as a property on the object
     
  2. Objs from a class can have properties set either:
    • via the constructor - when we don't know them when writing the class or they will differ between instances
    • public instance fields - when we do know them and they are the same for all instances

Methods

  1. If the thing you're modelling can do something, like 'walk', 'eat', 'run', etc. then set a method on the object.
  2. These will be your Instance methods, used by the objects that belong to that class
    • (you'll use this to in the method to refer to the object)
  3. IF the action your taking
    1. is to do with objects BUT doesn't require any knowledge of a particular instance to run
    2. or its not something the models frequently do
    3. then that is a:
      • Static Method​ and is stored on the class 
      • an example could be a method that generates Insurance policy numbers, where no details of a person are involved.
      • or uncommon functionality, like Person.calculateBMI(height, weight, age)

Groups of things

  1. Once you've created your noun representations, IF the nouns are in a plural, like cars, people, then they go in an array
     
  2. You can make a class or just use normal JS to manage that array
    • Give it methods to CRUD the items in the array
    • and any methods to describe actions that can happen to the set (e.g. 'feeding the whole group' - you'd add a method 'feed' to the class)

As you get better try to use these principles

How To Code

By James Sherry

How To Code

  • 1,107