Imperative vs Declarative Programming in DevOps
Tags: productivity, automation, tooling, devops
Published: 2023-04-13 22:18:22

Some teams might adopt several tools to automate DevOps with Infrastructure as Code, depending on the team's maturity level, which implies knowledge about declarative and imperative programming. These approaches are easy to find in many IaC tools.

Still, for an efficient implementation of an IaC in your project, see the difference between these approaches:

- Imperative: The code written is very explicit in every single line, which will help the computer understand **how** the algorithm should be executed. The developer will put on the "machine's hat" and think like a machine to understand how the computer operates so it can understand the given instructions. It's a more detailed programming style, which requires deep knowledge of code skills.

- Declarative: For the declarative approach, there's more abstraction when writing the IaC to tell **what** you want to be executed. It is more focused on the outcome without explicitly defining every step or condition the code will perform in the machine.

As an example of an imperative code, this one below creates explicitly the function that will track the events that will be sent to CTO.ai's Platform in order to measure the deployment metrics with DORA:

```
export async function track(
  tags: string[] | string,
  metadata: object,
): Promise<void> {
  try {
    await request.track({
      tags: Array.isArray(tags) ? tags : [tags],
      ...metadata,
    })
  } catch (e) {
    throw new CTOAI_Error(100, 'sdk.track')
  }
}
```

And for the Declarative code type, we can demonstrate it by importing the SDK to declare the event that will be traced.

```
sdk.track([], {
        event_name: 'deployment',
        event_action: 'failed',
        environment: STACK_ENV,
        repo: STACK_REPO,
        branch: STACK_TAG,
        commit: STACK_TAG,
        image: `${STACK_REPO}:${STACK_TAG}`
      })
```

Both approaches may be used when defining the Infrastructure as Code to automate Workflow processes for software deliveries. However, declarative programming is the most common approach used in DevOps tools, such as Terraform and CloudFormation from AWS, because it's more efficient for replicating the code throughout the CI/CD definition.

With the CTO.ai Developer Control Plane, the way of defining infrastructure is mostly in the declarative approach, although the usage of imperative programming can be applicable in some cases, depending on how are the developer workflows of an organization.

You can see the [source code of the SDKs provided by CTO.ai](https://github.com/cto-ai/sdk-js) mentioned in this article, which allow teams to write customizable workflows and enable dev teams to interact with deployment and builds from Slack or GitHub.

Therefore, you can see some templates of workflows, which [you can see in this GitHub repository](https://github.com/workflows-sh/aws-ecs-fargate), in order to measure your deployment performance with DORA metrics by sending deployment data to the Platform.

I hope this helped you understand Imperative and Declarative concepts and their applicability in DevOps within IaC for Workflows definition.

See you soon!