-
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
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:
-
Get Fruits
-
Add fruit to the blender.
-
Add yogurt to the blender.
-
Put the lid on the blender.
-
Switch blender on.
-
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?
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])
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.
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
- Logical breakdown of the problem.
- Write the quickest brute-force code to solve the problem.
- Go back and refactor
- Test for edge cases.
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.
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