New Programming Practices

New to FreeBASIC? Post your questions here.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

New Programming Practices

Post by Eclipzer »

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
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

I usually rely on the variable name,

var text = "..."
var filename = "..."
var body = "..."


or a comment if I feel it's appropriate.
steven522
Posts: 265
Joined: May 27, 2005 13:02
Location: Alabama, USA
Contact:

Post by steven522 »

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.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

I think some coding practices follow a convention of adding a suffix to the variable like:
strMyVar = string
intMyVar = integer
I find those extremely annoying, however.

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.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

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?

Code: Select all

do
  dim as integer done, var
  var+=1
loop until done
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]
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

'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
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

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) ;-)
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

That works perfectly. Is there anything I need to know about using STATIC?

KristopherWindsor: Yes, it's been a minute. I'm rebuilding though, so I should have a new site up soon. Thanks for the link.

-Eclipzer
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Eclipzer wrote:That works perfectly. Is there anything I need to know about using STATIC?
Yes. If you decide to do static as integer myVar = 10, myVar will only be initialized the first call.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

Hmm, also even though the variable is local to the loop, it's value is retained even after the loop ends. If the loop were to be entered again it would not reset. Is there a way for the variable to be removed or reset once the loop completes?

-Eclipzer
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

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
/' ... '/
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

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.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

If you're using For...Next loops you can declare it directly in the loop:

Code: Select all

For i As Integer = 1 To 10
  '...
Next i
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.

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
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.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Eclipzer: What you're asking to do completely destroys the entire point of scope :P
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Post by relsoft »

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.
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.
Post Reply