type | archivedreason | title | guid | uri | created | authors | related | redirects | |||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rule |
Bicep - Do you use User-defined Data Types? |
2a3ba816-ba40-4cfe-8193-7e151e038ac5 |
bicep-user-defined-data-types |
2024-03-19 19:19:19 UTC |
|
User-defined data types in Bicep allow you to create custom data structures for better code organization and type safety. They enhance reusability, abstraction, and maintainability within projects.
When creating a cloud resource, numerous parameters are typically required for configuration and customization. Organizing and naming these parameters effectively is increasingly important.
@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 :::
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. :::
// 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 :::