Nodejs Internship
If yuo're looking for a Nodejs internship and you have the gut and power to learn, message me on Discord (Node Js Channel or send me a PM).
Introduction
Flags are a very important concept in programming, specially when you're working with a lot of conditions, and you want to make your code more readable and maintainable.
What is a flag?
A flag is a variable that is used to indicate a condition, it's a boolean variable that is used to indicate a condition, and it's used to control the flow of the program.
It could also be a non-boolean value, but most of the time it's a boolean value.
Why flags?
Flags are very useful when you have a lot of conditions, and you want to make your code more readable and maintainable.
Example
Let's say you have a function that prints in console messages, but this should only be done if debug mode is enabled, and you have a variable called debugMode
that is a boolean value, and it's used to indicate if debug mode is enabled or not.
let debugMode = true;
function enableDebugMode() {
debugMode = true;
}
function disableDebugMode() {
debugMode = false;
}
function printMessage(message) {
if (debugMode) {
console.log(message);
}
}
printMessage("Hello World");
In the above example, we have a function called printMessage
that prints a message in console, but this should only be done if debug mode is enabled, and we have a variable called debugMode
that is a boolean value, and it's used to indicate if debug mode is enabled or not.
Let's see another example.
Assuming we have a function called getUsers
that returns a list of users, but this should only be done if the user is logged in, and we have a variable called loggedIn
that is a boolean value, and it's used to indicate if the user is logged in or not.
let loggedIn = false;
function login() {
loggedIn = true;
}
function logout() {
loggedIn = false;
}
function getUsers() {
if (loggedIn) {
return [
{ name: "Hassan", age: 25 },
{ name: "Ahmed", age: 30 },
];
}
return [];
}
getUsers(); // []
In the above example, we have a function called getUsers
that returns a list of users, but this should only be done if the user is logged in, and we have a variable called loggedIn
that is a boolean value, and it's used to indicate if the user is logged in or not.
Another good realistic example, let's create a function called isUnique
that receives an array of numbers and a number, and it returns true if the number is unique in the array, and false if it's not.
function isUnique(numbers, number) {
let isUnique = true;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === number) {
isUnique = false;
break;
}
}
return isUnique;
}
isUnique([1, 2, 3, 4, 5], 6); // true
isUnique([1, 2, 3, 4, 5, 6, 7, 5, 9], 5); // false
Let's take an example of non-boolean flag.
Now we're going to do something similar to the isUnique
but this time we are going to make unique
function which receives an array and return only the unique values.
function unique(numbers) {
let uniqueNumbers = [];
for (let i = 0; i < numbers.length; i++) {
if (isUnique(numbers, numbers[i])) {
uniqueNumbers.push(numbers[i]);
}
}
return uniqueNumbers;
}
Flags can be used as caching mechanism
Let's say we're building an Ajax requests package, we need to add feature of caching response, in that sense we can create an object to cache the response and the key of that object will be the url of the request, and the value will be the response.
let cache = {}; // <--- The flag
function ajax(url) {
if (cache[url]) {
return cache[url];
}
// make the request and save the response in cache
cache[url] = getResponseFromSomeWhere(url);
return response;
}
Conclusion
Flags are very useful when you have a lot of conditions, and you want to make your code more readable and maintainable.
Most of the time if you're developing something, you're definitely going to use flags.
😍 Join our community
Answer problem solving questions and get mentor to review your answer on mentoor.io
Join our community on Discord to get help and support (Node Js 2023 Channel).
📚 Bonus Content 📚
You may have a look at these articles, it will definitely boost your knowledge and productivity.
Courses (Articles)
- Nodejs Course 2023: Typescript, Fastify And MongoDB
- OOP In JS And TS From The Very Beginning
- ES6++: The Ultimate Guide to ES6 and Beyond
- React Js: Let"s Create File Manager With React Js and Node Js
General Topics
- Stop Using If..else
- Event Driven Architecture: A Practical Guide in Javascript
- Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript
- After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
Packages & Libraries
- Powerful File System manager for Nodejs
- Mongez Cache, a powerful storage manager for web applications
- React Atom: A simple yet powerful React JS / React Native state management
- Collections: Your ultimate Javascript Arrays Manager
- Supportive Is: an elegant utility to check types of values in JavaScript
- Localization: An agnostic i18n package to manage localization in your project
Top comments (0)