Pet peeves when coders ask questions

Something seen repeatedly is code that does not make sense while this is one variation. 

For instance the following line of code (where BS is a BindingSource) where the developer knows the type. What would be appropriate rather than TryCast is CType or DirectCast with a meaningful variable name. The person wants to update the current record and is not updating.

Dim r = TryCast(BS.Current, DataRowView)

When uses the following to set a value

r("Area") = calculatedresult

They had the following for updating on property changed

dataTable.Columns.Add(New DataColumn() With {
                             .ColumnName = "Area",
                             .DataType = GetType(Decimal)
                         })

Why isn't the value being updated? Because they went against the DataRowView rather than the DataRow -> column. I recommended using the following.

Public Class CodeSampleForm
    Private ReadOnly _bindingSource As New BindingSource()
 
    Private Sub CalculateButton_Click(sender As Object, e As EventArgs) _
        Handles CalculateButton.Click
 
        Dim currentRow = CType(_bindingSource.Current, DataRowView).Row
        Dim area = currentRow.Field(Of Decimal)("Area")
 
        currentRow.SetField("Area", area + 1)
 
    End Sub
    Private Sub CodeSampleForm_Shown(sender As Object, e As EventArgs) _
        Handles Me.Shown
 
        Dim dataTable As New DataTable()
        dataTable.Columns.Add(New DataColumn() With {
                             .ColumnName = "Area",
                             .DataType = GetType(Decimal)
                         })
 
        dataTable.Rows.Add(1.4D)
        dataTable.Rows.Add(2.4D)
 
        _bindingSource.DataSource = dataTable
 
        BindingNavigator1.BindingSource = _bindingSource
 
        Textbox_Area.DataBindings.Add("Text", _bindingSource, "Area",
                                      True,
                                      DataSourceUpdateMode.OnPropertyChanged)
    End Sub
End Class
 

As in many cases they came back and said the code worked but didn't understand why. Seems this is a problem not only with this developer but a vast amount of developers, they copy and paste code, run and look for expected results. If the result are as expected they move on and never attempt to learn why code given works but their code failed.

What these developers need to do in similar situations is to go back immediately and figure out why the provided code works against their original attempt. This usually entails some research by reading Microsoft documentation. There was a developer I worked with years ago who stared at a section of code for almost 30 minutes until I ask what he was doing? He said I'm trying to learn what the code does. In turn I said why not read the documentation? He replied, don't have time. This is a problem as one does not learn from staring at code at novice level and even at intermediate level coders.

Similarly I provided the following for another post.
Imports System.ComponentModel
 
Public Class RemoveAndBind(Of T)
    Inherits BindingList(Of T)
 
    Protected Overrides Sub RemoveItem(ByVal index As Integer)
        RaiseEvent FireBeforeRemove(Me,
            New ListChangedEventArgs(ListChangedType.ItemDeleted, index))
        MyBase.RemoveItem(index)
    End Sub
 
    Public Event FireBeforeRemove As EventHandler(Of ListChangedEventArgs)
End Class

I was asked to explain the code as in the example above and they had a full working example.

Imports System.ComponentModel
Imports BindingListListChangedLibrary
 
Public Class Form1
    WithEvents personBindList As RemoveAndBind(Of Person)
    Private ReadOnly operations As New Operations
    Private currentlyLoading As Boolean = True
    ''' <summary>
    ''' Focus on ListBox1 and BindingList personBindList.New List
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
 
        '
        ' Populate a few people
        '
        Dim peopleList = New List(Of Person) From {
                New Person() With {.FirstName = "Karen", .LastName = "Payne"},
                New Person() With {.FirstName = "Bill", .LastName = "Smith"},
                New Person() With {.FirstName = "Anne", .LastName = "Jones"}}
 
        '
        ' Add people to BindingList DataSource
        '
 
        personBindList = New RemoveAndBind(Of Person) '(peopleList)
        For Each person As Person In peopleList
            personBindList.Add(person)
        Next
 
        '
        ' Display people in ListBox
        '
        ListBox1.DataSource = personBindList
 
    End Sub
    ''' <summary>
    ''' Here we look to see if a new person was added, if
    ''' so pass the person to Operations class via NewPersonAdded procedure
    ''' which could be a function if something needs to come back
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub personBindList_ListChanged(sender As Object, e As ListChangedEventArgs) _
        Handles personBindList.ListChanged
 
        If Not currentlyLoading Then
            If e.ListChangedType = ListChangedType.ItemAdded Then
                Dim newPerson = personBindList.Item(e.NewIndex)
 
                operations.NewPersonAdded(newPerson)
            End If
        Else
            currentlyLoading = False
        End If
    End Sub
 
    Private Sub AddPersonButton_Click(sender As Object, e As EventArgs) _
        Handles AddPersonButton.Click
 
        personBindList.Add(New Person() With {
                              .FirstName = FirstNameTextBox.Text,
                              .LastName = LastNameTextBox.Text})
 
    End Sub
 
    Private Sub RemoveCurrentPersonButton_Click(sender As Object, e As EventArgs) _
        Handles RemoveCurrentPersonButton.Click
 
        If personBindList.Count > 0 Then
            personBindList.Remove(personBindList.Item(ListBox1.SelectedIndex))
        End If
 
    End Sub
 
    Private Sub personBindList_FireBeforeRemove(sender As Object, e As ListChangedEventArgs) _
        Handles personBindList.FireBeforeRemove
 
        operations.RemovedPersonAdded(personBindList.Item(ListBox1.SelectedIndex))
 
    End Sub
End Class

Well (to the developer) don't you study the code and read the docs along with stepping through the code? Developer replied with "I don't understand" and "I've never used breakpoints before".

This is inherently stops grow of any developer who is not reading the documentation along with never using the debugger. 

Couple this in regards to growth to move past conventional methods for working with data e.g. move from DataTable to classes with proper interfaces and Entity Framework/Entity Framework Core.

Some recommendations for those developer who fall back on tried and true.

  • Take time each week to self educate
    • Read Microsoft documentation
    • Read third party documentation when using non-Microsoft code.
  • Learn to work with Visual Studio debugger.
  • Learn how to write unit test
  • Become a expert at searching the web for solutions rather than asking a question.
Using these recommendations many times will save time to resolve an issue. For example, ask a question on a forum and wait. Then comes a response perhaps five minutes later or hours later that ask for more information as the initial question is vague.

By self educating a developer may solve the problem is much less time then relying on asking in a forum. 

Then there is "when things go wrong", from experience if a question has over eight replies without a solution most likely a solution never comes. I've seen questions with 50 plus replies with no solution and typically people working on the question get upset and things going in a bad direction. 

This in many cases can be resolved by self educating. So if you fall into the category of not being able to resolve simple task you need to self educate.

Take the time now rather than later rather then wasting time with forum questions and leave forum questions for time when you have worked through an issue best as you could along with working with the debugger, writing SQL in an editor like SSMS (SQL-Server Management Studio) rather than in code.

 

Comments

Popular posts from this blog

VB.NET Working with Delegate and Events

Coders asking questions in online forums

GitHub download partial repository