Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XS✔ ◾ Add Bicep User-defined Data Type Rule (#8189)
* Add Bicep User-defined Data Type Rule * Auto-fix Markdown files * Fix headings according to markdown suggestions * improved punctuations * Update rules/bicep-do-you-use-user-defined-data-types/rule.md Co-authored-by: Matt Wicks [SSW] <wicksipedia@users.noreply.github.com> * Auto-fix Markdown files * Add more bad example * change wording * Removed IaC category, moved new rule under Azure --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Tiago Araújo [SSW] <tiagov8@gmail.com> Co-authored-by: Matt Wicks [SSW] <wicksipedia@users.noreply.github.com>
- Loading branch information
1 parent
dac0e1b
commit f89af88
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
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--> | ||
|
||
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 | ||
::: | ||
|
||
``` bicep | ||
param sku object | ||
``` | ||
|
||
:::bad | ||
Bad example - When declaring a parameter as an untyped object, bicep cannot validate the object's properties and values at compile time, risking runtime errors. | ||
::: | ||
|
||
``` 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 | ||
::: |