DEV Community

Cover image for Flags In Programming
Hasan Zohdy
Hasan Zohdy

Posted on

Flags In Programming

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");
Enter fullscreen mode Exit fullscreen mode

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(); // []
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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)

General Topics

Packages & Libraries

Top comments (0)