Hi!
My name is Erik. I have been programming for 22 years. Admittedly, I was 9 years old when I started and I don't have all that much "professional programming experience" under my belt, but I'm getting there.
Lately I've been repeatedly running into certain similar problems across all the programs I am writing. My hope is that I can start a discussion with some programmers who are more experienced than I, and get a few tips. I don't know where else to go, there is no one here at work I can ask and I don't know anyone who has the experience I am trying to tap in to.
I am not sure how much to put in one question. My hope is that someone can see what I am driving at and help me ask the questions I don't even know how to ask!
With that somewhat incoherent preamble, I'll begin.
How important is it to avoid global variables? I have worked hard not to use them, one because I have read again and again since I started that they are bad, and two because my programming experience has proven this out. Globals can work when you have two or three variables, but when you have 10 or 20 or 100, it's time to do something different. Yet, sometimes in my quest to avoid globals I run into difficulties finding an elegant solution.
For example, VB does not allow a function to return an array. That is, it can return a variant which contains an array, but it cannot return an array of variants (or longs, or strings, or anything else).
Well, first of all I have been avoiding variants, too, because they seem... well, wrong or sloppy somehow. So to avoid using a variant which contains an array, and avoid using a global variable, I set up the function to receive the array as a parameter, ByRef of course.
Then I run into another problem. The calling function doesn't know how big the array needs to be. For example, Let's say I write a class module which connects to a dictionary of words. The class module does all the work of managing the dictionary: provides methods to load dictionaries from files, add words, delete words, return all words which start with a string or contain a string or are anagrams of a string. It even builds its own 2-level large index to optimize speed. You get the idea.
Now, let's say it makes sense for one function in clsDictionary to "return" an array instead of a collection or a recordset or any other method of containing many values. If I declare an array in my calling code, I don't know how big it needs to be, so I Dim it as empty() and figure I can resize it as necessary.
Wrong. You can't ReDim an array in anything but the scope in which it was declared. You don't get a runtime error, but the code simply doesn't work, and VB help confirms that you can't resize arrays which are passed as parameters.
What is wrong with my thinking? Should I just use variants? Should I use globals? Should I mess around with RtlMoveMemory and VarPtr? Should I use collections or recordsets or classes?
If you do chip in to help me out, please understand that I am looking for high level answers. That is, you could give me an idea on how to do what I'm asking, but what I really want is a why. I guess I am asking for strategies, not tactics.
I think this is enough for one question. I was going to give quick bullets of my other 10 burning programming style problems but every time I tried it came out too long, so I'll leave those for another message.
-E
My name is Erik. I have been programming for 22 years. Admittedly, I was 9 years old when I started and I don't have all that much "professional programming experience" under my belt, but I'm getting there.
Lately I've been repeatedly running into certain similar problems across all the programs I am writing. My hope is that I can start a discussion with some programmers who are more experienced than I, and get a few tips. I don't know where else to go, there is no one here at work I can ask and I don't know anyone who has the experience I am trying to tap in to.
I am not sure how much to put in one question. My hope is that someone can see what I am driving at and help me ask the questions I don't even know how to ask!
With that somewhat incoherent preamble, I'll begin.
How important is it to avoid global variables? I have worked hard not to use them, one because I have read again and again since I started that they are bad, and two because my programming experience has proven this out. Globals can work when you have two or three variables, but when you have 10 or 20 or 100, it's time to do something different. Yet, sometimes in my quest to avoid globals I run into difficulties finding an elegant solution.
For example, VB does not allow a function to return an array. That is, it can return a variant which contains an array, but it cannot return an array of variants (or longs, or strings, or anything else).
Well, first of all I have been avoiding variants, too, because they seem... well, wrong or sloppy somehow. So to avoid using a variant which contains an array, and avoid using a global variable, I set up the function to receive the array as a parameter, ByRef of course.
Then I run into another problem. The calling function doesn't know how big the array needs to be. For example, Let's say I write a class module which connects to a dictionary of words. The class module does all the work of managing the dictionary: provides methods to load dictionaries from files, add words, delete words, return all words which start with a string or contain a string or are anagrams of a string. It even builds its own 2-level large index to optimize speed. You get the idea.
Now, let's say it makes sense for one function in clsDictionary to "return" an array instead of a collection or a recordset or any other method of containing many values. If I declare an array in my calling code, I don't know how big it needs to be, so I Dim it as empty() and figure I can resize it as necessary.
Wrong. You can't ReDim an array in anything but the scope in which it was declared. You don't get a runtime error, but the code simply doesn't work, and VB help confirms that you can't resize arrays which are passed as parameters.
What is wrong with my thinking? Should I just use variants? Should I use globals? Should I mess around with RtlMoveMemory and VarPtr? Should I use collections or recordsets or classes?
If you do chip in to help me out, please understand that I am looking for high level answers. That is, you could give me an idea on how to do what I'm asking, but what I really want is a why. I guess I am asking for strategies, not tactics.
I think this is enough for one question. I was going to give quick bullets of my other 10 burning programming style problems but every time I tried it came out too long, so I'll leave those for another message.
-E