General Pointers


BeeHay

Recommended Posts

Hey again, I have a few questions I'm seeking that are more than likely opinion, rather than solid fact.

I'm getting into a rather large amount of variables, and wonder what your ideas are on private variables compared to global...

All of my variables (except a small few) are global. What do you think about going at things that way?

Would it be better to get rid a few globals and make them into V.Channels?

Also, I have a question about ending a sequence within the sequence it's running in.

Is that a good idea? :)

I have a sequence that when the variable is >0.009 it should end.

Would it be correct to do -

if(var<0.009)
  endseq(thissequence)
endif

It would be nice to stop on 0, but for some reason it never ends the sequence.

I think its because sometimes my var goes past 0.009 and 0 (into negative) very quickly.

Any pointers would be awesome! :)

Link to comment
Share on other sites

Lots of global variables are typically a bad idea and you should use privates whenever you can. The global namespace gets cluttered. Putting them as V channels helps a little, but then the V namespace gets cluttered. You might consider using a global class to do it. This is a non-documented feature, though we have made mention of it in several places in the forum. This will allow you to group your globals similar to how DAQFactory does it. Lets say you have globals called a, b, and c and you want to group them into Rig:

class CRig
   local a = 3
   local b = 6
   local string c = "abc"
endclass

global Rig = new(CRig)

now you access those variables using dot notation:

Rig.a = 5

Rig.b = 12

etc.

This is a real basic use of classes. If, for example, you had 3 rigs, all with the same sets of variables, you could reinstantiate the class 3 times:

class CRig
   local a = 3
   local b = 6
   local string c = "abc"
endclass

global Rig1 = new(CRig)
global Rig2 = new(CRig)
global Rig3 = new(CRig)

Now it becomes Rig1.x, Rig2.x, etc where each is a separate variable. Of course you can have other classes for other variable sets and it basically works the same way. Just remember that a class definition (the class xxx to endclass script) doesn't actually create the variables. You have to do the new(classname) to instantiate the class and assign it to a variable.

There's a lot more too it, but this is a good start for you.

As for sequence stopping, you can actually do "endseq(thissequence)", but its probably better to just to "return"

Link to comment
Share on other sites

Hmm, I'll have to try out a few class groups, sounds like it should clean it up a little.

I haven't tried return yet, does it kick the loop back up to the top?

Only have seen it used on the making of a password protected button in your help file.

Thanks!

Link to comment
Share on other sites

Really whatever you can manage. If you are organized, I don't see why you couldn't have thousands of globals. Channels are after all global in scope as well and there are many applications with a thousand channels. DAQFactory kind of forces you to use a number of globals because screen components access the global scope. The problem with too many globals is making sure you don't have name collisions. In other words you don't create a global variable that has the same name as a channel or sequence or internal DAQFactory function / object. You can get around this a bit by coming up with your own naming convention. So, instead of using classes as I described, group similar variables with a prefix like rig1_temperature and rig2_temperature. I personally name all my globals with a g prefix to differentiate between globals and privates.

There are a number of other general programming issues with using globals, mostly related to scalability and the ability to componentize your apps that only really apply to more advanced DF apps. However, still keep in mind if you use a lot of globals in some code that you expect to reuse in other DF apps you may create collisions when you go to paste the code into the new app.

Now for all the programmers out there that are cringing at the words "global variable": the use of globals are easier than using classes and the many other mechnismes available to avoid them. For the smaller applications that most users of DAQFactory are creating the issues of globals don't really apply, and its worth it to make the development much easier.

Link to comment
Share on other sites

  • 5 years later...

No they do not affect the global namespace.  Class declarations are stored in a separate table accessible only from the new() function (and as a parent).   I personally name my sequences "createXXXclass" or "createMiscClasses" that ways its clear that the sequence is just to create a class.  I recommend against putting any other script in a sequence with class declarations.  Whenever you change a function in a class, you have to rerun the sequence to update the class declaration.  If you have script outside class declarations in a sequence, then that code will execute every time you do this.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.