Can I simplify this VB.net code, so I don’t need to repeat so much?

I am creating a planning algorithm. My code is very long. Can anyone suggest how I can make my code shorter, since I have to repeat the same thing many times lower, and where the external one is 4 or 5 more, I have to repeat it even more:

Here is an example of only part of my code:

ElseIf Val(jobs_txt.Text) = 3 Then If (a < b And b < c) Then awt1_lbl.Text = Val(0) awt2_lbl.Text = a awt3_lbl.Text = a + b y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y ElseIf (a < c And c < b) Then awt1_lbl.Text = Val(0) awt2_lbl.Text = a + b awt3_lbl.Text = a y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y ElseIf (b < a And a < c) Then awt1_lbl.Text = a awt2_lbl.Text = Val(0) awt3_lbl.Text = a + b y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y ElseIf (b < c And c < a) Then awt1_lbl.Text = a + b awt2_lbl.Text = Val(0) awt3_lbl.Text = a y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y ElseIf (c < a And a < b) Then awt1_lbl.Text = a awt2_lbl.Text = a + b awt3_lbl.Text = Val(0) y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y ElseIf (c < b And b < a) Then awt1_lbl.Text = a + b awt2_lbl.Text = a awt3_lbl.Text = Val(0) y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y End If 
+4
source share
4 answers

Yes, there are a few things you can do:

  • The calculation of y is the same in all cases, so you do not need to repeat it.
  • The last ElseIf will always be true, so just use Else
  • Using Val(0) will result in a lot of implicit conversions, in fact it will be CStr(Val(Cstr(0))) , so just use "0" .

the code:

 ElseIf Val(jobs_txt.Text) = 3 Then If (a < b And b < c) Then awt1_lbl.Text = "0" awt2_lbl.Text = a awt3_lbl.Text = a + b ElseIf (a < c And c < b) Then awt1_lbl.Text = "0" awt2_lbl.Text = a + b awt3_lbl.Text = a ElseIf (b < a And a < c) Then awt1_lbl.Text = a awt2_lbl.Text = "0" awt3_lbl.Text = a + b ElseIf (b < c And c < a) Then awt1_lbl.Text = a + b awt2_lbl.Text = "0" awt3_lbl.Text = a ElseIf (c < a And a < b) Then awt1_lbl.Text = a awt2_lbl.Text = a + b awt3_lbl.Text = "0" Else awt1_lbl.Text = a + b awt2_lbl.Text = a awt3_lbl.Text = "0" End If y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y 

Actually, if the values ​​do not need to be converted to strings, and then return to the numbers, you can simply calculate y as follows:

 y = (a * 2 + b) / 3 
+3
source

Firstly, it looks like you can move the last two lines

 y = (Val(awt1_lbl.Text) + Val(awt2_lbl.Text) + Val(awt3_lbl.Text)) / 3 awt_ans.Text = y 

outside if-then-else , because they do the same in all six cases.

Further, since your code assigns Val (0) to the element with the smallest key a+b to the value in the middle and a to the largest, you can put a , b and c in the awt1_lbl , awt2_lb and awt3_lbl array into the value array and sort them as follows way:

 Dim keys() As String = { a, b, c } Dim labels() As LabelType = { awt1_lbl, awt2_lb, awt3_lbl } // put real label type there Array.Sort(keys, labels) labels(0).Text = Val(0) labels(1).Text = a+b labels(2).Text = a 
+3
source

You have to make it a class, then it is easier to read, reuse and extend:

 Dim awt = New AWT(a, b, c) awt1_lbl.Text = awt.GetAWT1().ToString awt2_lbl.Text = awt.GetAWT2().ToString awt3_lbl.Text = awt.GetAWT3().ToString awt_ans.Text = awt.GetY().ToString 

And here’s a possible implementation, I made it Option Strict so that it can be easily translated into C #, and there are also fewer -prone errors.

 Public Class AWT Public Property A As Int32 Public Property B As Int32 Public Property C As Int32 Public Sub New(a As Int32, b As Int32, c As Int32) Me.A = a Me.B = b Me.C = c End Sub Public Function GetY() As Int32 Return GetAWT1() + GetAWT2() + GetAWT3() End Function Public Function GetAWT1() As Int32 If (A < B AndAlso B < C) OrElse (A < C AndAlso C < B) Then Return 0 ElseIf (B < A AndAlso A < C) OrElse (C < A AndAlso A < B) Then Return A Else Return A + B End If End Function Public Function GetAWT2() As Int32 If (A < B AndAlso B < C) OrElse (C < B AndAlso B < A) Then Return A ElseIf (A < C AndAlso C < B) OrElse (C < A AndAlso A < B) Then Return A + B Else Return 0 End If End Function Public Function GetAWT3() As Int32 If (A < B AndAlso B < C) OrElse (B < A AndAlso A < C) Then Return A + B ElseIf (A < C AndAlso C < B) OrElse (B < C AndAlso C < A) Then Return A Else Return 0 End If End Function End Class 
+1
source

In addition to other answers:

 If (a < b And b < c) Then 

should be the same as:

 If (a < b < c) Then 

Saves multiple characters!

Edit: MetaKnight is right, this is an invalid syntax. However, the compiler does not complain, and I cannot understand what it is doing.

-one
source

Source: https://habr.com/ru/post/1393997/


All Articles