[TUT]Improve your Coding[VB] - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19) +---- Thread: [TUT]Improve your Coding[VB] (/showthread.php?tid=16219) Pages:
1
2
|
[TUT]Improve your Coding[VB] - ThePrinCe - 02-17-2011 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. 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. Code: '[color=#32CD32]comment [/color] Use your datatypes correctly Code: Dim int as integer = "5" Integer <> String. Therefore the correct way is: Code: Dim int as integer = 5 Shorten your code Instead of writing: Code: txtResult.text = txtResult.text + int Code: txtResult.text += int Using Code: Dim str as IO.Streamreader = New Streamreader("C:\test.txt") Code: Using str as new Io.Streamreader("C:\test.txt") Case instead of If Code: If txtResult.text = "a" Then Code: Select Case txtresult.Text If-Structures If condition Then [statements] ElseIf condition Then [statements] Else [statements] End If Code: If textbox1.text = "HakerDz" Using Case... Code: Select Case Textbox1.Text Code: If Textbox1.text <> "" then 'if it's not empty If not... Code: If not textbox1.text = "" then 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. Structure Code: Dim firstPos as point Code: Structure meh Booleans Code: Dim b as boolean 'by default the boolean is FALSE Code: If b = true then Code: if b then 'true Try-Catch To avoid your program from closing when an error appears, you can use Try-Catch Blocks. Code: Try However, do not use them if they are not necessary. Code: Try Regions You can use Regions to make your code more clear. Code: #Region "Settings" Good Luck , i hope you will like IT.!
HakerDz
RE: [TUT]Improve your Coding[VB] - MoonWalker - 02-17-2011 That was helpful. I'm learning VB currently, will keep this is in mind. Thanks for sharing btw. RE: [TUT]Improve your Coding[VB] - Knight - 02-17-2011 While I don't understand much about code, I learned a little bit from this. Thank you. RE: [TUT]Improve your Coding[VB] - Dïṉøṡαώṡ_ - 02-20-2011 Thanks a lot for this, I am not the best VB coder but I've totally realized how important it is to stay organised and clean whilst coding simple programs. Thanks a lot for the share, I've bookmarked this so when I am next making a application I'll revert back to this before finishing it to clean the code up a little. RE: [TUT]Improve your Coding[VB] - Untouch - 02-20-2011 Not bad and everything seems correct. Nice post. I like to use if's rather than case. RE: [TUT]Improve your Coding[VB] - Fragma - 02-20-2011 Yeh I tend to use If more than Case aswell. Other than that I do everything you mentioned here. Good post. It can make so much difference cleaning up your code, not only performance wise, but also just so that you know where things are, and what they do. You can end up confusing yourself if you have lots of code. What I tend to do is work step by step, and then I'll group various parts together using Regions. For example if I had a few functions, I'd group them all together into a Region named "Functions", same for Timers, Declarations etc.. RE: [TUT]Improve your Coding[VB] - Emily - 04-14-2011 I didn't really get the code. This helped me a bit but I do not understand the code. RE: [TUT]Improve your Coding[VB] - HerpinDerp - 04-15-2011 Thank you, very useful! RE: [TUT]Improve your Coding[VB] - panDa. - 04-16-2011 Some of the basic codes, but helpful. Thanks for sharing. ^_^ RE: [TUT]Improve your Coding[VB] - ThePrinCe - 05-07-2011 welcom Guys , Thank's for The Feedbacks. |