Programming: technical patterns, concepts
Software Development patterns and concepts
20 flashcards · Computer Science · by lakesare
What happens in call-by-reference languages?
const object = 1; const fn = (objectArgument) => { objectArgument = 2222222; } fn(object) // => 2222222 When you pass a variable by reference in a language that supports pass by reference feature, the caller & callee variable are identical, as if they were each others aliases, so reassigning one reassigns the other.
What languages support call-by-reference?
None of the modern ones! But, eg, C#, even though defaults to call-by-value, has special syntax for call-by-reference. A call-by-reference language makes it more difficult for a programmer to track the effects of a function call, and may introduce subtle bugs.
What is call-by-sharing?
Call-by-sharing really means call-by-value in the case where the value is an object reference. True call-by-reference means you are (in effect) passing the address of a variable, and the called method can update the variable.
What's meant by polymorphism in OOP?
Treating objects of different classes in the same way.
What is memoization? How does a memoized function work?
Optimization technique: store results of expensive function calls and return cached result when same inputs occur again. A memoized function "remembers" the results for specific inputs.
What's a tail call? Where is a(data) a tail call? function foo1(data) { return a(data) + 1; } function foo2(data) { var ret = a(data); return ret; }
Last function call that will be executed within a function. a(data) is a tail call in foo2. In foo1 '+ 1' needs to be executed, so foo1 frame needs to be present, and in foo
What is tail call optimization?
Tail call optimization means that, if the last expression in a function is a call to another function, then the engine will optimize so that the call stack does not grow. Since in a tail call the containing function is about to return, its environment can actually be discarded and the recursive call can be entered without creating a new call stack frame.
When is tail call optimization important?
Mostly in recursive calls. When we use recursion, there tends to be quite some stack frames, which slows down the program and may throw stack overflow. Tail call optimization lessens the amount of frames used, thus saving us from it.
What is currying?
When we change function that takes multiple arguments into multiple functions, each taking a single argument. The name "currying" is a reference to logician Haskell Curry.
Refactor using currying: const dragons = [ { id: 1, color: 'red' }, { id: 2, color: 'yellow' } ] const filterBy = (dragon, color) => dragon.color === color dragons.filter((dragon) => filterBy(dragon, 'yellow'))
const filterBy = (color) => (dragon) => dragon.color === color dragons.filter(filterBy('yellow'))
What's the premise of reactive programming?
Reactive programming is programming with asynchronous data streams. If we model our events as streams, we get access to rich ecosystem of composing streams. Links: perfect gist
Explain what's going on here:
Every click from the up stream maps to 1. Every click from the down stream maps to -1. These two mapped streams are merged, resulting in, e.g., 1 -1 -1 1 1 stream. Scan reduces these streams into a single value stream summing them up (-1 + 1 + 1) - counter. On every counter change, $('#counter').text = single value. Events are modeled as streams of events here.
Object-oriented languages VS prototype-based languages?
OOP: class-based: classes are defined beforehand and the objects are instantiated based on the classes prototype-based: objects are instantiated based on other objects [- their prototypes] In prototype-based languages the objects are the primary entities. No classes even exist.
What does i18n stand for?
Internalization (I + 18 letters + n) The terms are frequently abbreviated to the numeronyms i18n (where 18 stands for the number of letters between the first i and the last n in the word internationalization, a usage coined at Digital Equipment Corporation in the 1970s or 1980s).
What is Liskov Substitution Principle?
It should be possible to use subclass everywhere you would use a class. LSP forbids to violate the contracts of a supertype in a subtype, it does not forbid to change the behaviour of any method (within the bounds of that contract). An important principle of using inheritance effectively is substitutability. I should be able to substitute a Corporate Customer within any code that requires a Customer, and everything should work fine. E
What's design by contract?
When in DEBUG mode ordinary data types are extended by boolean checks (preconditions, postconditions and invariants). Prescribes that software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. Design by Contract is a design technique developed by Bertrand Meyer [Meyer]. The technique is a central feature of the Eiffel language he d…
What's is the relationship between tests and Design by Contract?
Design by Contract does not replace regular testing strategies, such as unit testing, integration testing and system testing. Rather, it complements external testing with internal self-tests that can be activated both for isolated tests and in production code during a test-phase. The advantage of internal self-tests is that they can detect errors before they manifest themselves as invalid results observed by the client.
Explain 3 types of assertions (boolean expressions) used in Design by Contract: pre-conditions post-conditions invariants
pre-conditions - how we expect the world to be before we execute an operation (e.g., pre-condition says that it is an error to invoke "square root" on a negative number and that the consequences of doing so are undefined). post-conditions - how we expect the world to be after we execute an operation (e.g., if we define the operation "square root" on a number, the postcondition would take the form input = result * result) invariants - evaluated before and after of every public method of a class.
How does Liskov Substitution Principle relate to Design by Contracts? Barbara Liskov
In a subclass: invariants must stay the same pre-conditions - can be weakened, but not strengthened post-conditions - can be strengthened, but not weakened Assertions can play a unique role in subclassing. One of the dangers of inheritance is that you could redefine a subclass's operations to be inconsistent with the superclass's operations. Assertions reduce the chances of this. The invariants and post-conditions of a class must apply to all subclasses.The subclasses can choose to strengthen t…
What does AST stand for?
Abstract Syntax Tree In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code.