Improve your coding
This is a small 'guide' on how to improve your coding and make it more clear. You can keep the overview, following these tips.
If I forgot anything, please remind me /Sticky plox.
Rename your controls
This is important to actually keep your code clean.
Do not keep the default control names like Button1/Textbox1.
Rename them.
Textbox1 = txtResult
Button1 = cmdCalculate
You can now easily recognize your events in your code, instead of having to search them.
Comment your code
This is obvious. You may forget stuff later on, therefore you can just add comments into your code.
[php]'comment[/php]
Use your datatypes correctly
Code:
Dim int as integer = "5"
Dim str as string = "Sample Text"
txtResult.text = int
Integer <> String. Therefore the correct way is:
[php]Dim int as integer = 5[/php]
Shorten your code
Instead of writing:
Code:
txtResult.text = txtResult.text + int
You better use:
[php]txtResult.text += int[/php]
Furthermore:
[php]= 'obvious
<> 'not the same, Noob <> Pro
i <=5 'i is smaller than 5 or 5
i >=5 'i is bigger than 5 or 5
i < 5 'i is smaller than 5
i > 5 'i is bigger than 5
+ 'add
- 'substract
* 'multiply
/ 'division
[/php]
Using
Code:
Dim str as IO.Streamreader = New Streamreader("C:\test.txt")
txtResult.text = str.readtoend
str.dispose
[php]Using str as new Io.Streamreader("C:\test.txt")
txtResult.text = str.readtoend
End Using[/php]
Case instead of If
Code:
If txtResult.text = "a" Then
Msgbox("Newb")
End if
If txtResult.Text = "b" Then
Msgbox("Newbie")
End if
If txtResult.text = "c" Then
Msgbox("Noob")
End if
[php]Select Case txtresult.Text
Case "a"
Msgbox("Newb")
Case "b"
Msgbox("Newbie")
Case "c"
Msgbox("Noob")
End Select[/php]
If-Structures
If condition Then
[statements]
ElseIf condition Then
[statements]
Else
[statements]
End If
[php]If textbox1.text = "meh"
'do something
elseif Textbox1.text = "duh"
'do something else
else 'if it's neither meh nor duh
'something
end if[/php]
Using Case...
[php]Select Case Textbox1.Text
Case "meh"
'do something
Case "Duh"
'do something else
Case Else
'something
end select[/php]
[php]If Textbox1.text <> "" then 'if it's not empty
end if[/php]
If not...
[php]If not textbox1.text = "" then
end if[/php]
Booleans
[php]Dim b as boolean 'by default the boolean is FALSE[/php]
Code:
If b = true then
'do something
end if
If b = false Then
'do something
end if
[php]if b then 'true
'do something
else 'false
'do something
end if[/php]
Try-Catch
To avoid your program from closing when an error appears, you can use Try-Catch Blocks.
[php]Try
Timer.Value = txtResult.Text 'user may enter a letter instead of a number
Catch ex as exception
Msgbox(ex.Tostring) 'optional
End try[/php]
However, do not use them if they are not necessary.
Code:
Try
Timer.Value = nudValue.Value 'NumericUpDown
Catch ex as exception
end try
Regions
You can use Regions to make your code more clear.
[php]#Region "Settings"
Private Sub spStartup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles spStartup.CheckedChanged
if spstartup then
my.settings.spStartup = true 'run singleplayer on startup
my.settings.save
end if
End sub
Private Sub mpStartup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mpStartup.CheckedChanged
if spstartup then
my.settings.mpStartup = true 'run multiplayer on startup
my.settings.save
end if
End sub
#End region[/php]
Declare your own Subs/Function
You may have a lot of repetitive code. Therefore, you can shorten your code a lot by building functions or subs.
Code:
'grenades
If My.Settings.nade = true Then
string1 = readdll2("cshell.dll")
string2 = "&H" + Hex(string1 + &H108DFC8)
Call ITZAMOBSVG(string2, 1337, 4)
End If
'flashs
If My.Settings.flash = true Then
string1 = readdll2("cshell.dll")
string2 = "&H" + Hex(string1 + &H108DFD4)
Call ITZAMOBSVG(string2, 1337, 4)
End If
'grenade launcher
If My.Settings.gl = true Then
string1 = readdll2("cshell.dll")
string2 = "&H" + Hex(string1 + &H108DF64)
Call ITZAMOBSVG(string2, 1337, 4)
End If
[php]Private sub h4x(ByVal address as string, byval value as long, byval bytes as integer)
string1 = readdll2("cshell.dll")
string2 = "&H" + Hex(string1 + address)
Call ITZAMOBSVG(string2, value, bytes)
End sub[/php]
[php]If My.Settings.nade Then
h4x(&H108DFC8, 1337, 4)
End If
'flashs
If My.Settings.flash Then
h4x(&H108DFD4, 1337, 4)
End If
'grenade launcher
If My.Settings.gl Then
h4x(&H108DF64, 1337, 4)
End If[/php]
What is the difference between a function and a sub?
A function always returns something.
[php]Private Sub calculate(byval number1 as integer, byval number2 as integer)
textbox1.text = number1 + number2
end sub[/php]
[php]calculate(5,3)[/php]
[php]private function calculate(byval number1 as integer, byval number2 as integer)
return number1 + number2
end function
[/php]
[php]textbox1.text = calculate(5,3)[/php]
With
Code:
lblStatus.text = "Something"
lblstatus.BackColor = Color.Yellow
lblstatus.Location = New Point(50, 100)
lblstatus.visible = true
[php]With lblstatus
.text = "Something"
.backcolor = color.yellow
.location = new point(50, 100)
.visibile = true
end with[/php]
Structure
Code:
Dim firstPos as point
Dim firstDir as string
Dim firstFrame as integer
Dim secondPos as point
Dim secondDir as string
Dim secondFrame as integer
Dim thirdPos as point
Dim thirdDir as string
Dim thirdFrame as integer
[php]Structure meh
Dim Pos As Point
Dim Dir As String
Dim Frame As Integer
end structure
dim first as meh
dim second as meh
dim third as meh[/php]
[php]first.dir = "up"[/php]
I will try to update this topic with more improvements.
I do hope you will now code "cleaner". This will improve your coding. [/QUOTE]