Print Page | Close Window

Optimizations and Coding Standards

Printed From: Mirage Source
Category: Tutorials
Forum Name: Approved Tutorials
Forum Discription: All tutorials shown to actually work with MSE are moved here.
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=29
Printed Date: 20 December 2006 at 6:01pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: Optimizations and Coding Standards
Posted By: Sync
Subject: Optimizations and Coding Standards
Date Posted: 07 February 2006 at 5:26pm
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, http://www.google.com/search?sourceid=navclient-ff&ie=UTF-8&q=Visual%20Basic%206%20Coding%20Standards - 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.



Replies:
Posted By: Sync
Date Posted: 07 February 2006 at 5:26pm
Approved, and this is a very good read!




Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info