?
Solved

Good vs Streamline (when not same) DEBATE wanted

Posted on 2003-03-08
10
Medium Priority
?
234 Views
Last Modified: 2010-05-01
No real clue what to address 'question' as in title, but I want some expert inputs and get some discussion going on this.  I want to get some discussion/debate going on a topic that's been at the back of my head for a long time, as I want to produce the most efficient code I possibly can, like everyone, but I also want it to follow good coding practise advice at all costs.  While there is usually a solution that fits both requirements (may require some rethinking to find it), sometimes I think they can be diametrically opposed.

Good coding practise: Encapsulate everything, privatize all variables as much as you can, make them public only when necessary, control access carefully.  Use globals sparingly but under controlled conditions.  Scope is critical; control scope always. Option Explicit, avoid using ByAny in API declarations, and always specify the data type, using varients only where unavoidable.  Documenting exhaustively, breaking every job down as far as possible into generic parts that can be easily reused later.

Efficient coding practise (wrt performance/speed): Streamlined algorithms, minimum function calls, minimized stackjumping (function calling a function calling a function).  Using LONG instead of integer when doing integer math.  Offsetting as much code from a form into a module as possible so the form loads faster.

---
First, a related but not my main question example: The long/integer and similar variables.  If I want to loop through only 10 items, declare as an integer/long or a byte?  Byte seems more logical, less memory occupied.  But does it handle well in the code, or is it more expensive to use?  If I just want to loop through an array of 10 items and add them together or debug.print the values, integer or long?  Theres no 'math' going on with it is there, not like adding or multiplying, so would an integer variable still undergo int->long internal conversion?  When would an integer declaration be an integer, and when is it rearranged into a long internally?  THIS IS NOT A DEBATE ISSUE, as there must be a factually correct answer.

The main point of debate:  Encapsulation of functional variables within a procedure/function vs exposure.  I'm not talking about a variable that would have a good reason to be a globally defined variable, but rather a good handful of integer, string and boolean variables that are the workhorses of several codes.
Example: I write an elaborate code that processes a string dynamically that is being created in a textbox, ie the user is entering a DNA code and I translate 3 putative protein sequences from the input.  The string is broken into blocks of 3 characters, each block of 3 corresponds to an amino acid, but there are 3 translation frames:  So ..CATCATCATCAT.. = ..CAT+CAT+CAT+CAT or ..C+ATC+ATC+ATC+AT.. or ..CA+TCA+TCA+TCA+T..
With every keystroke (which has an elaborate code for keystroke validation), three possible decodings of this dna are presented in real time, and since I allow cursor movement and cutting and pasting to occur, I have to process the string from start to finish each time, not just the last few characters.  YES, it would be much simpler to just have a command button do all the work at one time when finished, but there are reasons for this requirement.

Now my procedures and functions to decode the sequence are going to be called with every keystroke, 30 to 60 times on average.  Each call to a procedure results in a dozen dim statements for index variables, characters (string, string*1), etc.  And a a whack of function calls to translate the three-char block in each string, three times; each function call, dim another half dozen variables.

With all those workhorse variables encapsulated into each procedure/function {i,j,k indices; ch, tmpstr, codon strings; flag1, flag2 boolean, etc), forcing them only to be in the scope of the function/sub, can this be good?  Each new keypress, the cycles start over again.  
The constantly DIMming new local variables for each call to a procedure: isn't it costly to keep creating and destroying variables repeatedly especially when they will be done so back to back to back frequently?  

Now, not that my code is so slow I can see a difference between processing a 5 char string and a 65 char string, or even really perceive any delay in my inputting, so measurable effects are insignificant.  I'm talking about theoretial effects, inferring from this case to an extreme case.

Does there come a point where the benefit of scope confinement/encapsulation and the security/clarity that it offers can be outweighed by the cost of the variable creation/destruction and stack operations?  IS it better in such a case to declare a set of variables in a module/form such as wh_i, wh_j, wh_k, wh_ch (for workhorse), created only once for the module, but manipulated by whatever procedure needs to manipulate them?  Or whScanI and whTranslateJ, to make it clear that one variable is to be used in one procedure/function only, thereby never worring about the scope of a variable i, j or k?

--
Discussion points: Which method would you prefer to use?  Which would you prefer to debug of someone elses?  If you did adopt the 'single creation' of some workhorses approach, what guidelines would you try to set/follow?  Am I completely wrong, and on a technical scale, there would be no difference (actual operational difference, not percieved difference)?

Points will be divided at some point between some of the best arguments.  And points to the integer/long/byte index variable answer separately...
0
Comment
Question by:GPrentice00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 6

Author Comment

by:GPrentice00
ID: 8096341
I will keep this question open until ~ March 20 at which point final points will be awarded.
0
 
LVL 3

Accepted Solution

by:
Diveblue earned 120 total points
ID: 8096815
G-,
  Statistically, 80% of the cost of an application is related to maintenance. The arguments for good Object Oriented programming practice are many, but experience tells me that well designed systems are:

     -Easier (and therefore less costly) to debug
     -More adaptable to changes in business rules
     -Easier to extend.
     -Less susceptible to the introduction of bugs
      in places other than the location of a change
     -Have longer lives in production environments, and therefore a higher ROI.

Which method would I prefer to use?
Which would I prefer to debug of someone elses?  

Without doubt, the one that was coded using best practices. We don't call them "Best Practices" for nothing

"If you did adopt the 'single creation' of some workhorses approach, what guidelines would you try to set/follow?"
I can't recommend an approach that I would use.

  Am I completely wrong, and on a technical scale, there would be no difference (actual operational difference, not percieved difference)?

Actually it is hard to tell without testing your specific code both ways. But If the difference is imperceptible to the end user, is it truly there? (Kind of like the tree in the forest)when speed is an issue, I try to tune my algorithms or create a dll in C.

If you are concerned with the allocation/release of memory, you could keep your variables local and dim them as static (just make sure for proper initialization). Bear in mind this is likely to confuse the next person to look at your code.

As far as your question about Byte vs integer vs. Long:
  I know that in most math functions longs are faster than integers. I have never tested with byte.

Hope this helps.


0
 
LVL 46

Expert Comment

by:aikimark
ID: 8098659
1. To increase the performance of functions and subroutines, allocate their variables as static.  The variables are localized to the procedure, but are only allocated and deallocated once (first use and program termination).

2. It seems that you may also benefit from a detection of the textbox's SelStart value in the change event.  You might be able to save yourself some processing by starting the calculation from the point of change, not from the begining of the string.

3. You may also retain the prior contents of the textbox for left-to-right comparison as your change detection method, rather than the SelStart property, since this may be simpler.

4. If performance is a real issue, then you may consider generating all three string variations within the same procedure and then passing the result to the function(s).

5. String concatenation is a notoriously poor-performing operation in classic VB code.  (it is better in VB.Net using the StringBuilder class)  Consider alternatives:
  a. Work with byte arrays (see the StrConv function)
  b. Create a string 'buffer' of the total length of your string and use
Mid$(strX, lngPosn, lngLen) = "string expression"
to change the contents of the string without causing VB to go through storage allocation, memory copy, and storage deallocation.

6. Allocate variable storage in the calling routine and change/use the variables in the invoked routines.  Remember that sub/fcn parameters are ByRef.
________________________________________________
I'm not entirely sure what you want to get by asking this question in the manner you've asked it.  Do you actually have a problem?  Do you anticipate having problems?  Is the design work ahead of you or already completed?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Author Comment

by:GPrentice00
ID: 8099222
to clarify

my code is working okay, there are no functional problems with it, and it seems to work faster than I can perceive it working.  But I was wondering if doing it the way I was would eventually start to bog down.

I had not thought really thought about the use of static variables, as I've always connected them to wanting to hold a value between calls to the function.  And since I'd be using workhorse variables that will be reinitialized each time, they wouldn't be static values.  But I see that declaring them as such would have the effect of creating only one spot in memory to be the workhorse and not clearing/recreating it back to back constantly.

Question then becomes, is the declaration of a static variable for such a purpose 'kosher' with good coding practise as their use could be made pretty clear, or would a debugger scratch his head too much over why STATIC was used?

As for long/integer/byte -- all integer data gets internally converted into a long data for math functions, which is why long works better than integer for math functions.  But, is a simple loop considered math?  
If looping only 10 items, and if both long and integer could work identically, then the favor would be on the smaller variable integer (less memory allocated).
But how does a byte compare to an integer?  Less storage?  MORE - stored differently?  How would it compare to an integer in a loop operation?

When there are no math operations performed on the value, how is it used?  Is a loop considered math or does a for/next treat a variable differently than x=x+1?


0
 
LVL 46

Expert Comment

by:aikimark
ID: 8099716
When you do something out of the ordinary, such as using Static variable declarations, document it.

Not all (small) integer variables get converted to long/single/double data types when used in math expressions.  It depends on what is required.  There is NO data type conversion when an integer is used in a FOR...NEXT loop, unless you are assigning starting and stopping values that aren't integer.

The smaller memory footprint isn't as important as the speed of the (small) integer operations.

Unfortunately, a byte data type isn't as fast for math operations, since it is ALMOST ALWAYS converted for math expressions.
0
 
LVL 6

Author Comment

by:GPrentice00
ID: 8184876

I guess thanks are in order for contributing, it didn't turn out as I had hoped, but your comments are valid.

I will put in a request for point sharing.
0
 
LVL 6

Author Comment

by:GPrentice00
ID: 8184898
AIKIMARK -- please post a "hi" reply at the following question to get your points

DIVEBLUE -- you'll get points as well when the moderator drops the value of this question or nullifies it for me, and I'll post for you specifically.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 8185212
Per request in http://www.experts-exchange.com/Community_Support/Q_20559294.html the points in this question have been reduced from 135 to 30 for the purpose of enabling a split.

Netminder
EE Admin
0
 
LVL 46

Expert Comment

by:aikimark
ID: 8185582
GPrentice00,

Post a "hi" at what question?

Thanks (in advance) for the points.
0
 
LVL 6

Author Comment

by:GPrentice00
ID: 8186429
http://www.experts-exchange.com/Miscellaneous/Q_20543709.html

Sorry - was sure that I had pasted that in before!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question