General CS Knowledge Flashcards
What is O-O-P?
Object-Oriented-Programming: A programming paradigm based on the concept of objects and classes, which can contain data and code. Data being in the form of fields (e.g. attributes/properties). Code being the form of procedures (e.g. methods).
It is used to structure a piece of software program into simple, reusable pieces of code (classes) which are used to create individual instances of objects.
A feature of objects is that an objects own procedure can access and modify the data fields itself.
E.G. JavaScript, C++, Java, Python
What is a class? (oop)
A class is a defined structure to create an object. Classes often represent broad categories (e.g. dog, cat) that share attributes (e.g. colour). The classes define what attributes an instance of this type will have but not the value. Classes also contain functions (methods) that are only available to objects of that type
What is an object? (oop)
An object is an abstract data type. An object has a state (data/fields/vars) and behaviour (code/methods). It can contain multiple properties and methods, even other objects.
In most programming languages objects are defined as classes.
What is a programming paradigm and what are the 4 main types?
A way to classify programming languages based on their features:
- functional programming
- Logical programming
- object-oriented programming
- Imperative programming
What is the Imperative Programming paradigm?
The imperative programming paradigm assumes that the computer can maintain through environments of variables, any changes in a computation process. Computations are performed through a guided sequence of steps, in which these variables are referred to or changed. The order of the steps is crucial, because the given step with have different consequences depending on the current value of variables when the step is executed.
The imperative paradigm is popular because it closely resembles the machine itself.
Pros:
- efficient
- close to the machine
- popular
- familiar
Cons:
- the semantics of a program can be complex to understand or prove because of referential transparency doesn’t hold (due to side affects)
- side effects make debugging harder
- abstraction is more limited that with some paradigms
- order is crucial, which doesn’t always suit itself to problems
What is the Logical Programming paradigm?
The logical paradigm takes a declarative approach to problem solving. Various logical assertions about the situation are made, establishing all know facts. Then queries are made. The role of the computer becomes maintaining data and logical deduction.
A logical program is divided into 3 sections:
1 - a series of definitions/declarations that define the problem domain
2 - statements of relevant facts
3 - statement of goals in the form of a query
Any deducible solution to a query is returned.The definitions and declarations are constructed entirely from relations.
Pros (bifold):
- the system solves the problem, so the programming steps are kept to a minimum
- proving validity of a given program is simple
What is the Functional Programming paradigm?
The functional programming paradigm views all sub-programs are functions in the mathematical sense informally - they take in arguments and return a single solution. The solution returned is based entirely on input, the time at which the function has been called has no relevance.
Languages - functional languages are created based on the functional paradigm. Such languages permit functional solutions to problems by permitting a programmer to to treat functions as first class objects (they can be treated as data and assumed to have the value of what they return; therefore they can be passed to other functions as arguments or returned from functions).
Pros (or functional lang)
- the high level of abstraction, especially when functions are used, suppresses many of the details of programming and thus removes the possibility of committing many classes of error.
- the lack of dependence on assignment operations, allowing programs to be evaluated in many different orders. This evaluation order independence makes function-oriented languages good for programming massively parallel computers.
- the absence of assignment operations makes the function-orientated programs much more amenable to mathematical proof and analysis than imperative programs, because functional programs possess referential transparency.
What are the Object-Orientated Programming features and benefits?
OOP is a paradigm in which real world objects are viewed as seperate entities having their own state which is modified only by built in procedures (methods).
Because objects operate independently, they are encapsulated into modules which contain both local environments and methods. Communication with an object is done via message passing.
Objects are organised into classes, from which they inherit methods and equivalent variables. The o-o paradigm provides key benefits of code re-usability and extensibility.
Features + Benefits:
- a new class (called a derived class or a subclass) may be derived from another class (called a base class or super class) by a mechanism called inheritence. The derived class inherits all the features of the base class; its structure and behaviour(repsonse to messages). In addition, the derived class may include additional states (instance variables), and may exhibit additional behaviour (new method to respond to new message). The derived class can also override behaviour corresponding to some of the methods of base class: there would be a different method to respond to the same message. The inheritance method is allowed even without access to the source code of the base class.
- The ability to use inheritance is the single most distinguishable feature of the o-o paradigm. Inheritance gives OOP its chief benefit over other programming paradigms - relatively easy code reuse and extension without the need to change existing source code.
- The mechanism of modelling a program as a collection of objects of various classes and futhermore describing many classes as extensions or modifications of other classes provides a high degree of modularity.
- Ideally the state of an object is manipulated and accessed only by that objects methods. (Most o-o languages allow direct manipulation of the state, but such access is stylistically discouraged). In this way a class interface (how objects of that class are accessed) is separate from the class’ implementation (the actual code of the class’ methods). Thus encapsulation and information hiding are inherent benefits of OOP.
What is two way data binding and one way data flow and how are they different?
Two way data binding meaning the UI fields are bound to model data dynamically, such that when the UI field changes, the model data changes with it and vice-versa. Angular is a popular framework that uses 2-way-binding.
One way data flow means that the model is the single source of truth. Changes in the UI trigger messages that signal user intent to the model (or ‘store’ in react). Only the model has access to change the apps state.
The effect is that data always flows in a single direction, making it easier to understand. React uses one way data flow, cycle.js is another uni-directional flow example.
One way data flows are deterministic, where as two way data binding causes side-affects which are harder to understand/follow.
What is a monolithic architecture? pros/cons
A monolithic architecture means your app is written in one cohesive unit of code, whose components are designed to work together, sharing the same memory space and resources.
Pros:
- Most apps typically have a large number of cross cutting concerns e.g. logging, rate limiting, security features such as audit trails and DOS protection.
- When everything is running through the same app, it’s easy to hook up components with those cross cutting concerns.
- There can also be performance advantages since shared memory access is faster than IPC (inter-process communication)
Cons:
- Monolithic app services tend to get tightly coupled and entangled as the application evolves, making it difficult to isolate services for purposes such as scaling or code maintainability.
- Monolithic architectures are harder to understand because there may be dependencies, side affects and magic which are not obvious when you are looking at a particular service or controller
What is a micro service architecture? pros/cons
A micro service architecture means you app is made up of lots of smaller, independent applications capable of running their own memory space and scaling independently from each other across potentially many separate machines.
Pros:
- typically better organised since each micro service has a very specific job and not concerned with the jobs of other components. Decoupled services are also easier to recompose and reconfigure to serve the purposes of different apps
- they can have performance advantages depending on how they are organised because its possible to isolate hot services and scale them independent of the rest of the app.
- tend to scale better
Cons:
-if your building a new micro service architecture, your likely to discover lots of cross cutting concerns that you did not anticipate at design time. A monolithic app could establish shared magic helpers or middleware to handle such cross cutting concerns without much effort.
-in a micro service architecture, you’ll need to incur the overhead of separate modules for each cross-cutting concern, or encapsulate cross-cutting concerns in another service layer that all traffic gets routed through.
Eventually even a monolithic architectures tend to route traffic through an outer service layer for cross-cutting concerns. But with a monolithic architecture it’s possible to delay the cost of the work until the project is much more mature
-micro services are frequently deployed on their own virtual machines or containers, causing a proliferation of VM wrangling work. These tasks are frequently automated with container fleet management tools.
-higher initial cost
What is asynchronous programming and why is it important in JavaScript?
Synchronous programming means that, barring conditional and function calls, code is executed sequentially from top to bottom, blocking on long running tasks such as network requests and disk I/O.
Asynchronous programming means that the engine runs in an event loop. When a blocking operation is needed, the request is started, and the code keeps running without blocking the result. When the response is ready, an interrupt is fired, which causes an event handler to be run, where the control flow continues. This way a single program thread can handle many concurrent operations.
User interfaces are asynchronous by nature and spend most of their time waiting for user input to interrupt the event loop and trigger event handlers.
Node is asynchronous by default, meaning that the server works in much the same way, waiting in a loop for a network request, and accepting more incoming requests while the first one is being handled.
This is important in JavaScript because it is a very natural fit for user interface code, and very beneficial to performance on server.
What is a closure?
A closure is the combination of a function bundled together with references to its surrounding state (the lexical environment). A closure gives you access to an outer functions scope from an inner function. In JavaScript closures are created every time a function is created.
To use a closure, define a function inside another function and expose it. To expose it, return or pass it to another function.
The inner function will have access to variables in the outer function scope, even after the outer function has returned.
Using closures (examples)…
- commonly used to give people data privacy
- In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing outer function. You can’t get at the data from an outside scope except through the objects privileged methods.In JavaScript, any exposed method defined within the closure scope is privileged.
- Closures can also be used to create stateful functions, whose return values may be influenced by their internal state. e.g. const secret = msg => () => msg;
- in functional programming, closures are also used for partial application and currying
Define Application
The process of applying a function to its arguments in order to produce a return value.