Quote Originally Posted by techgnome View Post
Code that I will comment (and did)
[CODE]
Here's my first stab, this is probably not quite encapsulating the knowledge in the best way, simply because I'm not familiar with the problem, but here goes (and yes, I am fully aware of the irony of using comments to explain why I wouldn't have comments...)

vbnet Code:
  1. ' "Get a list of suppressed and unsuppressed messages"
  2. ' Your variable names tell me what these are, as well as having a simple predicate. Maybe it could be argues for two full blown methods:
  3. ' GetSuppressedMessages(bvMessages) and GetUnsuppressedMessage(bvMessage). Meh, I wouldn't feel that to be 100% necessary.
  4. Dim unsuppressedMessages As List(Of BatchValidationMessagesUIModel) = bvMessages.Where(Function(m) m.SUPPRESSED.Value = False).ToList
  5. Dim suppressedMessages As List(Of BatchValidationMessagesUIModel) = bvMessages.Where(Function(m) m.SUPPRESSED.Value = True).ToList
  6. Dim errorAnnotations = annotations.Value.Where(Function(a) a.Text.Value = "Record contains custom validation error(s).").ToList
  7. Dim warningAnnotations = annotations.Value.Where(Function(a) a.Text.Value = "Record contains suppressed custom validation error(s).").ToList
  8.  
  9. annotations.Visible = True
  10.  
  11. ' "If there are unsuppressed messages, ..."
  12. ' Well, how about writing the code exactly saying that, instead of obscuring it with counting and then
  13. ' comparing that count with some other value?
  14. If unsuppressedMessages.Any Then
  15.     ' "..., add to the annotations (the actual error will be a part of the system messages, which will prevent it from committing"
  16.     ' The part in brackets might fall in to the "comment why its the non-obvious way" part of my reason for commenting
  17.     ' The "add to the annotations part, however....
  18.     AddToAnnotations(unsuppressedMessages)
  19.  
  20. End If
  21.  
  22. ' "If there are suppressed messages, ..."
  23. ' Again, let's write our condition so it reads how you want to write the comment
  24. If suppressedMessages.Any Then
  25.     ' "...add to the annotations - should be a warning, so that it can still commit"
  26.     ' Sounds like we need to note that this is added as a warning, so I'll include that in the method name
  27.     ' Note that I've included the AndAlso check in this method, as it felt part of that logic -
  28.     ' another point about code that doesn't need comment is that you need to separate your concerns more carefully
  29.     AddWarningForSuppressedMessage()
  30. End If
  31.  
  32.  
  33. ' ...
  34.  
  35. Private Sub AddToAnnotations(unsuppressedMessages As List(Of Blah)
  36.  
  37.     ' "If there isn't already a current notice about custom validations, then add it"
  38.     ' So, we want to ensure that there's a custom validation notice? Okay...
  39.     EnsureCustomValidationNotice()
  40.  
  41.     ' "Add the validaiton messages"
  42.     ' What part of that isn't explained in the code?
  43.     For Each msg As BatchValidationMessagesUIModel In unsuppressedMessages
  44.         AddRowAnnotation(currentModel, AnnotationCategory.Unknown, msg.MESSAGETEXT.Value, False, BatchMessageType.GeneralError)
  45.     Next
  46.  
  47. End Sub
  48.  
  49. Private Sub EnsureCustomValidationNotice
  50.     If errorAnnotations.Count = 0 Then
  51.         AddRowAnnotation(currentModel, AnnotationCategory.Unknown, "Record contains custom validation error(s).", False, BatchMessageType.GeneralError)
  52.     End If
  53. End Sub
  54.  
  55. Private Sub AddWarningForSuppressedMessage
  56.     If Not warningAnnotations.Any Then
  57.         AddRowAnnotation(currentModel, AnnotationCategory.Unknown, "Record contains suppressed custom validation error(s).", False, BatchMessageType.GeneralError)
  58.     End If
  59. End Sub

Okay, and once again without the comments?
vbnet Code:
  1. Dim unsuppressedMessages As List(Of BatchValidationMessagesUIModel) = bvMessages.Where(Function(m) m.SUPPRESSED.Value = False).ToList
  2. Dim suppressedMessages As List(Of BatchValidationMessagesUIModel) = bvMessages.Where(Function(m) m.SUPPRESSED.Value = True).ToList
  3. Dim errorAnnotations = annotations.Value.Where(Function(a) a.Text.Value = "Record contains custom validation error(s).").ToList
  4. Dim warningAnnotations = annotations.Value.Where(Function(a) a.Text.Value = "Record contains suppressed custom validation error(s).").ToList
  5.  
  6. annotations.Visible = True
  7.  
  8. If unsuppressedMessages.Any Then
  9.     AddToAnnotations(unsuppressedMessages)
  10. End If
  11.  
  12. If suppressedMessages.Any Then
  13.     AddWarningForSuppressedMessage()
  14. End If
  15.  
  16.  
  17. ' ...
  18.  
  19. Private Sub AddToAnnotations(unsuppressedMessages As List(Of Blah)
  20.     EnsureCustomValidationNotice()
  21.  
  22.     For Each msg As BatchValidationMessagesUIModel In unsuppressedMessages
  23.         AddRowAnnotation(currentModel, AnnotationCategory.Unknown, msg.MESSAGETEXT.Value, False, BatchMessageType.GeneralError)
  24.     Next
  25. End Sub
  26.  
  27. Private Sub EnsureCustomValidationNotice
  28.     If errorAnnotations.Count = 0 Then
  29.         AddRowAnnotation(currentModel, AnnotationCategory.Unknown, "Record contains custom validation error(s).", False, BatchMessageType.GeneralError)
  30.     End If
  31. End Sub
  32.  
  33. Private Sub AddWarningForSuppressedMessage
  34.     If Not warningAnnotations.Any Then
  35.         AddRowAnnotation(currentModel, AnnotationCategory.Unknown, "Record contains suppressed custom validation error(s).", False, BatchMessageType.GeneralError)
  36.     End If
  37. End Sub