Page 1 of 4 123 ... LastLast
Results 1 to 15 of 60
  1. #1
    Blubb1337's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Germany
    Posts
    5,915
    Reputation
    161
    Thanks
    3,108

    Improve your Coding

    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]
    Last edited by NextGen1; 08-20-2010 at 05:08 AM.



  2. The Following 25 Users Say Thank You to Blubb1337 For This Useful Post:

    'Bruno (07-07-2010),3D-n1nj4 (06-21-2020),Abstract (07-07-2010),Alroundeath (07-10-2010),AndrewxXx (09-21-2011),cgallagher21 (08-29-2010),Hassan (07-07-2010),Ickiago (01-22-2021),Jason (07-07-2010),leoisso (03-04-2011),Lolland (07-07-2010),Luckyace (06-19-2013),omanel (07-07-2010),ppownage (09-11-2010),Renamon Toast Crunch (06-09-2013),RoPMadM (02-06-2016),Samueldo (07-20-2010),Sky_____ (03-22-2015),Slice- (09-01-2010),tempta43 (07-07-2010),Tunguestenio (03-27-2011),whit (07-07-2010),Zoom (07-07-2010),___x][GooD. (01-01-2011),|Cayn| (06-15-2020)

  3. #2
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    Wow...nice tutorial boy. Love it. Thanks for sharing

  4. #3
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Thanks Kevin, nice and informative guide you got there! I'm glad that it turns out that I do most of these already.

    Really need to work on renaming controls -.- I can never be fucked but getting into long projects it's confusing as shit! (What does button32_click do again...?)

    And commenting, I NEED DAT!

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  5. The Following User Says Thank You to Jason For This Useful Post:

    mayank2yk (07-30-2013)

  6. #4
    Blubb1337's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Germany
    Posts
    5,915
    Reputation
    161
    Thanks
    3,108
    Quote Originally Posted by J-Deezy View Post
    Thanks Kevin, nice and informative guide you got there! I'm glad that it turns out that I do most of these already.

    Really need to work on renaming controls -.- I can never be fucked but getting into long projects it's confusing as shit! (What does button32_click do again...?)

    And commenting, I NEED DAT!
    Indeed, on my trainer I'm always like wut se fack what is this button again. So I click on form1...go on the specific tab click the button...owwww that one...

    My File Sorter I did is very clean I guess =D



  7. #5
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    nice! good job dude

  8. #6
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Leaving my controls as default name on my exams when i was studying last 3 years would be instant negative, and would need to repeat exam... actually most of what you typed there would make it negative. It was a must. And also commenting was really a must. (This in C# btw)

    Plus i don't see much logic calling a button -> cmdSomething. Usually i call it btnSomething. It would also be confusing because of combo box's which i name them cmbSomething. (This in C# btw)

    Anyway, its good.
    Last edited by 'Bruno; 07-07-2010 at 02:14 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  9. #7
    Blubb1337's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Germany
    Posts
    5,915
    Reputation
    161
    Thanks
    3,108
    Quote Originally Posted by Brinuz View Post
    Leaving my controls as default name on my exams when i was studying last 3 years would be instant negative, and would need to repeat exam... actually most of what you typed there would make it negative. It was a must. And also commenting was really a must. (This in C# btw)

    Plus i don't see much logic calling a button -> cmdSomething. Usually i call it btnSomething. It would also be confusing because of combo box's which i name them cmbSomething. (This in C# btw)

    Anyway, its good.
    Well I don't force you to use cmd, lol.

    cbComboBox
    cmdButton

    This is the way I do it.

    You can sure use

    cmbComboBox
    btnButton

    It is your decision



  10. #8
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Haha I still use cmd from the old VB6 days

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  11. #9
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Quote Originally Posted by Blubb1337 View Post
    Well I don't force you to use cmd, lol.

    cbComboBox
    cmdButton

    This is the way I do it.

    You can sure use

    cmbComboBox
    btnButton

    It is your decision
    exactly. ^^ it was just an opinion ;o anyway could even call radiobuttons tits.. oO

    btw you could add to that list about creating functions for some repetitive code. :\ thats one of the best ways to clean code.. and people usually don't care much about it... but in bigger projects.. its a pain..

    it usually happens, eg: when accessing a database (people usually repeat the very same code a lot with it)
    Last edited by 'Bruno; 07-07-2010 at 02:26 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  12. #10
    Abstract's Avatar
    Join Date
    Mar 2009
    Gender
    male
    Location
    Look in your refrigerator
    Posts
    3,626
    Reputation
    54
    Thanks
    980
    Good tutorial man

  13. #11
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Quote Originally Posted by Brinuz View Post
    exactly. ^^ it was just an opinion ;o anyway could even call radiobuttons tits.. oO

    btw you could add to that list about creating functions for some repetitive code. :\ thats one of the best ways to clean code.. and people usually don't care much about it... but in bigger projects.. its a pain..

    it usually happens, eg: when accessing a database (people usually repeat the very same code a lot with it)
    Last topic of the thread is subs/functions? /

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  14. #12
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Quote Originally Posted by J-Deezy View Post


    Last topic of the thread is subs/functions? /
    nop not that.. it's about repetitive code... instead of always having for example the very same 5 lines of code or w/e to access a database on 20 different parts of them program, you could easily make a func.
    Or for example instead of having the very same Cicle to take a values from a BD in 10 different parts of a program... make a func.

    you see what i mean?

    btw don't take me wrong, that tutorial it's really good and one of the most helpful tuts i have seen on mpgh.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  15. #13
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Quote Originally Posted by Brinuz View Post
    nop not that.. it's about repetitive code... instead of always having for example the very same 5 lines of code or w/e to access a database on 20 different parts of them program, you could easily make a func.
    Or for example instead of having the very same Cicle to take a values from a BD in 10 different parts of a program... make a func.

    you see what i mean?

    btw don't take me wrong, that tutorial it's really good and one of the most helpful tuts i have seen on mpgh.
    But that's exactly what the last part of the tut describes?

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  16. #14
    'Bruno's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    Portugal
    Posts
    2,883
    Reputation
    290
    Thanks
    1,036
    My Mood
    Busy
    Quote Originally Posted by J-Deezy View Post


    But that's exactly what the last part of the tut describes?
    not quite the same imo... or maybe im expressing myself wrong? oO

    edit: Actually i have looked to it now with more attention oO i though he was talking about something else and not about repetitive stuff. Anyway..
    Last edited by 'Bruno; 07-07-2010 at 02:59 AM.
    Light travels faster than sound. That's why most people seem bright until you hear them speak.

  17. #15
    omanel's Avatar
    Join Date
    Mar 2010
    Gender
    male
    Location
    Portugal
    Posts
    93
    Reputation
    10
    Thanks
    4
    My Mood
    Inspired
    What's a boolean, what's it used to?
    Yeah, I suck lol

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Improve your reaction
    By Ferris Bueller in forum CrossFire Tutorials
    Replies: 13
    Last Post: 06-07-2011, 09:16 AM
  2. [Tutorial] HOW TO IMPROVE YOUR COMPUTERS FPS AND IN GENRAL!
    By Death217 in forum CrossFire Tutorials
    Replies: 8
    Last Post: 03-27-2011, 05:19 PM
  3. [Request] Mechanical share your code
    By deadman027 in forum CrossFire PH Discussions
    Replies: 13
    Last Post: 10-11-2010, 06:13 AM
  4. How Can I Improve This Code ?
    By kelechi96 in forum Combat Arms EU Hack Coding/Source Code
    Replies: 8
    Last Post: 08-02-2010, 10:51 AM
  5. Do you take drugs to improve your gameplay?
    By CAPTAIN OBVIOUS in forum General
    Replies: 23
    Last Post: 04-11-2008, 09:48 PM