Techletter #76 | June 8, 2024
Does Programming Language matter?
If you think it doesn’t matter then why do we have so many programming languages? If you think it matters, then why a backend app that can be built with Java can also be built with JavaScript or Python more quickly and efficiently?
Because there is positioning for every language. If you are building a web app then the default go-to language is JavaScript/TypeScript. They are just beautiful languages you can build very good backend and frontend applications with them. But if you are building a mobile app, then I think you should go for Java/Kotlin. I have built a mobile app with React Native. It’s frustrating when you want to access hardware-level APIs.
During college, I used to code in C in the first year, Java in the second and third years, and Python & JavaScript in the fourth year. After that, I got into pure JavaScript (React and Node) & TypeScript. I have no interest in writing long syntax just to print a string. That is the reason I chose JavaScript early when I started coding.
I believe that you should explore different languages, and stick to one because sticking to one for the initial few years will help in building a better understanding of the language, help us build software quickly have a better understanding of what happens under the hood. At the end of the day, more than a programming language, the problem that we are solving matters.
Every week I publish a newsletter where I write about some interesting things I learned. You can subscribe to it on my blog(link in the comments section)
What are the different ways of writing import
statements in TypeScript?
-
Default import: useful when a module exports a single value or function
import MODULE_NAME from './MODULE_LOCATION';
-
Named import: useful when you need to import multiple named values from a module
import { MODULE_NAME } from './MODULE_LOCATION'
-
importing multiple modules
import { MODULE_NAME1, MODULE_NAME2, ... , MODULE_NAMEn } from './MODULE_LOCATION'
-
importing everything from a module
import * as OBJ_NAME from './MODULE_LOCATION';
How do you get all the import statements inside a project in one go?
I landed up in such a situation where I would need all the import statements in a typescript project. I couldn’t find any package that does this, so I have written a simple function to replicate this functionality.
First, you must go inside the directory from which you need “import statements”.
I will fetch all the strings with the import command by reading all files. This can be achieved with the following command. I used shelljs to execute this command.
find ./src -type f -name "*.ts" -exec grep -H -r -e "import" {} +
now, YOUR_TEXT has the value in string format. Once you have this, you can separate all the import statements using regex;
const importRegex = /import\s+(?:{[^{}]+}|.*?)\s*(?:from)?\s*['"].*?['"]|import$$.*?$$/g;
const imports = YOUR_TEXT.match(importRegex)
What is the difference between spawn and execute in the node process?
spawn
-
spawn streams the child process’s stdout and stderr to the parent process, allowing you to access the output as it becomes available.
-
By default,
it
does not create a shell to execute the command, making it slightly more efficient. -
It is more suitable for long-running processes or those that generate large amounts of output, as it streams the data.
const { spawn } = require('child_process'); const child = spawn('ls', ['-l']); child.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); child.stderr.on('data', (data) => { console.error(`stderr: ${data}`); }); child.on('close', (code) => { console.log(`child process exited with code ${code}`); });
exec
-
exec buffers the entire output of the child process and passes it to a callback function once the process has completed.
-
It creates a shell to execute the command, allowing you to use shell syntaxes like pipes and redirects.
-
It may not be ideal for commands that generate a lot of output, as it buffers the entire output before passing it to the callback.
const { exec } = require('child_process'); exec('ls -l', (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`stdout: ${stdout}`); console.error(`stderr: ${stderr}`); });
How to execute a Linux command asynchronously in nodejs?
-
using promisify(exec)
const util = require('util'); const child_process = require('child_process'); const execAsync = util.promisify(child_process.exec); async function executeCommand() { try { const result = await execAsync(`find . -type f -name "*.js" -exec grep -H -r -e "import" {} +`); console.log(result.stdout); } catch (error) { console.error(error); } } executeCommand();
-
using shelljs, by keeping { async: true } while executing the shell command
const shell = require('shelljs'); async function executeCommand() { try { const result = await shell.exec(`find . -type f -name "*.js" -exec grep -H -r -e "import" {} +`, { async: true }); console.log(result.stdout); } catch (error) { console.error(error); } } executeCommand();