I have started learning program design and these articles are my attempt to document my learnings.
What is Program Design
Basic unit of a Program
Elements of Computation
Laws of Algebraic expressions
Program comes from programma, meaning a written public notice or open letter.
Design comes from designare, which means "to mark out," "appoint," "choose," or "devise".
Let's think of program design as a written notice to mark out or outlining a message. We are outlining the message we wanna convey to a computer.
It's an outline, not the message itself.
Hence, this is language-agnostic. You can write the message in any language using the same outline.
But for demo purposes, I have used Python Programming language.
Let's start.
The most basic unit of a program is a computation the computer will perform.
In Computation, given some information, you perform a transformation and obtain a result.
Let's look at the 2 elements of computation -
Expression
Evaluation
They describe the computation which can contain : values & operators, whose evaluation is governed by grouping/precedence rules.
eg.
3 + 5 * 2Step by step determination of the resulting value of an expression.
eg,
3 + 5 * 2
> 3 + (5 * 2) # multiply has more priority
> 3 + 10
> 13This is a substitution-style evaluation trace.
In the above example, we have 2 operators in a single expression. It's possible to have way more. So how do we decide which to give priority first so we can solve it 1 step at a time.
So, We use predence rules. Let's look at that !
It tells who is more important and therefore must come before!
Whenever we encounter expressions with multiple operators, this hierarchy determines what gets solved first.
1. () function calls
2. [] indexing
. attribute access
3. ** exponentiation
4. +x -x ~x unary operations
5. * / // % multiplication/division
6. + - addition/subtraction
7. << >> shifts
8. & bitwise AND
9. ^ bitwise XOR
10. | bitwise OR
11. in, not in, is, is not, <, <=, >, >=, ==, !=
12. not
13. and
14. or
This is not a complete list but enough to understand how evaluation would proceed.
NOTE: This is for python, other languages can have their own different rules.
(1) has the highest priority and its a descending order list.
Wherever there are multiple operators for same level, we usually solve from left to right, except some cases like ** (exponentiaton) eg. 2 ** 3 ** 2 == 2 ** (3 ** 2) == 2 ** 9 == 512 which is solved right to left. These can vary and can produce unwanted results, so referrring to docs is required.
.
The list above shows many types of operators. The table below is a cleaner categorization of the various types:
| Category | Operators | Example | Result
| ---------------- | -----------------
| Arithmetic | `+ - * / // % **`
| Comparison | `== != < > <= >=`
| Boolean | `and or not`
| Bitwise | `& \| ^ ~ << >>`
| Membership | `in`, `not in` | 1 in [1,2,3] | True
| Identity | `is`, `is not` | x is None | True/False
| Indexing | `[]` | array[5]
| Attribute access | `.` | user.name
| Function call | `()` | foo(args)
Also, An operator can have different nos. of operands:
; Unary (1)
-x
; Binary (2)
x + y (-) is both unary and binary operator.
.
An expression can contain multiple types of values. These are
Literals/constant - values directly written in code
42
3.14
"hello"
True
None
[1, 2, 3]Variables
x = 42
x + 5Function Calls
len("hello") # returns length of word helloIndex values
items = ["a", "b", "c"]
items[1]Atrribute values
user.name # key-value.
In most realistic scenarios, the expressions appear with a combination of all these types. The specially common one is :
variable-X = variable-A + Lierals + operators + funtion callA complex expression RHS whose evaluation result is assigned to another variable LHS - variable-X. ( = is assignment operator which assigns the resulting value to variable-X and replaces or shadows any previous value if exists and variable is mutable {more on variables on article #3})
.
That's it for this article.
In the next one, I plan to cover Git (without Github) for solo local project management. It was surprising for me to learn that Git isn't just for collab, open-souring or online code backups. Many devs I've talked to do not use git for personal projects unless they wanna upload it, but that was never the point. The main power of git is version control which can totally happen locally. That's what I'll cover. I'll write a separate article for collaboration, GitHub Integration, Remote Repo management and open sourcing - contribution, sharing and licensing.
You can even use another computer on a LAN network as a remote git repo.
More on them later.
0
0
0