Flags In Programming
Tags: bestpractice, controlstructure, codestructure, cleancode
Published: 2023-04-05 13:16:04

---
title: Flags In Programming
published: true
description: The importance of flags
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1lyzwfg7dja9sxtarpc.png
tags: bestPractice, controlStructure, codeStructure, cleanCode
---

## Nodejs Internship

If yuo're looking for a Nodejs internship and you have the gut and power to learn, message me on [Discord](https://discord.gg/pb2vmdfhGf) (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.

```js
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.

```js
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.

```js
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.

```js
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.

```js
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](mentoor.io)

Join our community on [Discord](https://discord.gg/pb2vmdfhGf) 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](https://dev.to/hassanzohdy/nodejs-course-2023-introduction-to-nodejs-3n4e)
- [OOP In JS And TS From The Very Beginning](https://dev.to/hassanzohdy/jsts-oop-course-1-introduction-2ld)
- [ES6++: The Ultimate Guide to ES6 and Beyond](https://dev.to/hassanzohdy/es6-1-introduction-h3e)
- [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)

General Topics

- [Stop Using If..else](https://dev.to/hassanzohdy/stop-using-ifelse-385l)
- [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
- [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
- [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

- [Powerful File System manager for Nodejs](https://dev.to/hassanzohdy/powerful-file-system-manager-for-nodejs-1f5h)
- [Mongez Cache, a powerful storage manager for web applications](https://dev.to/hassanzohdy/mongez-cache-a-powerful-storage-manager-for-web-applications-5bel)
- [React Atom: A simple yet powerful React JS / React Native state management](https://dev.to/hassanzohdy/react-atom-a-simple-yet-powerful-react-js-react-native-state-management-age)
- [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
- [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
- [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)