Clean Code 👨🏻‍💻, parameter 🫙 VS arguments 🍷, for (each VS of) 🌀, generics in programming 🚀

By Prajwal Haniya

Techletter #61 | February 24, 2024

The secret to clean code

You are not paid to write code. You are paid to solve problems. Half of the solution is understanding the problem. You can create the most elegantly crafted software on the planet, but if it doesn’t solve the business problem, it isn’t clean. Clean Code should solve the problem domain in the most efficient way possible**.** Software is all about making our customers happy.

Original Link | Archive Link


What is the difference between a Parameter and an Argument?

A parameter is a piece of data a function needs to run, declared as part of a function declaration. Also called a formal parameter.

An argument is a piece of data that you pass to a function when invoking it. Also called an actual parameter.


What is the difference between for each & for of loop in js?

forEach:

for…of:


Why do we need generics in TypeScript (Programming in general as well)?

Consider the below filter function.

function filter(array, f) {
    let result = []
    for (let i = 0; i < array.length; i++) {
        let item = array[i]
        if (f(item)) {
            result.push(item)
        }
    }
    return result
}

const result = filter([1, 2, 3, 4], _ => _ < 3);
console.log(result);

Now, if you take the type signature of the above function you have

type Filter = {
    (array: unknown, f: unknown) => unknown[];
}

If we are using unknown then what is the use of typescript?

The above filter function must be written in such a way that it can be used not only for numbers but also for strings, objects, other arrays, etc. Else it would be really difficult if you start writing filter functions for each type. This is the reason we have generics.

Now, let’s rewrite the type using generics for the above filter function

type Filter = {
    <T>(array: T[], f: (item: T) => boolean) : T[]
}

It’s awesome, right? At least when I was reading about this in the book “Programming Typescript”, I was filled with joy when I got to know this.

Explanation:

This function filter uses a generic type parameter T; we don’t know what this type will be ahead of time.

TypeScript infers T from the type we pass in for the array. Once TypeScript infers what T is for a given call to filter, it substitutes that type in for every T it sees. T is like a placeholder type, to be filled in by the type checker from context; it parameterizes the Filter’s type, which is why we call it a generic type parameter.

T is just a type name, and we could have used any other name instead: A, Zebra, or l33t. By convention, people use uppercase single-letter names starting with the letter T and continuing to U, V, W, and so on depending on how many generics they need.

If you’re declaring a lot of generics in a row or are using them in a complicated way, consider deviating from this convention and using more descriptive names like ValueorWidgetType instead.


Why should we hire you?

Answering this question in an interview is not as easy as we think. It changes for each individual, and you have to answer it based on your experience and skill. However, this medium article gives you a template which I found to be useful. It gives templates for both beginners and experienced individuals.

As this article couldn’t be archived, I am adding the templates below. You can check the original article with this link

How should Fresh Graduates Answer this Question?

How should Experienced Answer this Question?

Original Link

  1. Asynchronous JavaScript
  2. Message Passing in Chrome extension
  3. Request for startups
  4. 1.5+ million pdfs in 25 minutes