New Programming Practices
New Programming Practices
It's been about a year since I've done any coding and I think it's time to get back to the habit. I've downloaded FBv0.18.3b and have quickly noticed a few changes, such as all variables must be explicitly declared and there's no longer support for variable suffixes.
I used to use the $ suffix to denote string variables as it helped make my code easier to decipher. With this feature gone, I'm curious to know what practices you use to easily identify a string variable in your code.
-Eclipzer
I used to use the $ suffix to denote string variables as it helped make my code easier to decipher. With this feature gone, I'm curious to know what practices you use to easily identify a string variable in your code.
-Eclipzer
I think some coding practices follow a convention of adding a suffix to the variable like:
strMyVar = string
intMyVar = integer
However, I usually name the variable something that is easy to remember like:
UserFirstName = string
UserTotalPoints = integer
Once you progress past the a$, b$, C%, etc., it really doesn't matter if you use a type suffix or not.
strMyVar = string
intMyVar = integer
However, I usually name the variable something that is easy to remember like:
UserFirstName = string
UserTotalPoints = integer
Once you progress past the a$, b$, C%, etc., it really doesn't matter if you use a type suffix or not.
-
- Posts: 5494
- Joined: Sep 12, 2005 20:06
- Location: California
I find those extremely annoying, however.I think some coding practices follow a convention of adding a suffix to the variable like:
strMyVar = string
intMyVar = integer
Newer programming techniques ask you what the variable actually is used for. Descriptive variable names and explicit type-checking helps. Note, if you don't like FB's explicit variable declarations, you could compile with -lang QB as an option on the command-line, but I don't recommend it.
Even moreso in object oriented programming this occurs, where you're likely to use an object's API a lot and simply work with methods.
I tend to find that I'll use quite a few temp variables when I code. One thing I always liked about QB was not having to explicitly declare all those temp variables which may only be necessary for a few lines of code.
Let's say I have a looping structure with a temp variable only required within the loop. This variable gets updated within the loop. I read the FB will reduce this variables scope to the loop if declared within the loop. However, it seems it also reinitializes the variable as well. Is there a way to declare a variable within a loop but have it retain its value throughout the course of the loop?
Again, var will not increment properly here because of the dim within the loop. Also I noticed that done produces an error in the line "loop until done" because it's scope is reduced to within the loop. It would be nice to keep these declarations within the loop if possible so that it would make manipulating these types of code blocks easier in the future.
-Eclipzer[/code]
Let's say I have a looping structure with a temp variable only required within the loop. This variable gets updated within the loop. I read the FB will reduce this variables scope to the loop if declared within the loop. However, it seems it also reinitializes the variable as well. Is there a way to declare a variable within a loop but have it retain its value throughout the course of the loop?
Code: Select all
do
dim as integer done, var
var+=1
loop until done
-Eclipzer[/code]
-
- Posts: 5494
- Joined: Sep 12, 2005 20:06
- Location: California
'ere's how I'd do it. Static keeps the variable within scope, but retains its value and memory.
Code: Select all
Dim As Integer done
Do
static as integer vars
vars+=1
Loop Until done
-
- Posts: 2428
- Joined: Jul 19, 2006 19:17
- Location: Sunnyvale, CA
- Contact:
Hi Eclipzer, have you been gone that long!?? ;-)
I think this string name is fairly self-explanatory:
Dim Shared As String game_level_title
There is a related thread here:
What're YOUR Naming/Commenting Conventions?
BTW, Your website is gone? (o.O) ;-)
I think this string name is fairly self-explanatory:
Dim Shared As String game_level_title
There is a related thread here:
What're YOUR Naming/Commenting Conventions?
BTW, Your website is gone? (o.O) ;-)
-
- Posts: 5494
- Joined: Sep 12, 2005 20:06
- Location: California
You could create a local variable with an explicit scope block:
Code: Select all
/' ... '/
scope
var n = 0
do
print n : n += 1
loop while n < 3
end scope
/' ... '/
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
Static variables will only be initialised once, regardless of how many times the loop is encountered. If you encounter the loop a second time, which is quite possible if you use it in a sub/function, or an outer loop, the variable will still have the same value as it did last time you touched it.
You might be able to use static or shared to achieve what you want, but they are just hacks. They weren't meant for that sort of thing.
I strongly advise against trying to use hacks to declare the variable within the loop - instead, I suggest just declaring it beforehand.
You might be able to use static or shared to achieve what you want, but they are just hacks. They weren't meant for that sort of thing.
I strongly advise against trying to use hacks to declare the variable within the loop - instead, I suggest just declaring it beforehand.
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
If you're using For...Next loops you can declare it directly in the loop:
But obviously that won't work for normal loops. In normal loops, yeah, you basically have to declare it beforehand. I usually create one or two variables for that, and re-use the same variables in loops.
I think I like anonymous1337's method better, since I prefer to keep things within scope as much as possible. For a beginner like yourself, however, that might not matter as much.
Code: Select all
For i As Integer = 1 To 10
'...
Next i
Code: Select all
Sub something
Dim a As Integer
a = 0
Do
a += 1
Loop Until condition
'...
a = 0
Do
a += 1
Loop Until condition
'...
End Sub
-
- Posts: 5494
- Joined: Sep 12, 2005 20:06
- Location: California
Err, I don't think Eclipzer's a beginner. He's probably one of the more knowledgeable persons programmingwise in this forum. Maybe a newb in regard to the newer FB quirks but given maybe a week, you'll see a lot of stuff from this guy you won't believe possible.notthecheatr wrote:If you're using For...Next loops you can declare it directly in the loop:
I think I like anonymous1337's method better, since I prefer to keep things within scope as much as possible. For a beginner like yourself, however, that might not matter as much.