5
Programming Language Paradigms One of my favorite books on programming languages has been 7 Languages in 7 Weeks . You really get a sense of how diverse programming languages can be, and some feel powerful in completely different ways. When you’re at an interview, you’ll often get asked to compare and contrast a programming language you use with another language. Often times, this will kick off a brief discussion of programming language paradigms. Here are some paradigms to become familiar with: Imperative vs. Functional 1. Imperative programming: the language has statements that explicitly change the state of memory in the computer a. int a = 5; one example of an imperative statement 2. Functional programming: computation is done in such a way that avoids state and mutable data a. Functions depend only on inputs (can “exist in vacuum”) and leave behind only their returned outputs—this is known as having no side effects. Object Oriented: Class vs. Prototype-based In an object-oriented paradigm language, there are object concepts that have data fields and methods. These often implement inheritance—creating a hierarchy of objects with “is-a” relationships. For example, you might have a Cat object that is an Animal object. In this case, Cat would inherit from Animal. Class-based OO languages implement inheritance using a class hierarchy. This is a cheat sheet from the Coding for Interviews Udemy course . For more, see https://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS

Free Programming Language Paradigms Cheat Sheet

Embed Size (px)

DESCRIPTION

From Coding for Interviews: Learn the important distinctions between programming languages.More cheat sheets at the course: http://codingforinterviews.com/courseTopics covered:* Imperative vs. functional programming* Implementing object orientation using classes vs. prototypes* Passing arguments through to functions call-by-reference vs. call-by-value vs. “call-by-object-reference-value”* Concurrency vs. simply supporting callbacks

Citation preview

Programming Language Paradigms

One of my favorite books on programming languages has been 7 Languages in 7Weeks. You really get a sense of how diverse programming languages can be, andsome feel powerful in completely different ways.

When you’re at an interview, you’ll often get asked to compare and contrast aprogramming language you use with another language. Often times, this will kickoff a brief discussion of programming language paradigms.

Here are some paradigms to become familiar with:

Imperative vs. Functional

1. Imperative programming: the language has statements that explicitlychange the state of memory in the computer

a. int a = 5; ← one example of an imperative statement2. Functional programming: computation is done in such a way that avoids

state and mutable dataa. Functions depend only on inputs (can “exist in vacuum”) and leave

behind only their returned outputs—this is known as having no sideeffects.

Object Oriented: Class vs. Prototype-based

In an object-oriented paradigm language, there are object concepts that havedata fields and methods. These often implement inheritance—creating ahierarchy of objects with “is-a” relationships.

For example, you might have a Cat object that is an Animal object. In this case, Catwould inherit from Animal.

Class-based OO languages implement inheritance using a class hierarchy.

This is a cheat sheet from the Coding for Interviews Udemy course. For more, seehttps://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS

Prototype-based OO languages implement this by cloning instances.

Notably, Javascript is prototype-based (as are other ECMAScript-based languageslike AS3). Additionally, classes and class hierarchies can be implemented within aprototype-based framework.

Call-by-Reference vs. Call-by-Value

Call-by-reference and call-by-value are two evaluation strategies. Also sometimesknown with the prefix “pass-by”.

In languages that are call-by-value, argument expressions (the code nested inyour function call arguments) are first completely evaluated, then the resultingvalue is passed, often by copying, directly into the function being called.

In languages that evaluate by call-by-reference, functions receive references(pointers to memory locations) to variables you pass in.

Some languages don’t quite fit into either paradigm—Ruby and Python might beconsidered call-by-object-reference. They always pass through objectreferences. Even integers in Ruby are objects.

Notably, C, Java, Javascript and Scheme are call-by-value. It might seem confusingthat Java and Javascript are call-by-value—you can pass objects through andmutate them! But, technically, Java and Javascript pass through the value of theobject reference (but also support passing through direct values). That’s why yousometimes cast from int to Integer in Java—you might want an integer-basedobject reference to access some methods of the Integer class.

Dynamic vs. Static and “-typed”

Dynamic languages perform many operations during runtime that staticlanguages tend to perform during compilation.

Statically-typed languages must know the type of a variable at the time ofcompilation. Dynamically-typed languages do not require the type to be specified

This is a cheat sheet from the Coding for Interviews Udemy course. For more, seehttps://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS

prior to runtime, so you can often declare variables in these languages withoutspecifying a type (a = 5 instead of int a = 5).

Statically-typed languages are sometimes preferred because you can often catchtype mismatch errors at compile-time. So if you call a function with a string insteadof an integer, you’ll know much faster (maybe even within your IDE). In adynamically typed language, often it will be difficult for any system to detect anerror until that piece of code get hit—like when a user visits your website!

For this reason, I am a big advocate of the importance of writing tests fordynamically-typed code. Static type-checking rules out a pretty common class ofbugs during development—type mismatches.

Polymorphism, in general, refers to the ability to provide the same interface fordifferent underlying implementations. For allowing different behavior based onparameter types, this is sometimes called parametric polymorphism.Polymorphism referring to the object-interface separation is known as genericprogramming.

Concurrency

Concurrency is different from asynchronous callbacks. While Javascript uses callbacksall the time, they are implemented using an event loop. Concurrency, on the otherhand, is implemented by using multiple threads.

Some Language Paradigms

Many programming languages are multi-paradigm.

● Ruby: imperative, object-oriented (class), call-by-object-reference, andfunctional

● Python: imperative, object-oriented (class), call-by-object-reference andfunctional

This is a cheat sheet from the Coding for Interviews Udemy course. For more, seehttps://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS

● C++: imperative, object-oriented (class), call-by-value/reference (kind of has1

generics , functional )2 3

● Java: imperative, object-oriented (class), has generics, reflective,call-by-value

● Javascript: imperative, object-oriented (prototype), call-by-value, functional● C#: imperative, object-oriented (class), call-by-value/reference (kind of has

functional )4

● C: imperative, call-by-value

Conclusion

We learned about:

1. Imperative vs. functional programming2. Implementing object orientation using classes vs. prototypes3. Passing arguments through to functions call-by-reference vs. call-by-value

vs. “call-by-object-reference-value”4. Concurrency vs. simply supporting callbacks

Be sure to learn what paradigms your language of choice supports and anyapplicable caveats.

Now when your interviewer asks you about your language, you can start aninteresting conversation about how your language is different from all the others!

If you get really into this stuff, there’s a great course and book online where youlearn to build your own programming language. The author created the Thin webserver, and a student from this course ended up writing the popular Javascriptlanguage CoffeeScript!

1call-by-reference using &. e.g., void call_by_reference(int &a_reference)2 via templates3 C++ 11 adds some support for functional-style programming4 only support for lambdas

This is a cheat sheet from the Coding for Interviews Udemy course. For more, seehttps://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS

Resources

1. Lecture on Programming Paradigms from the University of Birmingham2. List of multi-paradigm languages from Wikipedia3. More on Javascript’s event loop4. Is python call-by-value or call-by-reference? Neither5. C++ values vs. reference

This is a cheat sheet from the Coding for Interviews Udemy course. For more, seehttps://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS