It consists of 2 parts:
Learning a programming language and its syntax and semantics gives you the skills to read and understand code as well as to write your own programs.
It is an analytical activity.
Most programming language has these building blocks. Only a
different syntax.
We will use JavaScript to learn them.
JavaScript is a high-level programming language. Nowadays you can (nearly) do anything with JavaScript.
JavaScript is not Java.
Java is to JavaScript as
cat is to catfish.
Is JavaScript a good choice as first programming language?
JavaScript has also pitfalls and problems - as most programming languages do.
Solving a problem in programming can be defined as writing an original program that performs a particular set of tasks and meets all stated constraints.
This set of tasks is called an algorithm.
It is a creative activity.
That's what makes it difficult.
Think about creating a website:
Constraints are the limitiations in how your program can perform the specific set of tasks.
The biggest mistake I see new programmers make is focusing on learning syntax instead of learning how to solve problems.
V. Anton Spraul
Read the problem several times until you can explain it to someone else in plain English.
If you can’t explain something in simple terms, you don’t understand it.Richard Feynman
Rubber duck debugging: Insights are often found by simply describing the problem aloud.
Take time to analyze the problem and process the information. Think very precisely about how you solve the problem.
Ask yourself: "Given input X, what are the steps necessary to return output Y?"
Do not try to solve one big problem. Break it down into steps. Steps that are so simple that a computer can execute them.
Once you solved every step (sub-problem), connect the dots and you
will find the
solution to the original problem.
Congratulations!
Step 3 is so important that you can use it to define programming.
Don't look for a solution to the big problem, only look for solutions to sub-problems.
You will learn and understand more. You will need less research and Google next time.
Google is not about Copy & Paste, it is about learning.
Practice makes perfect.
The more problems you solve, the more research you do on other programmers solving the same problem, the more you think like a programmer.
Demonstrating computational thinking or the ability to break down large, complex problems is just as valuable (if not more so) than the baseline technical skills required for a job.
Hacker Rank (2018 Developer Skills Report)
A farmer with a fox, a goose and a sack of corn needs to cross a river. The farmer has a rowboat, but there is room for only the farmer and one of his three items. Unfortunately, both the fox and the goose are hungry. The fox cannot be left alone with the goose, or the fox will eat the goose. The goose cannot be left alone with the corn, or the goose will eat the corn.
Find a partner and talk through the puzzle.
You have 15 minutes
to try solve the puzzle.
Describe the steps needed.
The more specific your operations are, the easier it is to miss possible operations.
This is also called abstraction.
Now we generate all possible sequences of moves, ending each sequence once it violates one of our contraints or reaches a configuration we've seen before.
Eventually we hit upon the only possible sequence, the solution of our problem.
We had that before.
You can use mind mapping tools or whiteboards to visualize your problem, e.g. a notepad or Miro.
Nothing can be automated that cannot be done manually!
Test your manual process with at least 3 different inputs. Think about what you did to find the solution.
Simplify and optimize your steps. Look for patterns and see if there's anything you can generalize.
Can you reduce any steps? Are you repeating steps?
Try for brevity. The lines that you don’t write are the lines where you can be sure that they don’t have bugs.
Writing pseudocode line by line, before starting to code, helps you defining the structure of your code.
You can do that on a sheet of paper or as comments in your code editor.
It is often easier to get started with flow charts.
Flow charts help you visualize the different steps within a process or program.
There are different shapes for Process, Terminator, Decision, Data and On-Page Reference
Pseudocode is an informal way of writing a program. However, it is not a computer program.
It only represents the algorithm of the program in natural language and mathematical notations.
A JavaScript developer and a Cow developer would use pseudocode to communicate with each other.
MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO Moo MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO Moo MoO MoO MoO MoO MoO MoO MoO Moo Moo MoO MoO MoO Moo OOO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO Moo MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO Moo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOo MOoMOo MOo MOo MOo MOo Moo MOo MOo MOo MOo MOo MOo MOo MOo Moo MoO MoO MoO Moo MOo MOo MOo MOo MOo MOo Moo MOo MOo MOo MOo MOo MOo MOo MOo MooOOO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO MoO Moo
After your pseudocode is ready, translate each line into real code.
Test each step you translate as early and as thoroughly as possible. Finding a problem in a small and easy piece of code is much simpler than trying to spot it in a large program.
This is also called code refactoring and we will hear more about that in the coming weeks.
Programs must be written for people to read, and only incidentally for machines to execute.
Gerald Jay Sussman and Hal Abelson
Again, practice makes perfect.
The best way to learn coding is by coding as much as possible.
One family wants to get through a tunnel. Dad can make it in 1 minute, mama in 2 minutes, son in 4 and daughter in 5 minutes. Unfortunately, not more than two persons can go through the narrow tunnel at one time, moving at the speed of the slower one.
Before you go to the next slide, try to solve the problem on your own first. Give yourself about 30 to 45 minutes.
GetThroughTheTunnel
Initialize maxTime as 12;
Initialize time1 as 1, time2 as 2, time3 as 4, time4 as 5;
Initialize maxPeople as 2;
Initialize totalTime as 0;
Initialize peopleAtStart as 4;
Initialize ListOfPeople;
Compare times with each other to sort them by speed
=> output is ordered list
Select the 2FastestPeople (times)
Select the 2SlowestPeople (times)
Step 1: 2 fastest going
IF 2FastestPeople <= maxPeople
totalTime = totalTime + (slowest of the 2 fastest);
peopleAtStart = peopleAtStart-2
END IF
Step 2: The fastest is going back
totalTime = totalTime + (fastest of the 2 fastest);
peopleAtStart = peopleAtStart+1
Step3: 2 slowest going
totalTime = totalTime + (slowest of the 2 slowest);
peopleAtStart = peopleAtStart-2
Step 4: The fastest is going back
totalTime = totalTime + (slowest of the 2 fastest);
peopleAtStart = peopleAtStart+1
Step 5: the remaining pair going
totalTime = totalTime + (slowest of the 2 fastest);
peopleAtStart = peopleAtStart-2
IF totalTime <= maxTime AND peopleAtStart == 0
Print "Success. You rule!"
ELSE
Print "Failure. Try again."
END IF
END GetThroughTheTunnel
Let's have a closer look to exercise 1 from before, finalize the pseudo-code and add how you would sort the times by speed.
Allow the user to input digits, afterwards sort and print them in numerical order.
Understanding the problem is key to solving it.
Sometimes the first brief might not be enough.
Ask questions.
Can you think of more?
Try to find them yourself first. Take 10 minutes to find contraints and note them down.
1,2,7,3 => true
1, 2, 7, 3 => true
01226354 => false
1237 => true
1,2,3,7 => false
2371 => false
[1,2,3,7] => false
... always define expected input and output and visualize the steps to get from input to output.
Have a look at your pseudocode again. Can you now optimize it with inputs and outputs in mind?
SortNumbers
INITIALIZE userInput AS String;
INITIALIZE digitsList AS List;
INITIALIZE output AS String;
INITIALIZE counter;
PROMPT "Please input minimum 2 digits and
maximum 10 digits (0-9), separated by commas" AS userInput
SPLIT userInput AT THE SEPERATOR "," TO digitsList
// check for spaces, are there commas in the string
FOR EACH item OF digitsList
IF not a digit
PROMPT ("Please make sure that you only add single digits (0-9)");
END IF
END FOR EACH
END SortNumbers
Build a tip calculator.
Once your questions are answered, summarize the problem in a statement:
Create a simple tip calculator. The program should prompt for a bill amount and a tip rate. The program must compute the tip and then display both the tip and the total amount of the bill.
For your understanding it is always helpful to have some example inputs and outputs in mind, before you start solving your problem.
This is the time where programmers often jump ahead and start coding.
Make sure your program is thought through properly, before you start.
Even the simplest program has always inputs, processes and outputs. It is the core definition of a program.
Go back to the problem statement and check for nouns and verbs. Nouns are usually your inputs and outputs, while verbs describe your processes.
Not every verb has to be a process. prompt and display define the GUI, how the user interacts with the program.
Test-driven development (TDD) is a very common practice in software developement and more and more web application development.
The essence of TDD is to think about what the expected result of the program is ahead of time and then work towards getting there.
Doing that even before you start coding, often enables you to think beyond the initial requirements.
A test plan lists the program's inputs and its expected result.
You start with writing down the program's inputs and expected output. After running your program you compare the expected result with the actual result.
Work with more than one test.
Try to think about corner and edge cases.
A problem or situation that occurs outside of normal operating parameters.
Think of it as a hard to reach design state that happens only when multiple conditions happen in some combination.
Your program logic meets more than one boundary condition at once.
A problem or situation that occurs only at an extreme (maximum or minimum) operating parameter, e.g. input.
Your program logic meets one boundary condition.
Is it very useful to add edge cases to your tests, as they are usually the place where bugs appear.
Come up with several test cases on your own (15 min), then meet a peer and discuss them together (15 min).
Try to think about corner and edge cases.
For example: what happens if the bill amount is already a decimal? What happens if the tip rate is a decimal? What could be an edge case?
Edge and corner cases can show you the constraints of your program. Also the program statement and test cases can visualize them.
An algorithm is a step-by-step set of operations that need to be performed.
If you take an algorithm and write code to perform those operations, you end up with a computer program.
Although there is no right or wrong way to write pseudocode, there are widely used terms.
Before you go to the next slide, try to write the pseudocode on your own first. Start with very general descriptions of your steps. Give yourself about 30 to 45 minutes.
TipCalculator
Initialize billAmount to 0
Initialize tip to 0
Initialize tipRate to 0
Initialize total to 0
Prompt for billAmount with "What is the bill amount?"
Prompt for tipRate with "What is the tip rate?"
convert billAmount to a number
convert tipRate to a number
tip = billAmount * (tipRate / 100)
round tip up to nearest 5 rappen
total = billAmount + tip
Display "Tip : CHF" + tip
Display "Total : CHF" + total
End
The next step would be to improve the code, e.g.
We will discuss data types, variables and functions in detail next week.
The final step would be to write the code, comment it properly, test and debug it right away and finally revise and optimize your code for quality and performance.
Never comment on what the code is doing, only write comments that explain why.
No need to understand the code of the example yet, just have a look at the comments.
// This function checks whether a number is even
let f(x) = function {
// compute x modulo 2 and check whether it is zero
if (x%2 == 0){
// the number is even
return true;
} else {
// the number is odd
return false;
}
}
Notice that the name of the functions and the variables are self-explanatory. Comments are not needed.
let is_divisible = function(number, divisor){
return number%divisor == 0;
}
let is_even = function(number){
return is_divisible(number, 2);
}
Better naming and a better task breakdown make the comments from the bad example (what comments) obsolete.
Revise your code just as you would revise an essay: Sketch, write, delete, reformulate, ask others what they think. Repeat until only the crispest possible expression of your idea remains.
An algorithm needs to be presented at their level of understanding and in a language they understand.
"They" being humans as well as machines.
I am thinking of an integer between 1 and 100. Your task is to find this number by asking me questions of the form "Is your number higher, lower or equal to x" for different numbers x.
Let's do it together.
In the first 2 cases, if you are unlucky, you have to ask 100 times, before you hit the right number.
This is an recursive algorithm
Validate a telephone number, as if written on an input form. Telephone numbers can be written as ten digits, or with dashes, spaces, or dots between the three segments, or with the area code parenthesized; both the area code and any white space between segments are optional.
Many problems will require a combination of algorithmic and heuristic solutions.
Heuristic solutions emerge from trial and error based on knowledge and experience.
Real-life examples: Winning a chess game, making a speech at a
ceremony or convention.
In programming typical problems are
getting computers to speak a language or recognize patterns
(Machine learning and artifical intelligence).
When writing algorithms the complexety or performance is a key factor to consider: cost efficient, space efficient and fast. In computer science the Big O notation is used to describe the performance, or better the worst-case scenario.
Find the greatest common divisor of 2 positive integers.
In math, the GCD of 2 or more positive integers is the largest positive integer that divides each of the integers.
You have 45 min to do answer the points in the next slide.
Moving data and playing with it is the foundation of programming, e.g. send login credentials to a web sever.
You program in teams, not on your own. Be as clear as possible and as simple as possible. Clean code is more important than clever code.
Good programming is about keeping the underlying logic expressed simply, so that it can be readable to others and your future self.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
The Zen of Python
Keep it simple, stupid.
Start small and simplify your program as much as possible, before you even start. The more complex the code, the more bugs and errors will turn up, maintaining the code as well as modifying it will be more difficult.
Don't repeat yourself.
Avoid duplicated and repeated code and data. Usually you can do that by abstracting and pulling code into functions, components and modules you only write once and re-use.
You aren't gonna need it.
Never code a functionality that you may need in the future. You might not need it after all and it makes your code more complex.
There are many more principles, but the bottom line is: especially as an inexperienced programmer code rarely comes out right the first time.
Revisit, rewrite or even redesign parts of your code base based on programming principles.
Declares what is being done rather than how it should be done.
Examples: SQL, HTML, CSS
Focuses on how a task is done rather than what is being done.
Examples: Java, JavaScript, Ruby
Uses procedures, also known as routines, subroutines or functions, to operate on data structures and carry out tasks.
Examples: Java, JavaScript, C, Pascal, Basic
Involves building objects with data attributes and programming subroutines. Code reusability and inheritance are main concepts of today's dominant paradigm.
Examples: Java, JavaScript, Python, PHP, C, C++, Ruby
Programming languages, like JavaScript, Java, and many more support more than one programming paradigm, in order to allow the most suitable programming style for a task.