Concision vs Lisibilité : entre les deux, mon coeur balance
Hello,
Juste une petite question rapide.
Voici deux bouts de code qui, sauf erreur de ma part, sont fonctionnellement identique.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| For Each p As PROMO_DTO.Product In If(dto.ListType = "INC", dto.Products, dto.ProductsOut)
Dim drp As DataRow = product.NewRow
drp.Item("BRA_ID") = dto.Brand.Id
drp.Item("DEP_ID") = dto.Department.Id
drp.Item("DTO_PERCENT") = dto.Percentage
drp.Item("PLO_BARCODE") = p.EAN
drp.Item("PLO_INCLUSIVE") = If(dto.ListType = "INC", True, False)
drp.Item("PLO_COLOR") = p.Color
drp.Item("PLO_DESC") = p.Description
If p.Price.HasValue Then
drp.Item("PLO_PRICE") = p.Price.Value
End If
drp.Item("PLO_SIZE") = p.Size
product.Rows.Add(drp)
Next |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| If dto.ListType = "INC" Then
For Each p As PROMO_DTO.Product In dto.Products
Dim drp As DataRow = product.NewRow
drp.Item("BRA_ID") = dto.Brand.Id
drp.Item("DEP_ID") = dto.Department.Id
drp.Item("DTO_PERCENT") = dto.Percentage
drp.Item("PLO_BARCODE") = p.EAN
drp.Item("PLO_INCLUSIVE") = True
drp.Item("PLO_COLOR") = p.Color
drp.Item("PLO_DESC") = p.Description
If p.Price.HasValue Then
drp.Item("PLO_PRICE") = p.Price.Value
End If
drp.Item("PLO_SIZE") = p.Size
product.Rows.Add(drp)
Next
ElseIf dto.ListType = "EXC" Then
For Each p As PROMO_DTO.Product In dto.ProductsOut
Dim drp As DataRow = product.NewRow
drp.Item("BRA_ID") = dto.Brand.Id
drp.Item("DEP_ID") = dto.Department.Id
drp.Item("DTO_PERCENT") = dto.Percentage
drp.Item("PLO_BARCODE") = p.EAN
drp.Item("PLO_INCLUSIVE") = False
drp.Item("PLO_COLOR") = p.Color
drp.Item("PLO_DESC") = p.Description
If p.Price.HasValue Then
drp.Item("PLO_PRICE") = p.Price.Value
End If
drp.Item("PLO_SIZE") = p.Size
product.Rows.Add(drp)
Next
End If |
Le premier est plus court mais pas forcément le plus clair au premier abord avec les fonctions If.
Le second est plus long mais on identifie du premier coup d’œil l'utilité de chaque branche du If.
On est bien d'accord que pour si peu, ma question n'a pas de grand intérêt. Mais j'suis du genre perfectionniste et j'ai pas trop l'habitude des fonctions If. Est-ce une bonne pratique ? Si quelqu'un d'autre doit reprendre ce code (ou si je dois revenir dessus dans 3 mois), j'ai des doutes sur la meilleure écriture.
Merci d'avance pour vos avis éclairés :).