Software Construction Flashcards
statically vs dynamically typed
- ** statically:**
- compiler is strict about types
- dynamically:
- ## assigning a variable a diff type will change the vars type
java, typescript, javascript
java: !interpreted & static
typescript: !interpreted & optionally static
javascript: !interpreted & dynamic
interpreted vs compiled
- interpreted:
- top down
- compiled:
- compiled languages are first analyzed and transformed by a compiler before they can be executed
linters common warnings
- unused method
- var is hoisted to scope of entire method rather than in the block:
- use let to restrict to block
- use const (var wont be overwritten at all in method)
Asynchronous execution in JavaScript
- JavaScript uses an asynchronous execution model
- Functions run on the call stack and always execute to completion—one function runs at a time
- If a function takes too long, it blocks the main thread
- extra info: JavaScript functions should ideally finish in under 16ms, if they dont they cause “jank”
Thread pool & Blocking Tasks
- Long operations (e.g., network requests, file I/O) often take much longer than 16ms
- These are offloaded to a limited thread pool in the browser or Node.js
- Once finished, results are passed back to the main thread via callbacks, using the event loop
Callback example: SetTimeout(onDone, delay)
SetTimeout(display, 1000);
function display() {
console.log(‘Hello’);
}, 1000);
this calls the function when done
Four Main Parts for Async
Call Stack
Web APIs
Event Loop
Callback Queue
NOW DRAW THIS
Why is error handling difficult in traditional JavaScript callbacks?
Because callbacks don’t return values, errors must be passed manually through parameters. This relies on the error-first convention (e.g., function(err, result)), which depends on developers checking err properly
Why can’t exceptions be caught with try/catch in callback-based code?
Because callbacks are executed in a separate context from the function that triggered them, there is no parent frame for try/catch to intercept exceptions.
Draw Promises Diagram
in notes
promises pros
- Flattening callback chains (dont actually create any functionality for thi alone, jsut make it easier for developers to read and “do the right thing”
- Enabling try/catch for error handling.
Refactoring Rule of 3
1) code the feature
2) code it but take note
3) refactor code first
(this acknowledges that premature refactoring is also bad)
Refactoring: 4 fun facts
- behaviour should be unchanged
- test suite should be run before and after
- without effective tests its harder to refactor
- integrates well with agile methodologies (earlier is good)
Rafactoring Triggers
- quality concern (add a new feature and its harder than expected, or bug fix is scattered)
- code review
- technical team benefits but risky and costly for stakeholders
Types of Refactoring (6)
- Rename (class/field/method).
- Move (class/field/method).
- Extract class/interface/method.
- Push down/pull up field/method.
- Replace magic number/string with constant.
- Replace inheritance with delegation.
anti-patterns: BAD (4)
- deep nesting
- bad naming
- individual style
- no comments
Name Code Smells (8)
- duplicated code
- long method
- large class
- long parameter list
- large class
- divergent change
- shotgun surgery
- feature envy
- middle man
feature envy
- when a method is in the wrong class
- refacotr method: extract
divergent change
- when many independent changes involve modifying a specific code element
- one symptom that a class is taking on more than one responsibility
- method: extract
middle man
- when a method does not do anything itself but just ends up delegating all work to another class.
- arises after refactorings that move functionality to other methods but the original base method remains
shotgun surgery
- when simple coherent changes require broad changes across a system
- can be called delocalized/scattered changes
- method: move method/field
what does REST service return?
data representation (json, html, xml)
how do REST services define resourcces?
as nouns using URIs (uniform response indentifiers)
what is in what is returned by REST service?
header: metadata and one IMPORTANT metadata, the status code
body: json, html, or xml
what is statelessness?
resource state vs application state
- clients need to maintain application state
API Versioning: 3 ways
1) PATH
- - GET /v2/users
2) QUERY
- - GET /users/?v=2
3) HEADER
- - APIVersion: 2
REST often does authentication
- token, cookies, or https