You are on page 1of 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 youre at an interview, youll 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 outputsthis 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 inheritancecreating 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

Prototype-based OO languages implement this by cloning instances. Notably, Javascript is prototype-based (as are other ECMAScript-based languages like AS3). Additionally, classes and class hierarchies can be implemented within a prototype-based framework.

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


Call-by-reference and call-by-value are two evaluation strategies. Also sometimes known with the prefix pass-by. In languages that are call-by-value, argument expressions (the code nested in your function call arguments) are first completely evaluated, then the resulting value 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 dont quite fit into either paradigmRuby and Python might be considered call-by-object-reference. They always pass through object references. Even integers in Ruby are objects. Notably, C, Java, Javascript and Scheme are call-by-value. It might seem confusing that Java and Javascript are call-by-valueyou can pass objects through and mutate them! But, technically, Java and Javascript pass through the value of the object reference (but also support passing through direct values). Thats why you sometimes cast from int to Integer in Javayou might want an integer-based object reference to access some methods of the Integer class.

Dynamic vs. Static and -typed


Dynamic languages perform many operations during runtime that static languages tend to perform during compilation. Statically-typed languages must know the type of a variable at the time of compilation. 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, see https://www.udemy.com/programming-code-interview/?couponCode=CHEATSHEETERS

prior to runtime, so you can often declare variables in these languages without specifying a type (a = 5 instead of int a = 5). Statically-typed languages are sometimes preferred because you can often catch type mismatch errors at compile-time. So if you call a function with a string instead of an integer, youll know much faster (maybe even within your IDE). In a dynamically typed language, often it will be difficult for any system to detect an error until that piece of code get hitlike when a user visits your website! For this reason, I am a big advocate of the importance of writing tests for dynamically-typed code. Static type-checking rules out a pretty common class of bugs during developmenttype mismatches. Polymorphism, in general, refers to the ability to provide the same interface for different underlying implementations. For allowing different behavior based on parameter types, this is sometimes called parametric polymorphism. Polymorphism referring to the object-interface separation is known as generic programming.

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

Some Language Paradigms


Many programming languages are multi-paradigm. Ruby: imperative, object-oriented (class), call-by-object-reference, and functional Python: imperative, object-oriented (class), call-by-object-reference and functional

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

C++: imperative, object-oriented (class), call-by-value/reference1 (kind of has generics2 , functional3) 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 functional4 ) C: imperative, call-by-value

Conclusion
We learned about: 1. Imperative vs. functional programming 2. Implementing object orientation using classes vs. prototypes 3. 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 any applicable caveats. Now when your interviewer asks you about your language, you can start an interesting conversation about how your language is different from all the others! If you get really into this stuff, theres a great course and book online where you learn to build your own programming language. The author created the Thin web server, and a student from this course ended up writing the popular Javascript language CoffeeScript!

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

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

Resources
1. 2. 3. 4. 5. Lecture on Programming Paradigms from the University of Birmingham List of multi-paradigm languages from Wikipedia More on Javascripts event loop Is python call-by-value or call-by-reference? Neither C++ values vs. reference

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

You might also like