Skip to content

Instantly share code, notes, and snippets.

@FaisalAl-Tameemi
Created June 21, 2016 21:02
Show Gist options
  • Save FaisalAl-Tameemi/97e17bcbee60b618acf420646fd3ba81 to your computer and use it in GitHub Desktop.
Save FaisalAl-Tameemi/97e17bcbee60b618acf420646fd3ba81 to your computer and use it in GitHub Desktop.
week_1_day_2_breakout

Breakout Notes

Important Terms

  • Method signature: The name of the method and its parameter list

  • Method contract: Given some inputs, what are the side-effects of a method and what does it return

# @return: the sum of two numbers
sumTwo(param_1, param_2) # method signature
    params_1 + param_2
end
  • Refactoring: Changing code without changing what it does

  • Extract variable: store the result of a computation in a variable instead of recomputing it multiple times

  • Extract method: take a portion of code that you want to evaluate later or multiple times and put it in a method

  • Inline variable: take a variable that is only being used once and replace its use with the computation stored in it

What's an Algorithim?

An algorithm is a sequence of instructions or a set of rules that are followed to complete a task. This task can be anything, so long as you can give clear instructions for it.

For example, to make yourself a smoothie you have to follow a sequence of steps in the right order. If you do something in the wrong order you might end up making a mess.

Let's break down the steps:

  1. Get Fruits

  2. Add fruit to the blender.

  3. Add yogurt to the blender.

  4. Put the lid on the blender.

  5. Switch blender on.

  6. etc...

What other algorithim's has your brain programmed for your daily life activities?

What would the algorithim be for a parking lot system that has a max capacity of 10 cars. If the parking lot is full, simple keep the gate closed?

Flow Chart Solution

Full Article

Sum of Squares

Create a method that calculates the sum of the squares of a series of numbers.

def sum(numbers_list)
    
end

def square(number)
    number * number
end

def sumSquares(numbers)
    squares = 
end

sumSquares([1,2,3,4,5])

Petals Around The Rose // Sums

Let's play a guessing game.

Let's breakdown the logic of the game:

  • Experimented with the game to see how it works
  • Figured out how it's counting the petals
  • Broke down each step that the game is showing in the terminal
  • Built a flow chart on the whiteboard

Petals Game Code -- see if you can refactor this like we did in class with the sum game.

We also built a Sums game where the terminal prompts the user for the sum of two random numbers between 1 and 10.

Sums Game Code

Writing your code in an iterative way!

This means only write a few lines of code (maybe even one new line), have an expectation of what it will do, and then run to make sure it's behaving as you expect before moving onto the next step in your solution. It also means that you shouldn't convert your entire pseudocode into real code without running it. In fact, you should take small constructive digressions AWAY from your solution (eg: use puts temporarily, use placeholder values for user input, etc) to help you craft your program in this way

  1. Logical breakdown of the problem.
  2. Write the quickest brute-force code to solve the problem.
  3. Go back and refactor
  4. Test for edge cases.

The Stack Trace

The stack trace aka backtrace is the how ALL programs keep a TRACE of the different method calls that the program makes while its running.

If the program were to encounter an error, it prints (DUMPS) a string representation of the trace to you (the developer) so you can use it as a breadcrumb trail and identify where and how the program occurred.

Debuggers

Sometimes using puts or just reading the backtrace to determine the cause of the error may not be enough to quickly hone in on the problem. In that case, you can use a gem called byebug or another one called pry (or both) to step through code.

byebug is JUST a debugger but Pry has other features (and a weaker debugger) so you can use them together even.

You can install it using gem install byebug as per their github page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment