Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XS✔ ◾ Add Bicep User-defined Data Type Rule #8189

Merged
merged 9 commits into from Mar 28, 2024
1 change: 1 addition & 0 deletions categories/software-engineering/index.md
Expand Up @@ -7,6 +7,7 @@ index:
- rules-to-better-net8-migrations
- rules-to-better-architecture-and-code-review
- rules-to-better-azure
- rules-to-better-infrastructure-as-code
- rules-to-better-blazor
- rules-to-better-mvc
- rules-to-better-bots
Expand Down
ricksu978 marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,17 @@
---
type: category
title: Rules to Better Infrastructure as Code (IaC)
guid: 2948bfca-605b-4e67-974a-3cec1c7aa741
uri: rules-to-better-infrastructure-as-code
experts: https://www.ssw.com.au/people/?skill=bicep
consulting: https://ssw.com.au/consulting/azure
index:
- bicep-user-defined-data-types

---

Infrastructure as code (IaC) uses DevOps methodology and versioning with a descriptive model to define and deploy infrastructure, such as networks, virtual machines, load balancers, and connection topologies. Just as the same source code always generates the same binary, an IaC model generates the same environment every time it deploys.

Need help with Microsoft Azure? Check [SSW's Azure consulting page](https://ssw.com.au/consulting/azure).

<!-- endintro -->
57 changes: 57 additions & 0 deletions rules/bicep-do-you-use-user-defined-data-types/rule.md
@@ -0,0 +1,57 @@
---
type: rule
archivedreason:
title: Bicep - Do you use User-defined Data Types?
guid: 2a3ba816-ba40-4cfe-8193-7e151e038ac5
uri: bicep-user-defined-data-types
created: 2024-03-19T19:19:19.1919191Z
authors:
- title: Rick Su
url: https://ssw.com.au/people/rick-su
related: []
redirects: []

---

[User-defined data types in Bicep](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/user-defined-data-types) allow you to create custom data structures for better code organization and type safety. They enhance reusability, abstraction, and maintainability within projects.

<!--endintro-->

## Get Started

[Bicep CLI version 0.12.X or higher](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/install) is required to use this feature.

## Pain
ricksu978 marked this conversation as resolved.
Show resolved Hide resolved

When creating a cloud resource, numerous parameters are typically required for configuration and customization. Organizing and naming these parameters effectively is increasingly important.

``` bicep
@allowed(['Basic', 'Standard'])
param skuName string = 'Basic'
@allowed([5, 10, 20, 50, 100])
param skuCapacity int = 5
param skuSizeInGB int = 2
```

:::bad
Bad example - Relying on parameter prefixes and order leads to unclear code, high complexity, and increased maintenance effort
:::

ricksu978 marked this conversation as resolved.
Show resolved Hide resolved
``` bicep
// User-defined data type
type skuConfig = {
name: 'Basic' | 'Standard'
capacity: 5 | 10 | 20 | 50 | 100
sizeInGB: int
}

param sku skuConfig = {
name: 'Basic'
capacity: 5
sizeInGB: 2
}
```

:::good
Good example - User-defined data type provides type safety, enhanced readability and making maintenance easier
:::