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.
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:
-
forEach
is a method available on arrays. -
It accepts a callback function as an argument and executes that function once for each element in the array.
-
The callback function passed to
forEach
accepts three arguments: the current element, the index of the current element, and the array being traversed (though this argument is optional and rarely used). -
forEach
does not return a new array or value; it is primarily used for its side effects (e.g., updating variables or performing operations on each element).const arr = [1, 2, 3, 4]; arr.forEach((element, index) => { console.log(index, ':' , element); })
for…of:
-
for…of iterates over the values of an iterable object (e.g., arrays, strings, maps, sets, etc.).
-
It provides a simpler syntax compared to traditional
for
loops, as it automatically handles iterating over the elements of the iterable without explicitly accessing the index or length. -
Unlike
forEach
,for...of
can be used with any iterable object, not just arrays.const str = 'Hello'; for (const char of str) { console.log(char); }
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?
- Value Proposition: “As a recent graduate with strong [skills], I’m eager to learn and contribute. My academic background in [field] and internship experience in [relevant area] equipped me with [specific skills].”
- STAR Example: “During my internship, I [situation] was tasked with [task]. By [action], I achieved [result], exceeding expectations by [quantification].”
- Connect to company: “I’m impressed by [company’s achievements] and believe my [skills] align with your focus on [company’s goal].”
- Confident closing: “I’m confident I can quickly learn and become a valuable asset to your team.”
How should Experienced Answer this Question?
- Value Proposition: “With [years] of experience in [industry], I have a proven track record of success in [areas]. I excel in [skills] and have a passion for [industry].”
- STAR Example: “In my previous role at [company], I led a project to [situation]. By implementing [action], I achieved [result], generating [quantifiable benefit].”
- Connect to company: “I’m particularly drawn to your [company’s unique aspect] and believe my experience can contribute to your ongoing [company’s goal].”
- Confident closing: “I’m confident I can make an immediate impact and help your company achieve its goals.”
Other articles
- Asynchronous JavaScript
- Message Passing in Chrome extension
- Request for startups
- 1.5+ million pdfs in 25 minutes