Where do you DIM your vars!?

I declare them as local as possible, best with an assignment.
That way I avoid a lot of potential bugs.

One thing that was a challenge for me at first with Xojo was having to declare all variables. Now I always put the dims right at the start at the module where i’ll use them, with comments above the key ones so I know what they’re for. It has actually helped my debugging process a lot over the old VB6 “use a variable where you want” methodology.

There’s no right answer or wrong answer. Unless you DIM at the top of the method, which is wrong answer.

:wink:

I dim them at the top. What happens if a variable is dim two times? I can’t test, I’m not at home because of work. I suppose Xojo show it as an error.

And I never give my variable a single lettre name like i or j. Their names are iNbr jNbt tText NameTxt dDate FirstLdate
Then a short descrition and a abreviate of the kind.

Getting into a method is like getting into the country through customs: do you have anything to declare? Dim it all up front, and nobody gets hurt. It’s a long standing convention that I don’t feel like changing. There’s one place to look. But keep it to a minimum and keep the methods short. Factor that code. Factor, factor, factor.

Sigh. I’m going to have to fight this battle all over again with the new hires. “I know its a little odd at first, but if you stick with the coding conventions, it works better in the long run.” I’ve been spoiled with my long standing team. If you look at a section of code, you can’t tell who wrote it. We just all code the same. It makes things so easy.

I guess it’s karma. When I was the new guy, I questioned every convention. Now that I’m the boss, bringing on fresh blood, I get to fight the battle from the other side of the table. Yay me!?!?

For me I generally put variables at the top if for no other reason that I hate some being at the top and others sprinkled throughout. More than likely every method needs at least one, so theres always something at the top. Why not put the rest there? Obviously limited scope variables like in For loops and such are contained within the loop.

I make a strict distinction between application specific code and general re-usable “library” code in my large applications.

Almost all my application specific methods are short, so they fit completely into the editor on my 17" screen:

  • I declare variables as late as possible because it makes the code more readable
  • I use very long descriptive variable names.
  • No comments at all.
  • Influenced by programming in Objective C.

For complex and algorithm-heavy library functions, which may have hundreds of lines I do the opposite:

  • I declare all variables at the top.
  • I use short variable names.
  • Everything is heavily commented.
  • Influenced by reading and adapting C code.

Colleagues looking at my code probably think that I’m actually a two person company…

I do both. I decide where the most readable place is for the DIMs.

Does not matter as long as you know where what is.

It is probably far easier to dim next to the first use, but if the variable is reused further down, less easy to track.

Firstly, congrats on achieving the “Boss” level in your company to begin with, Tim … seeing your posts throughout time here doesn’t surprise me that you’d excel within any company given the knowledge and logic you display with your numerous helpful and insightful interactions.

Just think, Tim … It only gets better … when you get to be the “Owner”, then you’ll get the privilege to fight the battles from all sides (and sometimes all at the same time too) and be responsible for the outcome no matter how much or little you actually influenced it. It tends to put the final nail in the coffin of the urban legend of “the higher you go (in an organization), the easier it gets”. And just think, we aspire and fight like hell to reach that “place” … which kinda reminds me of that line from the country music song, “What was I thinking?” ^^

Thanks, Don. On October 1 of this year, Owner and Boss happened at the same time. And, yes, I am already asking myself, “What was I thinking?!?!” That, and “What just happened?!?!”