Code Design Flashcards
When should you use a singleton?
- To control access to some share resource. For example, a database object.
- To provide global access point to some data without it being overwritten. For example, giving you stricter control to global variables.
What is a leaky abstraction?
A leaky abstraction refers to any implemented abstraction, intended to reduce (or hide) complexity, where the underlying details are not completely hidden.
What are closures?
A closure is an inner function along with its enclosing environment that retains the state of its enclosing environment.
Why would you need an inner function in python?
To create a closure that retains the state of its current environment.
What is a high order function?
A function that does at least one of the following:
takes one or more functions as arguments or
returns a function as its result.
What is an example of a decorator function?
def add_messages(func):
… def _add_messages():
… print(“This is my first decorator”)
… func()
… print(“Bye!”)
… return _add_messages
What is an example of a closure?
def generate_power(exponent):
def power(base):
return base ** exponent
return power
What is an example of an abstraction?
The TCP layer over IP https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/
Or strings.
What is an example of a leaky abstraction?
Networks using TCP getting slower with more traffic. The underlying implementation of TCP becomes clearer and is felt.
What is TCP and what does it do?
Transmission Control Protocol (TCP) is a communications standard that enables application programs and computing devices to exchange messages over a network.
What is IP and what does it do?
The Internet Protocol (IP) is the method for sending data from one device to another across the internet.
How are TCP and IP related?
TCP wraps around IP to guarantee delivery of packets. IP obtains and defines the address—the IP address—of the application or device the data must be sent to. TCP is then responsible for transporting and routing data through the network architecture and ensuring it gets delivered to the destination application or device that IP has defined.