# Leaky Abstractions

## Summary

In any large system of many components, it's impossible to keep in view full implementation details of all components. To manage complexity better, it's helpful to abstract away the details of other components when working on a particular component. Each component talks to other components via **interfaces** without worrying about the implementation details of those components. It's for this reason that **abstractions** are used.

Sometimes an abstraction is not perfect. When an abstraction fails to hide some of the underlying implementation details, we call this a **leaky abstraction**. In this case, client users of that interface will experience wrong or unsatisfactory behaviour. Clients can mitigate this by considering implementation details behind the interface and changing the way they use the interface.

## Discussion

### Could you explain leaky abstractions with an example?

Let's take the example of hashing that takes plaintext and produces a hash out of it. This problem is so well defined that application programmers rarely need to write their own implementation. Many libraries are available for hashing and their interfaces nicely abstract away the implementations. Programmers rarely need to bother how the hashing is implemented. They can simply treat hashing as a "black box". Hashing is an example of a good abstraction.

An example of a leaky abstraction is Axios that wraps the *fetch* JavaScript API in browsers. When there's an HTTP error, Axios will coerce it into JavaScript error. This behaviour is different from *fetch* that treats even HTTP 404 responses as successful responses. Axios behaviour may work for many use cases but it's not the general case. Some applications may not want this behaviour.

Consider a database search. A MySQL query containing `LIKE 'abc%'` is fast but one containing `LIKE '%abc%'` is slow. This is because indices use binary trees in which the latter search is not optimized. Thus, the implementation is exposed and clients have to be aware of this.

### Which are the different types of leaky abstractions?

In some examples of leaky abstractions, we find that **performance** is affected. An MySQL query may run lot slower than expected. An array access may take lot longer than expected.

In other examples, we find that the **behaviour** is not as expected of the abstraction. An HTTP 404 status code is coerced into a JavaScript error. A database orchestration layer promises support for transactions when in fact it can't achieve this when dealing with multiple SQL and NoSQL databases. A single call to the service layer results in six HTTP calls when in fact the caller expects only one.

Another variant, or perhaps a related leak, is called **technical leak**. This can be stated as "it compiles, but doesn't work". For example, an interface would work only if the methods are called in a certain order. This is a technical leak that's called *temporal coupling*. Another example is initializing an object before using it or closing a database before destroying the object. Technical leaks require developers to learn something about the implementation even when it has nothing to do with business logic.

### What does the Law of Leaky Abstractions say?

This is a phrase coined by Joel Spolsky in 2002. It says,

> All non-trivial abstractions, to some degree, are leaky.

Spolsky gives many examples where leaky abstractions arise. TCP provides higher layers reliable transport and delivery of packets. However, TCP can't do anything if a cable is cut or there's an overloaded hub along the way. The abstraction therefore leaks.

When iterating over a 2-D array, performance shouldn't differ if you iterate by rows versus by columns. Ideally, a programmer should not care about how the array is stored in memory. In reality, when virtual memory is involved and page faults happen, some memory accesses may take a lot longer. C++ string class is another example of leaky abstraction. They're not first-class data types. On a string instance `s`, we can do `s + "bar"` but when we do `"foo" + "bar"` we have to recognize that strings are really `char*` underneath.

In conclusion, abstractions are good when writing code but we still have to learn what's underneath them. High-level languages with abstractions are paradoxically harder to work with since we have to learn what these abstractions are attempting to hide.

### How can developers overcome leaky abstractions?

Abstractions reduce complexity but they're not perfect. If an abstraction leaks too much, remove it or create a better one. If you're writing an abstraction, document its limitations. Abstractions are good but having too many adds complexity, as noted by David J. Wheeler,

> All problems in computer science can be solved by another level of indirection, except for the problem of too many layers of indirection.

Given that at least some abstractions will leak, developers could create a **wrapper** around the abstraction. The application is required to call this wrapper rather than the original abstraction. This wrapper would modify the behaviour into what the application expects.

In a more extreme case, the developer **reimplements** the functionality to suit the application. This is not a good practice since, with the loss of the abstraction, the application becomes more complex.

Another approach is to **code between the lines**. The developer understands the implementation behind the abstraction (such as how memory is allocated) and contorts the code to suit that implementation. Code becomes more complex, less readable and less portable to other platforms.

## Milestones

1974

The fact that abstractions leak is recognized in the design of high-level programming languages that tend to abstract low-level details. A quote by Niklaus Wirth is relevant here,

> I found a large number of programs perform poorly because of the language’s tendency to hide “what is going on” with the misguided intention of “not bothering the programmer with details.”

1980

In the context of programming languages, some decisions taken by language designers are seen to be **pre-emptive**, that is, they constrain developers to use the language in a specific way. For example, a developer needs two triangular arrays but is forced to use two rectangular arrays (more memory) or pack them into a single rectangular array (complex code). We may say that the abstraction provided by the language doesn't suit such specialized use cases.

1992

Gregor Kiczales explains leaky abstractions at a workshop. He proposes to divide an abstraction into two parts: one that does abstraction in the traditional way and another that allows clients some control over the implementation. He calls this an **open implementation** supported by meta-level architectures and metaobject protocols. For designing meta-level interfaces he notes four design principles: scope control, conceptual separation, incrementality and robustness.

2002

Joel Spolsky on his blog *Joel on Software* coins and explains the phrase "Law of Leaky Abstractions".

2006

Ryan Bemrose of Microsoft states a corollary to the Law of Leaky Abstractions, "An abstraction should not hide or disable that which it abstracts". He gives an example of an IRC bot that could interface with third-party plugins. The IRC protocol itself is abstracted via an object-oriented interface. It's discovered that plugins that use custom modes can't function properly because such functionality was disabled by the abstraction. Abstractions are useful but we shouldn't attempt to abstract away everything.

Apr
2017

At the ContainerWorld 2017 conference, one speaker notes that **containers** are also leaky abstractions. Processes running inside containers have to sometimes know about I/O performance, versions, configurations, garbage collection of old images, etc. In one example, it's seen that two containers contending for the same IO are affected.

Leaky Abstractions


# gRPC

## Summary

Tightly integrated monolithic applications of the past gave way to modular design. With the growth of the Internet, different parts an application could reside in different servers and accessed over the network using what we *Remote Procedure Call (RPC)*. With the advent of cloud computing, applications are composed of microservices. These microservices are loosely integrated but their interfaces are precisely defined using APIs.

gRPC is a framework that enables the implementation of highly scalable and performant application whose parts are distributed over a network. The framework abstracts away the low-level networking details from app developers so that they can focus on the application logic. Developers need not worry about how one part calls a functionality in another remote part. gRPC takes care of this and enables more responsive real-time apps.

**gRPC** is a recursive abbreviation that expands to **gRPC Remote Procedure Call**.

## Discussion

### How has gRPC come about?

Since the 1990s, networking has played an important part of computing infrastructure. In distributing computing, one computer can trigger an action on another remote computer. CORBA, DCOM and Java's Remote Method Invocation(RMI) were used to make this possible. As the web matured in early 2000s, RPC methodology started relying more and more on HTTP, XML, SOAP and WSDL. The idea was to use standardized technologies for interoperability regardless of languages or platforms. With Web 2.0 and cloud computing, HTTP/XML/SOAP combination was replaced with HTTP/JSON, which resulted in REST. Cloud applications were composed of microservices and these communicated via REST APIs.

More recently, smartphones and IoT devices are invoking these microservices. These devices have limited compute power, communication bandwidth or both. In such cases, HTTP/JSON with REST is seen as an inefficient approach. gRPC solves this problem. In fact, back in 2016, within it's data centers Google made about 100 billion RPC calls per second. At that scale, Google calling REST APIs would have been inefficient.

### What are the essential elements of gRPC?

One way to summarize gRPC is that,

> It's a lean platform using a lean transport system to deliver lean bits of code.

**gRPC** is the platform. **HTTP/2** is the transport. **Protocol Buffers** deliver lean bits of code. Together, they reduce latency and improve efficiency.

gRPC depends on HTTP/2. HTTP/2 supports bidirectional streaming, flow control and header compression. Moreover, multiple requests can be sent over a single TCP connection.

When distributed parts of an application need to communicate with one another, data needs to be serialized. Protocol Buffers is used as the *Interface Definition Language (IDL)* to specify both the service interface and the structure of the payload messages. Thus, the specification is developer-friendly. In addition, data is sent on the wire in an efficient binary format. The encoding and decoding is transparent to the developer and done automatically by the framework.

gRPC is about services and messages, not objects and references. gRPC is language agnostic: developer can use any programming language. gRPC is payload agnostic: the use of Protocol Buffers is not required. gRPC provides authentication out of the box.

### What are the benefits of using gRPC?

In the world of RESTful JSON API, there can be problems of version incompatibility: the API has changed at the server, and client is using an older version. Writing a client library along with authentication is also extra work. The use of HTTP/1.1 is also a poor fit for streaming services. Because JSON data is textual, it takes more bandwidth. It also takes more work to encode and decode JSON.

With gRPC, both client and server code are generated by the compiler. gRPC comes with language bindings for many popular languages. The use of Protocol Buffers solves the efficiency problem of JSON. Bidirectional streaming is possible and authentication is part of the framework.

Protobuf ensures backward compatibility and offers validations and extensibility. Protobuf provides static/strong typing that REST lacks. This minimizes the overhead of encoding.

Compared to other RPC frameworks, gRPC speaks the language of the web (HTTP/2), comes with multiple language bindings, and therefore makes it easier for developers.

### What are the different gRPC life cycles?

gRPC life cycles include:

    + **Unary**: This is the simplest one. When client calls a server method, server is notified with client metadata for this call, method name and deadline. Server may respond with its own metadata or simply wait for client's request message. Once request is received, server will execute the method, then send response and status code to client.
    + **Server Streaming**: Similar to the unary case, except that server sends a stream of responses.
    + **Client Streaming**: Similar to the unary case, except that the client sends a stream of requests. Server need not wait for all requests before sending a single response.
    + **Bidirectional Streaming**: This starts with a client request but subsequently client and server can read or write in any order. Each stream operates independent of the other. It's really application dependent.gRPC calls can be either synchronous or asynchronous. Since networks are inherently asynchronous, asynchronous calls are recommended so that current thread is not blocked.

### Where does gRPC fit within the communication stack?

gRPC is the framework layer that interfaces to the transport layer below and to the application layer above. In fact, gRPC makes it easier for application developers to make RPC calls over the network since the framework abstracts away all the low-level network operations. By using HTTP/2 for transport, gRPC benefits from all the performance enhancements that HTTP/2 offers over HTTP/1.1.

Developers can write apps in any language of their choice. To ease the job of calling framework APIs, gRPC provides language bindings in many popular languages. The core of gRPC is implemented in C for performance reasons. These language bindings are therefore wrappers over the low-level C API.

Finally, application code can be fully hand-written but it's possible to generate boilerplate code based on message definitions. For example, if we use protobuf for data serialization, the *protobuf compiler* can be used to generate request/response and client/server classes. The developer can therefore focus on writing her application to invoke the methods of these generated classes.

### What are some myths surrounding gRPC?

Here are some myths about gRPC:

    + gRPC works with only microservices: gRPC can be used in any distributed app that needs to communicate over the network.
    + gRPC depends on protobuf: Protocol Buffers are used by default but any other suitable data serialization protocol can be used depending on specific app requirements. JSON could be useful when data must be human readable or data needs to be directly consumed by a web browser.
    + gRPC will replace REST: This may happen in future but right now both will probably coexist and even interwork. Prefer REST for interoperability and gRPC for performance. It's easy to directly consume REST APIs with tools such as curl, but gRPC tooling may improve to offer similar functionality.
    + gRPC is only for server-side communications: Web browsers can also use gRPC. grpc-web is one project that enables this.
    + gRPC core is implemented in C: C is the default implementation but there are implementations in Go, Java, Swift, Dart and JavaScript.

### If I'm developing a gRPC service, what's the recommended workflow?

First you define the service methods and data using an IDL of your choice. Protocol Buffers is one possible choice of IDL. Definitions are stored in `*.proto` files.

The next step is to invoke the `protoc` compiler to generate code in languages of your choice. Many languages are supported as on June 2018: C++, Java, Python, Go, Ruby, C#, Node.js, Android Java, Objective-C, PHP and Dart.

On the client side, gRPC functionality is implemented in what's called a *stub*. Your app code can call into methods of the stub along with local objects as arguments. gRPC will translate the calls to gRPC requests to the server along with binary serialized protobuf.

### How do I migrate my REST API-based web service to gRPC?

Migrating a service to gRPC overnight might break many clients still using REST APIs. It's therefore recommended to have a transition period in which REST and gRPC services are both operational. Even in the long term, it might a good idea to redirect REST calls to gRPC.

Project grpc-gateway is a way to generate *reverse proxy* code. grpc-gateway works as a plugin to protobuf compiler. The reverse proxy will then translate RESTful JSON API into gRPC method calls.

Within Google Cloud, transcoding HTTP/JSON to gRPC happens via what is called *Extensible Service Proxy (ESP)*. This is enabled by adding annotations in your `*.proto` files or separately in YAML files.

### Who's been using gRPC in the real world?

gRPC has been adopted by CoreOS, Netflix, Square, Cockroach Labs and etcd. Telecom companies Cisco, Juniper, and Arista are using it for streaming telemetry data and network configurations. Docker's containerd exposes functionality via gRPC.

Square moved from a proprietary RPC framework to gRPC and they have shared their experience on YouTube. Lyft has used gRPC along with Envoy. For some services, they used a Go implementation of gRPC. For others, they used Flask-Python with gevent, which didn't work well with gRPC. Hence, they used Envoy to bridge gRPC with HTTP/1.1 while using protobuf.

### Could you share some performance numbers of gRPC?

A benchmark test on Google Cloud using 9 gRPC channels compared gRPC performance versus HTTP1.1/JSON. gRPC achieved 3x throughput using only a fourth of the resources. On a per CPU basis, this was 11x the HTTP1.1/JSON throughput. This improvement is due to protobuf in which data is kept and transmitted in binary without involving any Base64 or JSON encoding/decoding. In addition, HTTP/2 can multiplex requests on a single connection and apply header compression.

Another test involving 300K requests of key/value stores in *etcd* showed that JSON took 1.6ms, single-client gRPC took 122.4µs and 100-client gRPC took 12.9µs. Memory usage per operation dropped by 44%. When comparing single-client and 100-client gRPC cases, the latter ran in one-fifth the time because multiple requests go on a single TCP connection.

When sending files over gRPC, it's been shown that plain HTTP/2 is faster because there's no encoding or decoding; it's just raw data transmission.

### How does gRPC compare against other RPC frameworks?

When comparing different RPC frameworks, gRPC's deserialization can be slow compared to Cap'n Proto. Within gRPC, a C++ implementation is faster than one in Java, Go or Python. It should be noted that performance depends on the message complexity. Another benchmark tests showed that gRPC isn't the fastest. Cap'n Proto, rpclib and Thrift all perform better with Cap'n Proto being the best for deeply nested message structures. This could be because gRPC forgoes zero-copy approach that Cap'n Proto or Flatbuffers use. Instead, gRPC aims to encode data for smaller bandwidth requirements at the expense of extra CPU computation.

Thrift and gRPC both support code generation and serialization. However, Thrift doesn't use protobuf or HTTP/2. While REST+JSON API isn't RPC, Swagger is a tool that supports API design and code generation. gRPC alternatives in Java are many.

Some developers might find it useful to study the implementation of different RPC mechanisms. Developer Renato Athaydes claims that gRPC doesn't work well with JVM types and hence provides his own version of RPC based on Protocol Buffers.

## Milestones

Jul
2008

Initial version of **Protocol Buffers** is released. Version 1.0 of Protocol Buffers (internal to Google) inspires the design and implementation of version 2.0, often called *proto2*. Profocol Buffers are also called *protobufs*.

Dec
2014

Version 3.0-alpha of Protocol Buffers (*proto3*) is released. A stable version 3.0.0 is released in July 2016. This development parallels that of gRPC.

Feb
2015

Google releases and open sources **gRPC** based on its vast experience in building distributed systems. Google states that they're starting to expose most their public services via gRPC endpoints. gRPC uses HTTP/2. Meanwhile, Internet Engineering Steering Group (IESG) approves HTTP/2 as a proposed standard. In May 2015, HTTP/2 is published as RFC 7540. gRPC itself is influenced by Stubby, an earlier RPC framework used by Google internally.

Aug
2016

Google releases V1.0 of gRPC with improved usability, interoperability, and performance measurement. This version is now considered stable and production ready. Language support includes C++, Java, Go, Node, Ruby, Python and C# across Linux, Windows, and Mac; Objective-C and Android Java on iOS and Android.

Mar
2017

Cloud Native Computing Foundation (CNCF) adds gRPC to its portfolio. This means that CNCF will host and guide the development of gRPC.

gRPC


# Functional Programming

## Summary

Functional programming (FP) puts emphasis on functions, where functions are closer in spirit to mathematical functions than functions or subroutines in computer science. A function is something that takes inputs and returns outputs without any side effects. A set of inputs given to a function will always return the same set of outputs. This implies that functions do not depend on global variables, shared resources, the thread context or the timing across threads. Absence of side effects is essential to FP.

Functional programming has been around since the 1950s but they got sidelined by imperative languages such as C, C++ and Java. Today, in the context of parallel computing, synchronization across parallel threads can be difficult. FP has come to the rescue. FP enables clean abstractions, component design and interfaces making the program more declarative and shorter.

## Discussion

### What exactly do you mean by side effects?

When a function relates to its external world in a hidden manner, then it has side effects. For a function without side effects, its inputs are passed explicitly as function arguments. It's output is via function return. Such a function does not receive inputs via global variables such as message queues, shared buffers, databases or files. Likewise, it does not modify external entities such as writing to a file or making an AJAX call in a web application.

The goal of FP is to reduce side effects, not completely eliminate them in the entire program. Since programs have to interface to users, write to files or make network calls, they cannot be without side effects. We can however make most of the program functional by writing functions without side effects. It's important to track which parts of the system are impure. Some languages have a type system to facilitate this.

Sometimes input and output are independently recognized as side causes and side effects respectively. More commonly, it's sufficient to use the term side effects to refer to both.

### What conditions should a programming language satisfy to be considered functional?

A pure FP language should prohibit side effects. FP places importance on expressions rather than statements. In this sense, FP is an instance of declarative programming, which implies that focus is on what is to be computed rather than how to compute it. Objects are immutable: once created, they cannot be changed. The focus is on data manipulated by functions rather than states stored within objects. It has been said that "immutability and statelessness are core to functional programming."

Program state is tracked as part of function arguments. Thus, function recursion is preferred to looping based on loop variables. Data flow is more important than control flow and this facilitates concurrency. The order of function calls doesn't matter if the data flow is preserved.

FP does not require dynamic typing or static typing for that matter. The use of compile-time AST (Abstract Syntax Tree) macros does not imply FP.

### How is FP different from imperative programming?

With imperative programming, assignment of a variable is a common operation. For example, `x = x + 1` is a valid operation in C, C++ or Java. But this makes no sense from a mathematical perspective; that is, if we consider it as an equation, there's no solution for it. In FP, data is transformed but not mutated. Expressions are important but storing the result is not. Results are passed around to other functions.

Imperative programming is about objects and their states. FP is about pure functions and higher-order functions. Complex behaviour in FP can be constructed by composing simpler functions. If OOP is about passing objects, FP is about passing functions. With imperative programming, operations are specified step by step. Loops with loop variables are used to iterate over lists. With FP, the style is declarative. Recursion and list processing using iterators are common control structures.

Michel Feathers tweeted in 2010 that,

> OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.

### Can you explain pure functions, first-class functions and higher-order functions?

Pure functions conform to the rules of lambda calculus. Programs written in pure FP will be referentially transparent and their correctness can be formally proven. A practical way to define pure functions is that they have no side effects and always produce the same outputs for the same inputs. This is also called determinism. Impure FP therefore have elements of FP such as higher-order functions but may allow mutation of variables or even access to data outside the scope of functions. Side effects could be a language feature or due to monads. Closures cannot be considered as pure functions.

First-class functions can be assigned to variables, passed as arguments to other functions and can be returned from other functions. Functions in FP are first-class functions. Higher-order functions are functions that accept a function as argument and/or return a function as output. Thus, first-class functions is about the status that functions have in the language whereas higher-order functions are explicitly those that accept/return functions as input/output respectively. They eliminate loops. Code can be refactored to avoid repetitive code. Closures are examples of higher-order functions.

### Can you point out some concepts/features of FP?

We may note the following:

    + Closure: This is a data structure with a code pointer and an environment of bound variables, implemented as anonymous functions. Alternatively, "a closure is a function's scope that's kept alive by a reference to that function."
    + Referential transparency: This is a property of pure functions that always return the same outputs for the same inputs. Another way of stating this is that an object can be replaced by its value since objects are immutable. Equational reasoning can be applied to code for either provability or optimization.
    + Currying: A function with multiple arguments is transformed into a chain of functions, each taking a single argument. Currying is useful to adapt a function to a form that some other function or interface expects. Currying is sometimes confused for partial functions.
    + Composability: When functions perform specific tasks, we can use them as building blocks to compose more complex functionality. While composability is a concept, currying and pipelining may be considered as an implementations for it.

### What techniques do FP languages use?

We may note the following:

    + Tail call optimization: With recursion, FP usually supports tail call optimization that reuses the same stack frame for a sequence of recursive calls. Without this, program can run out of stack memory when recursion descends many levels deep.
    + Pattern matching: A concise way to match a value or type and thereby avoid complex control structures.
    + Lazy evaluation: Code will be executed only when required. Haskell does this well. Graph reduction is a technique to implement lazy evaluation.
    + Partial functions: They wrap a function by fixing some arguments to values and retaining the rest as arguments. In fact, partial functions simplify implementation of closures.
    + Pipelining: Pass the output of one function as input to another. Many function calls can be chained in a sequence in this way. Pipelining makes possible code reuse, unit testing and parallel execution.
    + Continuation: This enforces execution in a specified order, which is particularly useful when lazy evaluation would skip execution.

### Why or when should we use FP?

With multicore, multi-processor or networked systems, concurrency is becoming a challenge. With no side effects, FP is an approach that eliminates shared variables, critical sections and deadlocks. Erlang implements Actor concurrency model, meaning that messages are passed between threads rather than sharing variables.

Since there are no side effects (and side causes), FP are easier to test and debug. Unit tests need to worry about only function arguments. Peeking into the stack is enough to debug issues. Hot code deployment is possible with FP since object instances holding states don't really exist in FP. Reability of code generally improves with FP. Code is concise since the focus is on what rather than how.

For mission-critical systems, program correctness can be proven formally. Likewise, compilers can transform code into more optimized forms. These are possible due to the mathematical basis on which FP is founded. Due to lazy evaluation, it's possible to optimize code, abstract control structures and implement infinite data structures.

### Where is FP used in the real world?

Ericsson uses Erlang in telecommunication switches. AT&T uses Haskell for network security. Akamai content delivery network uses Clojure while Twitter uses Scala. Elm is a pure FP language that compiles to JavaScript. It is used by NoRedInk. Computer aided design (CAD) software often support AutoLISP. OCaml has been used for financial analysis and research. Scala is widely used for data science. Clojure is being used in wide range of applications from finance to retail. Hughes shows examples of using FP for numerical problems as well as gaming.

Many uses of Haskell in industry is well documented. An annual conference named Commercial Uses of Functional Programming (CUFP) has been going on as early as 2004 to track and share use cases and best practices. As an example, the uses of Caml in industry was presented in 2007.

### Which programming languages are considered as functional?

While LISP is one of the earliest FP language, the following (non-exhaustive list) are among those being used commercially: Haskell, F#, Clojure, Scala, OCaml, SML, Erlang. Wikipedia gives lists of FP languages that are considered pure and impure. Excel may be considered as a FP language.

It's likely that object-oriented programming (OOP) will coexist with FP. For complex applications requiring concurrency, FP may be preferred. F# and Scala combine elements of FP and OOP. Some have even used the term "object-functional programming" to mark this trend.

Many popular languages while not designed as FP, can be used in part as FP. Examples include Java, Python, and R. Sometimes third-party libraries or packages enable FP, such as Swiftz for Swift, Underscore.js for JavaScript and Fn.py for Python. Immutable.js in JavaScript enables data immutability.

### Why did FP take so long to get popular?

FP started with ideas rooted in mathematics, logic and formalism. This appealed to academics who approached it theoretically. It didn't appeal to the programming community who considered functions more as code blocks for partitioning behaviour and reusing code. To them, imperative programming (including OOP) was more practical. It was also believed that FP, with all its limitations, could not be used to build big complex programs. FP was also believed to be slower.

Some have argued that FP involves abstraction. This can be difficult for programmers used to coding with loops and assignments.

### How is FP different from lambda calculus?

FP may be regarded as a practical implementation of lambda calculus. Lambda calculus was a tool to study the problem of computability. It is independent of physical constraints. FP is application of some ideas of lambda calculus.

## Milestones

1936

Alonzo Church invents lambda calculus at Princeton University while formally investigating ways to program machines. This is part a bigger effort at Princeton where others including Alan Turing, John von Neumann and Kurt Gödel attempt to formalise mathematics and computing.

1956

IPL is created at Carnegie Institute of Technology and could be considered as the first FP language (in assembly), though it has some imperative language features.

1958

MIT professor John McCarthy invents List Processing Language (LISP) as the first implementation of Church's lambda calculus. Common Lisp and Scheme are two popular dialects of LISP. The influence of lambda calculus on Lisp has been challenged.

1973

Programmers at MIT's Artificial Intelligence Lab build a Lisp machine. Thus Lisp got its own native hardware rather than running on von Neumann architecture.

1973

Robin Milner at the University of Edinburgh creates ML (Meta Language). OCaml and Standard ML (SML) are popular dialects of ML.

1977

John Backus publishes a paper describing the proper use of functions and expressions without assignments and storage. Some call this function-level programming but the paper influences FP.

Functional Programming


# Web Storage

## Summary

Client-server interactions on the web are stateless. This means that a client request is independent of all previous requests. However, many applications require state to be maintained across requests. Examples are shopping on e-commerce sites or filling a form after login authentication. HTTP cookies sent with every request allow us to maintain state. Moreover, server may store session data, often in a database.

Web Storage is a client-side feature that allows web apps to store data or state on the client/browser without involving the server. Unlike HTTP cookies, storage is managed at the JavaScript layer.

Each web storage area is bound to an origin, which is a triplet of domain, protocol and port. Thus, one domain can't access the storage of another.

Web Storage shouldn't be confused with cloud storage, which is about storing data on cloud computing platforms.

## Discussion

### Why should I use Web Storage when I can use cookies instead?

Cookies have been used to create client-side state but it has it's limitations. Cookies can be created only on the server and then exchanged with the client. Client must include the cookies with every request to the server. The amount of cookie storage is limited to 4KB. Cookies also are not persistent. They have an expiry time.

W3C Recommendation for Web Storage notes an example in which a user could have the same site open in two windows. With a cookie implementation, the cookie would "leak" from one window to the other, with the user ending up with duplicate flight bookings.

With web storage, data can be stored on the client without any support from the server. This is useful for building Single Page Applications (SPAs). Unlike cookies, few megabytes can be stored. Two different types of storage areas are available: local and session. With the former, storage is persistent even if the browser is closed and reopened.

Some browsers allow 10MB of web storage per origin while others allow 5MB.

### In Web Storage, how is browser local storage different from session storage?

Local storage has no expiry. Even if the browser is closed, the data will persist. Session storage is limited to the current tab. It will be lost when the tab is closed.

With session storage on an e-commerce site, it's not possible to accidentally make double purchases just because the same site is open on two different tabs. Using local storage for this use case is not a good idea since the storage is accessible to all tabs opened to that site.

### Could you share real-world examples of sites using Web Storage?

By 2011, many websites had started to use web storage. A Twitter search tool called Snapbird used it to remember the last person's Twitter stream that the user had searched from that browser. The assumption is that user is likely to continue the same search at a later time, thus saving some effort in retyping the name. Webbie Madness and CSS Lint are two other examples that use local storage to remember user's selection of checkboxes.

Web storage is not used to store sensitive content such as passwords, JSON Web Tokens or credit card information. However, one developer used it to store the hashed ID of an authenticated user. This hash is later used to give access to protected pages. For example, it can be used to maintain a shopping cart for an e-commerce site.

In social websites, relying on the server for data can involve complex technologies such server-side caching, distributed databases and DNS load balancers. Instead, web storage can be used to locally store recent chat messages, system notifications and news feeds.

### What are the basics of managing Web Storage?

The API for using web storage is simple. No libraries need to be installed. Web storage can be used with plain JavaScript code. The storage is just key-value pairs with values being only strings. In case more complex data types need to be stored, such as JavaScript objects or arrays, they need to be serialized into strings.

To store `username` for example, we can use the syntax `localStorage.username = 'jsmith'` or `localStorage.setItem('username', 'jsmith')`. To retrieve data, use `localStorage.getItem('username')`. Use `localStorage.length` to know how many items are stored. A key can be accessed by its integer position `n` by calling `localStorage.key(n)`. To delete a key-value pair, call `localStorage.removeItem('username')`. Call `localStorage.clear()` to delete all pairs.

The syntax described above for `localStorage` is also applicable for `sessionStorage`.

When either local or session storage is modified, `storage` event is fired. The event will contain key, old value and new value.

An app will fail if it depends on web storage but not supported in a particular browser. The solution is to reliably ascertain browser support and have fallback code.

### What are some common criticisms of Web Storage?

Web Storage is limited to storing strings. Although more complex data types can be serialized and stored as strings, this is seen as an "ugly hack". In terms of size, few megabytes may not be enough for data-intensive apps or apps that need to work offline. Moreover, it's not easy to know when storage is at its limit.

Providing app functionality that's highly dependent on web storage can be problematic. Users might delete the data just as they tend to clear cookies.

The API is synchronous, which implies data loading can block rendering on the main thread. This would be worse if files and images are stored. Likewise, for background processing, web workers can't be used since they can't access web storage.

Web Storage has security issues. Any JavaScript code on that page can access the storage. Cross-site scripting attacks are possible. Malicious JavaScript code can read the storage and send it to another domain of its choice. This is unlikely if the entire code was built in-house. However, it's common for apps to use third-party code, which could have been compromised.

### Could you share some resources for working with Web Storage?

Beginners can look at a demo of Web Storage as well as details of the storage event that's triggered by each change to the storage.

There are wrappers around Web Storage. *Lockr* allows developers to store non-string values as well. *Store.js* is similar to Lockr but with fallback should the browser lack support for web storage. *Crypt.io* (previously called secStore.js) adds a security layer. *Barn* provides a Redis-like interface. For asynchronous calls, *localForage* combines the easy-to-use Web Storage API with IndexedDB. A quick search on NPM will reveal many more packages related to local storage.

PersistJS is a cross-browser alternative to client-side storage.

If your app uses web storage, it's easy to unit test your app even without access to browser's web storage. Web Storage can be *mocked* and one developer shows how to do this in Angular.

### Which are the alternatives to Web Storage?

For sensitive data such as a session ID, it's better to use signed cookies. When using cookies, use the `httpOnly` cookie flag and set `SameSite=strict` and `secure=true`. For most other sensitive data, use server-side storage.

For non-string and non-sensitive data, use IndexedDB. This gives standard database features including primary keys, indexing, and transactions. For offline applications, we could use a combination of IndexedDB and Cache API that's often used by service workers. In fact, it's possible to create a polyfill to override default Web Storage API to store in IndexedDB and store asynchronously.

Web SQL used to be an alternative but W3C stopped working on this due to lack of implementations.

Application Cache is a text file to tell browsers what resources to cache. This could be useful for offline viewing of the site. It's more of a caching mechanism than client-side storage.

## Milestones

Apr
2009

W3C releases the first working draft of **Web Storage**. This draft defines both `localStorage` and `sessionStorage`. In addition, it defines an interface to store in databases.

Jun
2013

W3C publishes **Web Storage** as a W3C Recommendation. Database storage defined in the first draft is deleted in the Recommendation. Both `localStorage` and `sessionStorage` are referred to as *IDL attributes*, following Web IDL (Interface Definition Language) that's used to describe interfaces web browsers are required to implement. The Recommendation also points out issues of user privacy and sensitive data, and how user agents (browsers) can mitigate the risks.

Apr
2016

W3C publishes **Web Storage (Second Edition)** as a W3C Recommendation.

Apr
2019

Web storage is slow due to its synchronous nature. On the other hand, IndexedDB is asynchronous but has a more complex API. As a compromise, **KV Storage** is proposed. It's a key-value storage like web storage but with IndexedDB as the underlying storage. It becomes available with the release of Chrome 74 on an experimental basis. The trial ends with Chrome 76. In future, IndexedDB may come with a simpler API.

Web Storage


# Arduino Programming

## Summary

Programming Arduino boards is done via a language called **Wiring**, which itself is based on Processing and C/C++ languages. We can think of Wiring as providing a simpler abstraction on top of the more complex C/C++ syntax.

Hardware can be programmed directly using a USB connection. Arduino IDE is open source and it supports all the Arduino variants. It's also possible to program the hardware from within a web browser after installing a plugin.

Every Arduino board will have constraints of memory, GPIO pins, and so on. Developers should be aware of these and optimize the code accordingly.

## Discussion

### Which are the basic programming constructs used in Arduino?

Every Arduino sketch must have two basic constructs:

    + `setup()`: This is called once when the program starts, that is, when the board is powered up or is reset. This is the place to initialize interfaces, such as GPIO pin modes or baud rate for serial communication.
    + `loop()`: This is called repeatedly and is the place for the main code. This is equivalent to `while (1) {…}` C code.For digital IO, we can use `digitalRead()` and `digitalWrite()`. Since a GPIO pin can be either input or output, it should first be setup using `pinMode()`. For analogue IO, we can use `analogRead()` and `analogWrite()`.

Like in C/C++, control structures in Arduino include if-else, switch-case, for loops, do-while, break and continue.

Data types common in C language are available but there's also `String` class for easier manipulation of strings. `Serial` and `Stream` are also useful classes.

There are built-in math functions, timing functions, random number generation, bit-level operation, and interrupt processing. All of these are documented in the Arduino Reference.

### What are the main features of the Arduino IDE?

Arduino programs are called *sketches*. Arduino IDE is more than a code editor for a sketch. It does syntax highlighting on the code. It includes a number of simple examples to learn programming. Examples are accessible via File→Examples menu. From the IDE, we can verify the sketch and upload the binary to the target hardware. Any errors in this process are shown to the user.

The IDE can target all hardware variants. This selection is done via Tools→Board menu. What's more, we can use the *Boards Manager* to install newer boards or update to newer versions of board libraries. Likewise, developers have easy access to hundreds of third-party libraries or install custom libraries from a Zip file. This is done via menu Sketch→Include-Library.

To monitor serial communications, there's Serial Monitor and Serial Plotter. If IoT data is being sent, Serial Plotter's visualization makes it easy to see what's happening.

We also mention that there are alternatives to the Arduino IDE for programming Arduino boards. Some names are PlatformIO, Eclipse Arduino and Atmel Studio.

### How can I make the best use of limited memory resources on the Arduino?

Arduino Uno has only 2KB of SRAM. This means that not a lot of variables can be held in memory, particularly long strings. If SRAM is required for processing data, try offloading the processing to the cloud or another computer/device. If the range of your data can fit within a byte, use `byte` data type rather than two bytes of `int`. Use `String.reserve()` to avoid memory fragmentation. Prefer local to global variables.

Using `PROGMEM` keyword as part of data declaration, we can tell the compiler to store data in Flash. Using the macro `pgm_read_byte_near()`, we can read a byte from Flash. Similar macros exist for other data types: word, dword, float and ptr. If a string is defined inline, then the `F()` is useful to store it in Flash.

Let's note that the `const` keyword has read-only semantics, particularly useful for passing function arguments. It's not meant to tell the compiler where to store the data.

### What exactly is Software Serial and why do we need it?

Serial communications can be controlled using `Serial` class. This relies on the underlying UART hardware that's part of the microcontroller. Arduino Uno has one serial interface on pins 0 and 1. Due and Mega have four serial interfaces. What if we want another serial interface on the Uno? This is where Software Serial becomes useful.

Serial communications can be implemented on any pair of digital pins using software. We don't have to write low-level code to do this since third-party libraries are available. SoftwareSerial is available in Arduino IDE by default. The main disadvantage is that if multiple software serial interfaces are used, only one can receive data at a time. Speeds are also limited compared to UART. AltSoftSerial is an alternative but it has other limitations.

Either UART or software, serial interface operates at either 5V or 3.3V depending on the Arduino board. Don't connect them directly to RS232 serial port that operates at +-12V. Use a USB-to-serial adaptor in between.

### How do I program the Arduino for analogue input?

For analogue input, there's `analogRead()` function. Arduino Uno uses a 10-bit *Analogue-to-Digital Converter (ADC)*. Since Uno runs on 5V, this implies that the ADC resolution is 5V/1024 = 4.88mV. It's possible to improve the resolution by sacrificing range. For example, calling `analogReference(INTERNAL)` sets the ADC reference to 1.1V and improves resolution to 1.1V/1024 = 1.07mV. Uno WiFi Rev2 board has a default reference of 0.55V.

Due, Zero and MKR Family boards use 3.3V reference and 12-bit ADC. Thus, their resolution is at 0.806mV. However, they default to 10-bit ADC and this can be changed using `analogReadResolution()`. It's interesting that this function can be used to specify a lower resolution (least significant bits are discarded) or a higher resolution (extra bits are zero-padded).

An external reference via the AREF pin can also be used and selected using `analogReference(EXTERNAL)`. It's important to call this before calling `analogRead()`. The input to AREF must also respect the allowed voltage range.

### How do I program the Arduino for analogue output?

On most Arduino boards, analogue output is not actually a continuous signal but rather a stream of digital pulses called *Pulse Width Modulation (PWM)*. MKR and Zero boards are capable of true analogue on pin DAC0. Due can do this on pins DAC0 and DAC1.

For both PWM and true analogue, the relevant API to call is `analogWrite()`. Arduino Uno has PWM on pins 3, 10 and 11 at 490Hz and pins 5 and 6 at 980Hz. On Due, twelve pins can do PWM at 1000Hz.

PWM is generated from timers. For example, ATmega328P used in the Uno has three timers. These can be used in one of two modes: Fast PWM or Phase-Correct PWM. However, these are low-level details not exposed via `analogWrite()`.

Called **Software PWM**, it's possible to generate PWM waveforms on digital pins. This approach is called *bit banging*. With this, we can also control the frequency of the pulses. The problem with Software PWM is that interrupts will affect timing, resulting in jitter. Another problem is that processor is dedicated to generating PWM and can't be doing anything else.

### How do I program the Arduino for I2C and SPI interfaces?

Both I2C and SPI are synchronous protocols that rely on a clock line. In both cases, libraries are available: `Wire` for I2C and `SPI` for SPI. In addition, other specialized libraries might wrap these to further simplify programming. An example of this is Adafruit TMP007 library for the I2C-based infrared thermopile sensor. These libraries abstract away the actual pins used by these interfaces.

I2C is also called Two-Wire-Interface (TWI). For I2C, the Wire library uses 7-bit slave addressing and 32 byte buffer. Pullup resistors are required for lines SDA and SCL. Uno has one I2C interface while Due has two.

With SPI interfacing, microcontroller is typically the master. We need to know some things about the slave: bit order, mode, and maximum clock speed. These must be configured using `SPI.beginTransaction(SPISettings(…))`. On Uno, pins 10-13 make up the SPI interface. MOSI, MISO and SCK are also available on the ICSP header.

### How can I manage power consumption of my Arduino application?

Managing power consumption is critical for battery-powered applications. When there's no activity, Arduino must go into sleep to save energy. The built-in function `delay()` gives some savings but more can be obtained by using the LowPower library. The problem with both approaches is that the system can't respond to interrupts while asleep. Sleep time is also limited to maximum 8 seconds.

A better approach is to use the built-in `sleep_cpu()` along with interrupts. On Uno, hardware interrupts are available on pins 2 and 3. Depending on the application, interrupts can come from a sensor or an RTC module.

You can also slow down the clock to as low as 62.5kHz by setting the CLKPR register. An external crystal can be used for other clock values. This will need changes to bootloader and optionally to the Arduino IDE. **Optiboot** can be used to create a custom bootloader.

Other hardware changes can reduce power. Replace the onboard voltage regulator with a more efficient DC-DC buck converter. Better still, make a custom board just right for your application.

### Could you name some popular third-party libraries for the Arduino?

Registered Arduino libraries are listed online and also available from within the IDE. Libraries are organized by category, license and processor architecture. In June 2019, among the most starred or forked libraries were ArduinoJson, WiFiManager, FastLED, Blynk, IRremote, PubSubClient, Adafruit NeoPXL8, Adafruit NeoPixel, and MFRC522.

During Jan-Mar 2019, these libraries recorded most downloads: Adafruit Circuit Playground, DHT sensor library, ArduinoJson, Servo, SD, Adafruit GFX Library, Adafuit NeoPixel, LiquidCrystal I2C, MFRC522, and Blynk.

Among the top contributors are Adafruit Industries, SparkFun Electronics, Seeed Studio, Arduino Libraries, and STM32duino.

A curated list of libraries is available from Lembed. Another list appears on Arduino Playground site. Read a tutorial to create your own library.

## Milestones

2003

Hernando Barragán creates **Wiring** platform so that designers and artists can approach electronics and programming more easily. Wiring abstracts away the hardware pins with API calls such as `pinMode`, `digitalRead`, `analogWrite`, `delay`, etc. In fact, the syntax is defined before any hardware implementation.

Aug
2005

Arduino IDE and language library is released for the first time with the name **Arduino 0001**. Main sketch is compiled as C code but from Arduino 0004 (April 2006) it's compiled as C++ code.

Nov
2011

**Arduino 1.0** is released. Sketches now use extension `*.ino` rather than `*.pde` used for Processing files. This version introduces the use of `F()` macro. This macro was invented about a year ago by Paul Stoffregen who invented the Teensy derivative of Arduino.

Oct
2012

With the release of **Arduino 1.5 Beta**, the IDE now supports both AVR 8-bit and ARM 32-bit. This is also the year when the first 32-bit Arduino is released with Arduino Due.

Mar
2019

There are more than 2,150 libraries registered with the Arduino Library Manager. These can be downloaded and installed directly from within the IDE. There are also over 7,000 libraries in the wild. Since v1.0.5, the latter can be installed from a Zip file. Meanwhile, **Arduino 1.8.9** is released with support for ARM64 such as NVIDIA Jetson and Raspberry Pi 3.

Arduino Programming


# Entity Linking

## Summary

Finding knowledge is one of the most popular tasks of Internet users. In most cases, query results are a mix of pages containing different entities that share the same name.

Entity linking is the process of connecting entity mentions in text to their knowledgebase counterparts. Prospective information extraction, retrieval, and knowledgebase population are some applications of entity linking. This job, however, is difficult due to entity ambiguity and name variants. Because of the large number of web applications that produce knowledgebase data, major entity linking research has been conducted.

In many retrieval systems, a user would simply enter an entity or concept name into the retrieval system, and search results would be clustered by various entities/concepts that share that name. Additional details in the indexed records is one way to implement such a framework.

## Discussion

### What are the main steps in the entity linking process?

Entity entity, in most cases, involves the three sub-tasks that are executed in this specific order:

    + **Information Extraction**: Extract information from unstructured data.
    + **Named Entity Recognition (NER)**: Individuals, places, organizations, and other real-world objects are examples of named entities. NER recognizes and classifies named entity occurrences in text into pre-defined categories. The role of assigning a tag to each word in a sentence is modeled as NER.
    + **Named Entity Linking (NEL)**: Each entity identified by NER will be assigned a unique identity by NEL. NEL then attempts to link each entity to its description in a knowledgebase. The knowledgebase to be used depends on the program, but we may use Wikipedia-derived knowledgebases for open-domain text, such as Wikidata, DBpedia, or YAGO. **Wikification** refers to the process of connecting entities to Wikipedia.Entity linking can either be end-to-end involving both recognition and disambiguation. If gold standard named entities as available at the input, entity linking does only disambiguation.

### What are the main issues with entity linking?

There are two main issues with entity linking:

    + A phrase or word can be represented with multiple entities from the knowledgebase. For example, the entity *Japan* could mean Japan (national football team), Japan (country), Japan (Band), etc.
    + A single entity from the knowledgebase can have multiple names that represent entities that belong to distinct concepts. Take the example of *bass* as shown in the figure. The image on the left represents bass in context of sound and music. The image on the right represents bass as a fish.The challenge for entity linking is to figure out the correct context to resolve these ambiguities and link each entity to the most suitable entries in the knowledgebase.

### Which are the main entity recognition paradigms?

For Named Entity Recognition and Classification (NERC), we have the following machine learning approaches:

    + **Supervised Learning**: Relies on distinctive features that separate positive and negative examples. From earlier handcrafted rules, supervised learning has evolved to automatically inferred rule-based systems or sequence labeling algorithms from a set of training examples. Currently a popular approach and has many variants: Hidden Markov Models (HMM), Decision Trees, Maximum Entropy Models, Support Vector Machines (SVM), and Conditional Random Fields (CRF). To solve entity ambiguity, Milne and Witten employed used Wikipedia entities as training data. Other approaches also collected training data based on unambiguous synonyms.
    + **Semi-Supervised Learning**: Also called "weakly supervised", the most common approach is called *bootstrapping*. The learning process begins with a small amount of supervision, such as a collection of seeds. From these few samples, the model learns contextual clues that are then applied to the rest of the data in the next iteration. With many iterations, the model see more and more examples to learn from. Some semi-supervised approaches are known to rival baseline supervised approaches.
    + **Unsupervised Learning**: Not very common but one possible approach is to semantic relations present in the data.

### How can we use knowledge graphs for the entity linking task?

Modern entity linking systems use broad knowledge graphs built from knowledgebases like Wikipedia instead of textual features generated from input documents or text corpora. These systems extract complex features that take advantage of the information graph topology or exploit multi-step relations between entities that would otherwise go undetected by simple text analysis. Furthermore, developing multilingual entity linking systems based on natural language processing (NLP) is inherently difficult, as it necessitates either broad text corpora, which are often lacking for many languages, or hand-crafted grammar rules, which vary greatly between languages.

In the previous work of Han et al. proposed a graph-based collective entity linking method to model global topical interdependence (rather than pairwise interdependence) among different entity linking decisions in a single document. They first proposed Referent Graph, a graph-based representation that could model both textual context similarity and global topical interdependence between entity linking decisions as its graph structure. Then, using a strictly collective inference algorithm over the Referent Graph, they were able to jointly infer mapping entities for all entity mentions in the same paper.

### What are the main steps in implementing an entity linking system?

An entity linking system will need the following:

    + **Recognize**: Recognize the entities that are mentioned in the context of text. In this module, for each entity mention m ∈ M, the entity linking system aims to filter out irrelevant entities in the knowledgebase and retrieve a candidate entity set Em which contains possible entities that entity mention m may refer to.
    + **Rank**: Rank each candidate. In most cases, the size of the candidate entity set Em is larger than one. Researchers leverage different kinds of evidence to rank the candidate entities in Em. They try to find the entity e ∈ Em which is the most likely link for mention m.
    + **Link**: Link the recognized entities to the categorized entities in the knowledge graph.New facts are created and digitally expressed on the web as the world evolves. For semantic web and knowledge management strategies, automatically populating and enriching existing knowledgebases with newly derived facts has become a key problem. Entity linking is inherently regarded as a critical subtask in the population of a knowledgebase. Entity linking help a knowledgebase to grow.

### What datasets are available for entity linking?

**YAGO** is a high-coverage, high-quality open-domain information base that combines Wikipedia and WordNet. It's similar to Wikipedia by size but uses WordNet's clean taxonomy of concepts. YAGO currently contains over 10 million entities (such as individuals, organizations, places, and so on) and 120 million information about these entities, including the Is-A hierarchy (such as form and subclass of relations) as well as non-taxonomic relations. YAGO includes means-relation the relates strings to entities. For example, "Harry" denotes Harry Potter. Hoffart et al. used YAGO relations to create candidate entities.

**DBpedia** is a multilingual knowledgebase built by extracting structured data from Wikipedia, such as categorization details, geo-coordinates, and links to external web pages. English DBpedia contains 4 million entities. Furthermore, it adapts to Wikipedia's changes automatically.

**Freebase** is a broad online knowledgebase that's primarily generated collaboratively by its users. Non-programmers can edit the structured data in Freebase using a user interface. Freebase is a database that compiles information from a variety of sites, including Wikipedia. There are currently over 43 million entities and 2.4 billion facts about them in the database.

### What evaluation metrics are suited for entity linking?

Where only disambiguation is done, we have:

    + **Micro-Precision**: Fraction of correctly disambiguated named entities in the full corpus.
    + **Macro-Precision**: Fraction of correctly disambiguated named entities, averaged by document.Where both entity recognition and disambiguation are done, we have:

    + **Gerbil Micro-F1 – Strong Matching**: InKB micro F1 score for correctly linked and disambiguated mentions in the full corpus as computed using the Gerbil platform. InKB means only mentions with valid KB entities are used for evaluation.
    + **Gerbil Macro-F1 – Strong Matching**: InKB macro F1 score for correctly linked and disambiguated mentions in the full corpus as computed using the Gerbil platform. InKB means only mentions with valid KB entities are used for evaluation.

### What's the current state-of-the-art (SOTA) in entity linking?

Mulang et al. 2020 is the current Sota for ConLL-AIDA dataset.

Raiman is the current SOTA in cross-lingual entity linking for WikiDisamb30 and TAC KBP 2010 datasets. They construct a type system, and use it to constrain the outputs of a neural network to respect the symbolic structure. They achieve this by reformulating the design problem into a mixed integer problem: create a type system and subsequently train a neural network with it. They propose a 2-step algorithm: 1) heuristic search or stochastic optimization over discrete variables that define a type system informed by an Oracle and a Learnability heuristic, 2) gradient descent to fit classifier parameters.

They apply DeepType to the problem of entity linking on three standard datasets (WikiDisamb30, CoNLL (YAGO), TAC KBP 2010) and find that it outperforms all existing solutions by a wide margin, including approaches that rely on a human-designed type system or recent deep learning-based entity embeddings. Explicitly using symbolic information lets it integrate new entities without retraining.

## Milestones

2006

Bunescu and Paşca propose using Wikipedia for Named Entity Disambiguation (NED). Their model makes use of Wikipedia's redirect pages, disambiguation pages, categories and hyperlinks. They apply some rules to construct a dictionary of named entities from Wikipedia. For context-article similarity they use cosine similarity with vectors formed from TF-IDF of words in the vocabulary. As similar work is due to Cucerzan (2007).

Nov
2007

Mihalcea and Csomai propose **Wikify!**, a system that recognizes key phrases or concepts and link them to suitable Wikipedia pages. The two main tasks in the process are keyword extraction and word sense disambiguation (WSD). They adopt unsupervised keyword extraction that involves candidate extraction and ranking. For WSD, they evaluate a knowledge-based approach (inspired by Lesk algorithm) and a data-driven approach (using Naive Bayes classifier).

2008

Given two phrases, their **semantic relatedness** is usually computed using external knowledge sources. Instead, Milne and Witten propose using both incoming and outgoing links in Wikipedia pages to measure semantic relatedness. This is useful for WSD and hence for entity linking as well. To determine relatedness, the authors compare each potential candidate to the document's surrounding background, which is created by the other candidates. The use of Wikipedia for semantic relatedness was previously studied by Strube and Ponzetto (2006) and Gabrilovich and Markovitch (2007).

2011

Ratinov et al. propose the **GLOW** system, which is an approximation of joint disambiguation using mutual information for semantic relatedness. The researchers extract additional named entity mentions and noun phrases from the document that were previously used as link anchor texts in Wikipedia to emphasize the coherence among candidates. Candidates are then retrieved by querying an anchor-title index that maps each link goal in Wikipedia to its various link anchor texts and vice versa, augmenting the given query mentions with this collection.

2012

Shen et al. present **LINDEN**, a framework that uses YAGO to connect named entity mentions. They consider coherence among potential candidate entities. They consider semantic similarity of candidates to the forms in the YAGO ontology. This function assumes that candidate senses are organized into a tree structure of categories. They also consider global coherence of candidates for document mentions, where one candidate's global coherence is equal to the average Semantic Role Labelling (SRL) of all candidates.

2016

Tsai and Roth consider the problem of linking entity mentions in non-English language text to the English Wikipedia. They address this using **multilingual embeddings** of titles and words. Their system doesn't handle the case of an English Wikipedia entry that doesn't have an equivalent entry in a foreign language. They also release a Wikipedia dataset across 12 languages.

Entity Linking


# Non-Destructive Testing

## Summary

**Non-Destructive Testing (NDT)** is used in various industries to evaluate the properties of a material, component or system without damaging the test subject. NDT is also known by the terms *Non-Destructive Examination (NDE)*, *Non-Destructive Inspection (NDI)*, and *Non-Destructive Evaluation (NDE)*.

In testing, troubleshooting, and research, NDT is a successful strategy that saves both time and money. NDT plays a crucial role in everyday life and is essential for safety and reliability. Aircraft, spacecraft, motor vehicles, pipelines, bridges, railways, power plants, and oil platforms are just some examples tested with NDT.

NDT has many techniques. Eddy-current, magnetic-particle, liquid penetrant, radiographic, ultrasonic, leak, acoustic emission, and visual testing are most commonly used.

NDT is frequently used in forensics, mechanical engineering, petroleum engineering, electrical engineering, civil engineering, systems engineering, aeronautics, medicine, and art.

## Discussion

### What's the difference between destructive and non-destructive testing?

**Destructive testing** is simply testing the destroys or damages the subject under test. There are many material properties that can only be evaluated by applying physical force or load. Examples include tensile strength, elongation, and hardness. Destructive tests to evaluate these include tensile test, bending test, fracture test, flattening test, hardness test, shear test, and impact test.

On the other hand, NDT doesn't damage the test subject. This means that the sample can be used in the field provided the tests pass. NDT can be used to detect defects or discontinuities. It can detect degradation of properties after long use. Surface or internal cracking, poor welding, impact damages, delamination, density, porosity and pitting some problems that NDT can detect.

While destructive testing is often more reliable, NDT offers a safer, faster, cost-effective and less wasteful alternative. Thus, NDT complements but doesn't replace destructive testing.

### Why are NDT checks necessary for a material?

All materials, products, and equipment have standard design requirements and estimated life. Sometimes products get through production, fabrication, or delivery with undetected defects. These may cause catastrophic failures. Such catastrophes can be costly and can even terminate projects. NDT can catch these problems before catastrophes occur. NDT saves lives and property. It helps companies adhere to regulations and standards.

In addition to security, NDT is used to assure the effectiveness and longevity of equipment. It's useful for asset integrity management, which leads to increased productivity and profitability for businesses. For example, in the 2020 port blast in Beirut, many buildings within the blast radius are still standing but could be structurally unsafe. Rather than demolish them, NDT is being used to assess extent of damage and repair them where possible.

NDT can help engineers choose the right materials or treatments for each application. NDT can validate product design and suggest enhancements. It also validates manufacturing processes.

In conclusion, NDT brings safety, regulatory compliance, failure prevention, quality assurance, cost efficiency and reliability.

### Which industries are using NDT checks?

Several industries are using NDT:

    + **Aerospace & Defence**: Airframe structures are tested for wear and tear, and operation under extreme conditions. Ultrasonic method is widely used to reveal even the smallest defects. Users include Boeing, Airbus, GE Aviation, Hindustan Aeronautics Ltd., etc.
    + **Oil & Gas**: Internal structures are inspected for welds, cracks, voids and other structural defects. Ultrasonic and radiographic methods are commonly used. Users include Indian Oil Corporation, Bharat Petroleum, Reliance Petroleum Limited, ONGC, etc.
    + **Biomedical & Medical Devices**: Ultrasounds and x-rays are widely used. Vendors include Medtronic plc, Johnson and Johnson, Abbott Laboratories, etc.
    + **Civil & Heavy Construction**: Bigger structures imply bigger stresses and a greater need for NDT. NDT is applied to buildings, bridges and dams. Users include L&T Engineering & Construction Division, Tata Projects Ltd, etc.
    + **Metals & Mining**: Metals are the foundations on which many industries function. NDT validates material properties and quality. Users include BHP Group Ltd, Jiangxi Copper Co. Ltd, etc.Other industries using NDT include power generation, petrochemicals, automotive, and maritime.

### How to select a suitable NDT method for a certain material?

The selection of NDT method is based on material and defect type:

    + **Visual Testing**: Used for analyzing surfaces, examining the condition of mating surfaces, and checking for leaks.
    + **Liquid Penetrant Testing**: Detects surface-breaking defects such as hairline cracks, surface porosity, leaks in new products, and fatigue cracks.
    + **Magnetic Particle Inspection**: Mostly identifies surface and near-surface faults or cracks in ferromagnetic materials. It detects seams, porosity, and small tight cracks.
    + **Eddy Current Testing**: Uses electromagnetic induction to find defects in conductive materials. Detects electrical conductivity, permeability, cracks, seams, alloy content, heat treatment variations, wall or coating thickness.
    + **Ultrasonic Testing**: Uses ultrasonic waves in the range 0.1-50 MHz to pick up cracks and variations in thickness. Applicable for concrete, wood, composites, metals, alloys, and welds.
    + **Radiographic Testing**: Electromagnetic radiation penetrates materials and exposes defects on radiation-sensitive film. It mainly uses X-rays and Gamma-rays. It detects corrosion, geometry variation, density changes, and misaligned parts. It's a good test for inspecting weld interiors and picks out cracks, porosity, inclusions, voids, and lack of fusion.

### What are the best practices or precautions for NDT?

NDT itself poses a risk to testing professionals who are therefore required to follow many safety precautions. Always use trained and experienced professionals. They must know what testing method to use, how to use it, and how to correctly interpret the results. Suitable NDT software can help overcome examiner fatigue and loss of concentration.

Where ultraviolet radiation, ionizing radiation, or X-rays are used, operators should wear personal protective equipment, and use suitable filters and lenses. Even when more benign techniques such as ultrasonic or eddy current testing are employed, the testing environment itself can pose a risk. For example, changing probes without shutting down the system can cause sparks and explosions. Testing environments must be clean and free of clutter. Compressed gases (sulphur hexafluoride, acetylene, and nitrous oxide) commonly found at NDT facilities must be handled properly.

For magnetic particle testing, operator should use a local exhaust or at least wear respiratory protective equipment. For penetrant inspection, avoid skin contact, and keep away from food, drinks and smoking materials. For radiography, magneto-inductive and eddy current testing, cordon off the area so that personnel don't unintentionally expose themselves to radiation.

### What are some real-world catastrophes that could've been prevented with NDT?

In 2009, one of the pressure vessels at NDK Crystal in Belvidere, Illinois violently ruptured. Later investigations confirmed that the company had failed to do NDT on the inside of the vessel that had been damaged due to corrosive chemicals. The inside wall had experienced stress corrosive cracking.

The Columbia Space Shuttle disaster of 2003 killed seven astronauts onboard. It was caused by a falling piece of foam that then damaged the left wing. Following an investigation, it was deemed that more extensive NDT on the foam and wings could have prevented this disaster. Another recommendation was to develop new NDT techniques to complement destructive testing methods.

During the 1940s, the U.S. mass produced 2710 Liberty ships, some completed within five days. But 12 ships broke in half, later attributed to tiny fractures in the steel. Modern NDT techniques could have ascertained the quality of the steel.

### What certifications are available for NDT professionals?

**ISO 9712:2021** is the main standard for third-party qualification and certification of NDT personnel. Methods included in its scope are acoustic emission, eddy current, leak, magnetic, penetrant, radiographic, strain gauge, thermographic, ultrasonic and visual. It's an evolution of two earlier standards: EN 473 and ISO 9712.  ISO 9712 has been adopted in the U.S. as ANSI/ASNT CP-106.

**EN ISO 20807** establishes a system for the qualification of personnel who perform NDT applications of a limited, repetitive or automated nature. **ISO TS 11774** is a performance-based qualification. It's applicable for safety critical applications where even third-party certification (ISO 9712:2021) may not suffice.

For employer-based certification, we have **ANSI/ASNT CP-189**. Particular to aerospace, we have AIA NAS 410:2014 and EN 4179:2017.

Certification under these standards includes training, work experience under supervision, and passing a written and practical examination conducted by the independent certification authority.

For a complete list of all ISO NDT standards covering requirements on testing equipment, visit ISO's 19.100: Non-destructive testing.

## Milestones

1868

Englishman S.H. Saxby first recorded NDT in his journal, "Engineering," a method of detecting cracks in gun barrels using magnetic inductions.

1895

After the discovery of radiography Wilhelm Conrad Röntgen described the properties of radiography a type of electromagnetic radiation.

1912

The Englishman Richardson claimed the identification of icebergs by ultrasound in his patent after titanic sink.
 Until 1912, the discovery of X-rays was mostly used in medical and dentistry fields.

1920

William Hoke discovered the magnetic particles can be used to locate the defects using magnetism.

1929

Russian Sokolov studied the use of ultrasonic waves in detecting metal objects. Victor de Forest and Foster Doane used the ultrasonic waves in real industrial applications.

1930

Richard Seifert developed X-ray technology so that it is more powerful and precise than before.

1932

Giraudi an Italian built a magnetic particle crack detector named as "Metalloscopio."

1940

Firestone developed pulsed ultrasonic testing using a pulse echo testing, and fluorescent or visible dye is added to the oil used to penetrate test objects.

1950

The Schmidt Hammer (also known as "Swiss Hammer") is invented. The instrument uses the world's first patented non-destructive testing method for concrete. Also, J. Kaiser introduces acoustic emission as an NDT method.

2008

NDT in Aerospace Conference was established DGZfP and Fraunhofer IIS hosted the first international congress in Bavaria, Germany.

2020

Indian Society for Non-destructive Testing (ISNT) Accreditation Certification from NABCB for Qualification and Certification of NDT Personnel as per ISO 9712:2012.

Non-Destructive Testing


# CSS Specificity

## Summary

Assume an element on a web document is targeted by two different CSS selectors. Each selector applies different styling to that element. The selector with the higher *specificity* will take effect. CSS defines clear rules to determine the specificity of CSS selectors.

A good understanding of the rules of CSS specificity can enable developers write better selectors and troubleshoot problems in their stylesheets. Otherwise, developers may end up misusing `!important` in their CSS declarations, thus making code more difficult to maintain in the long run.

## Discussion

### What are the rules to determine CSS specificity?

A CSS selector will typically use ID, class, element/tag name or a combination of these. In addition, some selectors will use element attributes, pseudo-classes and pseudo-elements. Specificity is computed by counting each of these and ranking them. IDs have more importance than classes, pseudo-classes and attributes; the latter have more importance than elements and pseudo-elements. Inline style, specified as attribute `style="…"`, has highest importance. The counts are concatenated to arrive at the final specificity. A selector with a higher number to the left implies higher specificity.

If two selectors have the same specificity, one that appears later in the stylesheet is applied. Universal selector `*` and combinators (`~`, `>`, `+`) are ignored. Pseudo-class `:not()` is not counted but selectors inside it are counted.

Although specificity is calculated for selectors, styles are applied based on individual property-value declarations. Regardless of specificity, declarations with `!important` have highest importance. If conflicting declarations contain `!important`, higher specificity wins.

If a selector has no declaration for a specific property, inherited value (if available) or initial value is applied.

### Could you explain CSS specificity with an example?

Given an ID, three classes and two elements, specificity is 0-1-3-0. This has higher specificity than a selector with just five classes, where specificity is 0-0-5-0.

Consider HTML content `<p class="foo" id="bar">Lorem ipsum.</p>`. Consider CSS rules `p { color: red; }`, `.foo { color: green; }` and `#bar { color: blue; }`. All three selectors target the paragraph but the last selector has highest specificity. Hence, we'll see blue-coloured text. This can be understood by calculating the specificity:

    + `p`: one element: 0-0-0-1
    + `.foo`: one class: 0-0-1-0
    + `#bar`: one ID: 0-1-0-0Since 0-1-0-0 > 0-0-1-0 > 0-0-0-0, `#bar` selector has the highest specificity.

If we have `p { color: red !important; }`, we'll have red-coloured text. Specificity is ignored.

Suppose we introduce inline styling, `<p class="foo" id="bar" style="color: purple">…</p>`. This will take precedence, unless there's an earlier declaration with `!important`.

Suppose we have two classes `<p class="foo hoo">…</p>` styled with `.hoo { color: yellow; }`. Specificity is the same for both `.foo` and `.hoo`. If `.hoo` appears later in the stylesheet, we'll have yellow-coloured text. When specificity is same, order matters.

### How is specificity affected by cascading order?

Consider HTML content `<p class="foo" id="bar">Lorem ipsum.</p>`. Suppose the author defines `.foo { color: green; }` and the user defines `#bar { color: blue; }`. User-defined styles are typical for accessibility reasons. The latter has higher specificity but the former declaration is used; that is, text is rendered in green. To understand this, we need to understand the concept of **origin**.

CSS styles can come from different origins: user (reader of the document), user agent (browser), or author (web developer). The standard defines precedence of the origin. This is applied first before specificity is considered. The order also considers declarations that include `!important`.

Precedence in descending order is Transition declarations, Important user agent declarations, Important user declarations, Important author declarations, Animation declarations, Normal author declarations, Normal user declarations, and Normal user agent declarations.

### What are some examples of CSS specificity calculation?

Here we share a few examples:

    + `ul#nav li.active a`: `#nav` is the ID, `.active` is the class, and three elements `ul`, `li` and `a` are used. Specificity 0-1-1-3.
    + `body.ie7 .col_3 h2 ~ h2`: Two classes `ie7` and `.col_3`, and three elements `body`, `h2` and `h2` are used. `~` is not counted. Specificity 0-0-2-3.
    + `<li style="color: red;">`: Has inline style attribute. Specificity 1-0-0-0.
    + `ul > li ul li ol li:first-letter`: Apart from six elements, `:first-letter` is a pseudo-element. `>` is not counted. Specificity 0-0-0-7.
    + `li:nth-child(2):hover`: Both `:nth-child(2)` and `:hover` are pseudo-classes. Specificity 0-0-2-1.
    + `.bar1.bar2.bar3.bar4`: This is a combined selector with four classes. Specificity 0-0-4-0.

### What are some tips for managing CSS specificity?

Use IDs to increase specificity. For example, `a#foo` has higher specificity compared to `a[id="foo"]`: 0-1-0-1 > 0-0-1-1.

When two selectors have same specificity, the selector defined second takes precedence. The use of `!important` overrides specificity. If two declarations have `!important`, the second one wins. In any case, avoid the use of `!important`.

For link styling, define CSS rules in the order of Link, Visited, Hover, Active (LVHA).

In terms of online resources, developers can consult handy cheat sheets on CSS specificity at Stuff & Nonsense or at Standardista. There's also an online calculator.

### I find CSS specificity confusing. Is there a simpler way?

One approach is to adopt a naming convention such as **Block-Element-Modifier (BEM)**. By defining CSS classes for design components (called blocks), scope of a class is "limited" to that block.

In BEM, selectors use only classes. IDs and elements are avoided in selectors. Combined selectors (of the form `.foo.bar`) are avoided. Nested selectors (of the form `.foo .bar`) are allowed but discouraged in BEM. Since selectors are just classes, it's easier to determine specificity. Selectors can be reused more easily.

**CSS Modules** offers a modern way to restrict the scope of CSS declarations. Via tooling, this automatically renames the selectors. This can be configured to adopt BEM naming approach. For example, a menu component is styled with `.Menu_item--3FNtb`. If it appears within a header, the style changes to `.Header_item--1NKCj`. Although both have same specificity, the latter occurs later in the stylesheet.

One alternative to BEM is Enduring CSS (ECSS). Partly inspired by BEM, ECSS promotes the use of single class selectors. IDs and tag names are not used in selectors. Nested selectors are allowed.

Other alternatives include Atomic CSS (ACSS), Object-Oriented CSS (OOCSS), Scalable and Modular Architecture for CSS (SMACSS).

### Is CSS specificity still relevant in modern JavaScript frameworks?

Among the well-known JavaScript frameworks are Angular, React and Vue. All of these offer ways to style documents in a modular fashion. This is in contrast to the default global scope of CSS selectors and declarations. In Angular and React, a **styled component** would have declarations that are applicable only to that component. In Vue, **scoped CSS** achieves the same effect.

This idea of combining JS and CSS has been formalized under the term **CSS-in-JS**. However, one disadvantage is that we're combining HTML structure and styling into a component file. Although this isolates one component from another, it also makes styles harder to reuse across components.

Although CSS declarations are restricted to their components, specificity can't be ignored. Specificity still applies within the component but a lot easier to manage.

## Milestones

Dec
1996

W3C publishes **CSS1** as a W3C Recommendation. This clarifies the precedence when conflicting rules target the same element. It also explains how to calculate the specificity of selectors.

2000

There's no exact date when developers recognize the importance of CSS specificity. Probably around 2000 (plus or minus a few years), as websites and stylesheets start to grow in complexity, developers have a hard time debugging and maintaining their code. It's at this point that they begin to learn and understand CSS specificity.

Aug
2002

W3C publishes the first draft of **CSS2.1**, which is a revision of CSS2 from 1997. This clarifies the **cascading order**. In descending order, it's user important, author important, author normal, user normal and user agent declarations. In CSS1, precedence in descending order was author, reader and user agent declarations.

Jan
2006

Developer Keegan Street open sources a JavaScript module that, given a selector, calculates and return its specificity. This is a command-line tool. For convenience, an online version of this calculator is available.

Jan
2013

In W3C Working Draft titled *CSS Cascading and Inheritance Level 3*, cascading order is updated. This document becomes a W3C Candidate Recommendation in April 2015.

CSS Specificity


# Naming Conventions

## Summary

In general, code is written once but read multiple times, by others in the project team or even those from other teams. Readability is therefore important. Readability is nothing more than figuring out what the code does in less time.

Among the many best practices of coding, is the way variables, functions, classes and even files are named in a project. A common naming convention that everyone agrees to follow must be accompanied by consistent usage. This will result in developers, reviewers and project managers communicate effectively with respect to what the code does.

While there are well-established naming conventions, there's no single one that fits all scenarios. Each programming language recommends its own convention. Each project or organization may define it's own convention.

## Discussion

### Why should we have a naming convention and what are its advantages?

Naming conventions are probably not important if the code is written by a single developer, who's also the sole maintainer. However, typical real-world projects are developed and maintained by teams of developers. Particularly in the context of open source projects, code is shared, updated and merged by many individuals across organizations and geographies. Naming conventions are therefore important.

Naming conventions result in improvements in terms of "four Cs": communication, code integration, consistency and clarity. The idea is that "code should explain itself". At code reviews, we can focus on important design decisions or program flow rather than argue about naming.

Naming conventions lead to predictability and discoverability. A common naming convention, coupled with a consistent project structure, makes it easier to find files in a project.

In short, naming convention is so important that Phil Karlton is said to have said,

> There are only two hard things in Computer Science: cache invalidation and naming things.

### What are some common naming conventions used in programming?

Among the common ones are the following:

    + **Camel Case**: First letter of every word is capitalized with no spaces or symbols between words. Examples: `UserAccount`, `FedEx`, `WordPerfect`. A variation common in programming is to start with a lower case: `iPad`, `eBay`, `fileName`, `userAccount`. Microsoft uses the term Camel Case to refer strictly to this variation.
    + **Pascal Case**: Popularized by Pascal programming language, this is a subset of Camel Case where the word starts with uppercase. Thus, `UserAccount` is in Pascal Case but not `userAccount`.
    + **Snake Case**: Words within phrases or compound words are separated with an underscore. Examples: `first_name`, `error_message`, `account_balance`.
    + **Kebab Case**: Like Snake Case, but using hyphens instead. Examples: `first-name`, `main-section`.
    + **Screaming Case**: This refers to names in uppercase. Examples: `TAXRATE`, `TAX_RATE`.
    + **Hungarian Notation**: Names start with a lowercase prefix to indicate intention. Rest of the name is in Pascal Case. It comes in two variants: (a) *Systems Hungarian*, where prefix indicates data type; (b) *Apps Hungarian*, where prefix indicates logical purpose. Examples: `strFirstName`, `arrUserNames` for Systems; `rwPosition`, `pchName` for Apps.

### What are the categories of naming conventions?

It's common to categorize a naming convention as one of these:

    + **Typographical**: This relates to the use of letter case and symbols such as underscore, dot and hyphen.
    + **Grammatical**: This relates to the semantics or the purpose. For example, classes should be nouns or noun phrases to identify the entity; methods and functions should be verbs or verb phrases to identify action performed; annotations can be any part of speech; interfaces should be adjectives.Grammatical conventions are less important for variable names or instance properties. They are more important for classes, interfaces and methods that are often exposed as APIs.

### What's the typical scope of a naming convention?

Naming convention is applicable to constants, variables, functions, modules, packages and files. In object-oriented languages, it's applicable to classes, objects, methods and instance variables.

With regard to scope, global names may have a different convention compared to local names; such as, Pascal Case for globals: `Optind` rather than `optind` in gawk. Private or protected attributes may be named differently: `_secret` or `__secret` rather than `secret`. Some may want to distinguish local variables from method arguments using prefixes. Python's PEP8 doesn't give a naming convention to tell apart class attributes from instance attributes but other languages or projects might define such a convention.

In general, use nouns for classes, verbs for functions, and names that show purpose for variables, attributes and arguments. Avoid (Systems) Hungarian notation in dynamically typed languages since data types can change. Use lower Camel Case for variables and methods. Use Pascal Case for classes and their constructors. Use Screaming Case for constants.

### What word separators are used in naming conventions and who's using what?

With Camel Case and Pascal Case, there are no word separators. Readability is achieved by capitalizing the first letter of each word. Languages adopting this include Pascal, Modula, Java and .NET.

With Snake Case, underscore is the separator. This practice is common in Python, Ruby, C/C++ standard libraries, and WordPress.

With Kebab Case, hyphen is the separator. Languages using this include COBOL, Lisp, Perl 6, and CSS. Since hyphen in many languages is used for subtraction, Kebab Case is less common than Snake Case.

It's common for a language to adopt different case styles for different contexts. For example, a PHP convention named PSR-1 adopts the following: `PascalCase` for class names, `UPPER_SNAKE_CASE` for class variables and `camelCase` for method names.

### Where can I find published naming conventions for different languages?

Python defines its naming convention as part of PEP8. Beginners can read a summary.

Rust's conventions are summarized in its official documentation. It uses Camel Case for type constructs and Snake Case for value constructs.

Google offers it's own naming convention for Java and for R.

Kotlin follows Java's conventions and they're summarized in its documentation.

.NET naming conventions are available online.

Resources in Android projects are defined in XML but without namespaces. This makes it difficult to find stuff. One suggested XML naming convention solves this problem.

For user interface design and names in CSS, BEM Methodology is worth studying.

### Are there exceptions to following a common naming convention?

Following a common naming convention is beneficial. However, it may be okay to relax the rules in some cases. When a name is highly localized, lacks business context or used within a few lines of code, it may be acceptable to use a short name (`fi` rather than `CustAcctFileInfo`).

When a project is written in multiple languages, it's not possible to have a single naming convention. For each language, adopt the naming convention prevalent in that language. Another example is when you're using a third-party library. Follow their convention for consistency and readability.

Ultimately, naming conventions should not be enforced blindly. We should be sensitive to the context of use. This is partly because IDEs come to the aid of developers. They can highlight the name in all places it occurs. The practice of prefixing member and static variables with `m` and `s` respectively is also not in favour since IDEs colour them differently.

### Could you give some tips to create a naming convention?

Without being exhaustive here are some things to consider:

    + Reveal intentions: `fileName` is better `f`; `maxPugs` is better than `pugs`.
    + Make distinctions: `moneyInDollars` is better than `money`.
    + Put the distinguishing aspect first: `dollarMoney` and `rupeeMoney` are better than `moneyInDollars` and `moneyInRupees`.
    + Easy to pronounce: `timeStamp` is better than `ts`.
    + Verbs for functions: `getName()` and `isPosted()` are good; `hasWeight()` or `isMale()` when boolean values are returned; `toDollars()` for conversions.
    + One word, one concept: `fetch`, `retrieve`, `get` all imply the same thing: use one of them consistently.
    + Relate to business context: `AddCustomer` is better than `IncrementCounter`.
    + Use shortforms judiciously: `PremiumCust` may be used over `PremiumCustomer` to emphasize "Premium"; but `fn` is not a good substitute for `fileName`.
    + Describe content rather than storage: `user_info` is better than `user_list`.
    + Plurals for containers: `fruitNames` is better than `fruit` for an array of fruit names.
    + Describe content rather than presentational aspects: in CSS, for example, `main-section` is better than `middle-left-and-then-a-little-lower` as identifier name.

## Milestones

1813

An early use of Camel Case, more formally called **medial capitals**, starts in chemistry. Swedish chemist Jacob Berzelius invents it to represent chemical formulae. For example, Sodium Chloride is easier to read as `NaCl` than `Nacl`.

1960

The use of **Snake Case** can be traced to the late 1960s. In the 1970s, it's used in C language, while Pascal uses what is later called **Pascal Case**. However, the name Snake Case itself is coined in the early 2000s.

1970

The 1970s is when **Camel Case** started becoming common in the world of computer programming. However, the name itself is coined years later (in 1995) and is attributed to Newton Love.

1980

In the 1980s, Charles Simonyi, a Hungarian working at Microsoft, invents the notation of using lowercase prefixes to names to indicate what the name referred to. Thus is born (Apps) **Hungarian Notation**. Unfortunately, some mistake Simonyi's idea and use prefixes to indicate data types. This is born Systems Hungarian. While Apps Hungarian is useful, Systems Hungarian is not, particularly in type safe languages. When Microsoft start working on .NET in the late 1990s, they recommend not using Hungarian notation.

Dec
2012

R language doesn't officially specify a naming convention. Therefore multiple conventions exist. An analysis of 2668 R packages downloaded from CRAN shows a lack of consistency. In fact, 28% of packages use three or more conventions.

Naming Conventions