Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing and Coding Standards
#1
Originally posted by BigRed

Well, I've recently looked through the MSE Build 1, and even went as far as to go through 3.0.3 and 3.0.7 and I have to say, none of these follow any of the basic coding standards that most programmers know. So, I'm going to give you some tips to help optimize your projects a little bit. Some of these are only fractional speed differences, but when it comes to creating a project, you typically want as much speed as you can possibly get.

So for the first part, push CTRL+F, and replace all of the following:

String( -> String$(
Chr( -> Chr$(
Trim( -> Trim$(
LTrim( -> LTrim$(
RTrim( -> RTrim$(
Hex( -> Hex$(
Space( -> Space$(
CurDir( -> ChrDir$(
Dir( -> Dir$(
Left( -> Left$(
Right( -> Right$(
Mid( -> Mid$(

If I missed any, let me know. If you want the reasoning behind this, well its as simple as, the $ functions return the function as a string instead of a variant. Typically you don't decalre ANYTHING as a variant unless you are unsure what data will be passed through the function. So if you are currently using any variants and didn't know what the difference was, change them now. Now, if you are using the Chr$() function to pass things such as a tab, or a new line, replace them with the vb equivilent, (vbTab, vbCrLf, etc). Those are there for a reason.

Next, Option Explicit, you've seen it, you most likely have no idea what it does, so you remove it to see if there's any difference, you most likely notice no difference whatsoever so you leave it off, and never use it, ever. *Slaps hand* BAD! Very Bad! Never remove the Option Explicits, and when you create any type of object, ALWAYS add it to the top. What it does is if any variables are used in any sub or function and it is not declared in any way, then it returns an error. Option Explicit is your friend. I'll use a Mirage example, say you are getting a playername. You would use something along the lines of St = Player(Index).Name. Well Say, you accidently typed it like this St = Player(Idnex).Name. Well guess what, it wouldn't work. If you had Option Explicit, it would tell you what is wrong, and if you didn't it wouldn't. See it is and always will be your friend.

Now Mirage does an ok job of prefixing the modules and such, but if you go off and create your own project, a typical standard is to name all objects with the proper prefix (frm = Form, mod = Module, cls = Class, col = Collection, etc) This is just a typical standard, and you should even follow it when name the objects on a Form (cmd = Command, lbl = Label, pic = PictureBox, etc)

This next standard is ignored even by the most experienced programmers, because of a common belief that it doesn't matter how you delcare it because it will do it for you. All constands should be declared as something. As String, As Long, As Byte, As Integer, As ColorConstants, etc. Always declare it to reflect what it is being used to store. No matter what anyone says, just follow the standard, it will in the long run, be better for you.

A few short standards now. Never, ever, use Dim outside a form. Dimming is form level variables. Public/Private/Global is for all others. This does not apply to Subs/Functions, just variables that are in the Declarations. Never declare anything As New. It is not needed and hinders preformance. Besides, when you do Set Variable = New clsWhatever. You do it there anyway. Never use the Public declaration of variables outside of a module.

This is just a small list of the standards that you should follow at all times, in any project you make in Visual Basic. If you want a full list, Google it. Feel free to add on, to the list, or even add optimizations to some of the code used in MSE. I think if I ever feel like it, I'll go through and add some of my own optimizations to the MSE code. For startes, if you want to attempt this on your own, find anything that uses bitblt and remove it. Odds are, you loaded the image into a secondary picturebox and you are using the exact same graphics that are already loaded into memory. For example, some people posting things to show the sprite on login or character creation using bitblt. Get rid of it and change it to DirectX. Very simple and you don't have the same images loaded a hundred times into memory. I also believe that the NPC editor and the Mapeditor both used bitblt. Change them as well.
Reply
#2
grimsk8ter11 Wrote:String( -> String$(
Chr( -> Chr$(
Trim( -> Trim$(
LTrim( -> LTrim$(
RTrim( -> RTrim$(
Hex( -> Hex$(
Space( -> Space$(
CurDir( -> ChrDir$(
Dir( -> Dir$(
Left( -> Left$(
Right( -> Right$(
Mid( -> Mid$(

If I missed any, let me know.

Sorry if this is considered to be necroposting, but the bolded comment urged me to post. >.>

I believe the LCase and UCase functions also have $ counterparts:
LCase( -> LCase$(
UCase( -> UCase$(

When I'm typing them up in VB6, I type LCase( and it says "LCase(String)." When I add the $ it changes to "LCase$(String As String) As String." Same thing for UCase. I just thought I should point that out. >.> Sorry if I should've PMed this instead.
Reply
#3
Burn him!!! :twisted:

Haha, its not necro posting, any post contributing to the discussion or adding new usefull information is welcome Smile. Plus its not that old, and we like optimization. So thanks for the info, hopefully grim will update his post.
Reply
#4
Yay! I'm helping!

Well... looking through the VB6 object browser, looking at the list, I can see a few more...

Date -> Date$
Time -> Time$
ChrB( -> ChrB$(
ChrW( -> ChrW$(
Command -> Command$
Environ -> Environ$
Error -> Error$
Format(-> Format$(
LeftB( -> LeftB$(
MidB( -> MidB$(
Oct( -> Oct$(
Right( -> Right$(
RightB( -> RightB$(

There are a crapload more. I just kind of went down the list in the VB6 Object Browser and typed up the ones I saw that weren't already on the list. And all of those were from not even going halfway through the Object Browser's list of global properties/methods. Granted, I'm not sure how many of those are actually used in MS... but I guess it's good to keep a list around just to be sure. XD
Reply
#5
Just becareful when replacing all of this, like the LenB function for example, it doesnt work exactly like the Len function, so it could cause errors if not used correctly.
Reply
#6
LenB, just divide the result by two to get true result

If you want it even faster multiple by 0.5, since multiplication is faster then division.

Or course, I dont actually know if lenB * 0.5 is still faster then len but at least lenB is still faster if your just checking if a space is blank


Oh yea, heres a good site that'll give you ideas on how to optimize your code to the max
http://www.aivosto.com/vbtips/stringopt.html
Reply
#7
Heh, I missed a lot. But then again, many of those are very unlikely to be used. Oh well, just in case right?

Oh and funky, great find! Wish I could edit my post to add this stuff, but looks like grim needs to do it.
Reply
#8
I think I found it using stumbleupon plugin for firefox ages ago, had it bookmarked for months

To be honest, i'm suprised how many usful sites stumbleupon has led me to, I've had to stop bookmarking now cause my bookmark folders are overflowing lol
Reply
#9
Len is actually 1/2(Lenb) Tongue

http://www.quadrantwars.com/optimizations.htm

http://www.aivosto.com/vbtips/stringopt.html

also, never swqithc completely to LEnB xD it screwes everything up in MS lol. as well as Space$ and a few others xD
Reply
#10
Mkdir works too
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)