electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
Cleaning up my code with the help of sporks....
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [9 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Mon Dec 17, 2007 10:43 am    Post subject: Cleaning up my code with the help of sporks.... Reply with quote  Mark this post and the followings unread

Hi guys,

I've finally finished my semester so I can carry on from where I left off in September Very Happy

I'm currently trying to clean up my code and have different functions stored as separate .ck files.

The problem I have encountered with this is hitting a brick wall when it comes to sharing variables across each individual file.

For example if I have one process that does the following in one text file called loader...

Code:


// This is loader.ck

0 => int x;
x + 1 => x;
Machine.add( "printx.txt" ) => int printx;
500::ms => now;



and then go on to load that variable in another sporked process (printx), it simply doesnt work

Code:


// This is printx.ck

//Have to declare x again so I dont get any errors when sporking printx
//but this just resets x to zero and this way x does not retain its value
//from loader

int x;

while (true)
{
   1000::ms => now;
   <<<x>>>;
}



Can anybody sh(r)ed any light on this particular problem.

In the meantime I hope everyone is having a merry Christmas!

Thanks for your time during this festive season,

Rhys
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Dec 17, 2007 10:59 am    Post subject: Reply with quote  Mark this post and the followings unread

Yes, that's right, it's a namespace issue.

All shreds that come from a single file can share the same namespace, just like "normal" functions share the namespace of the shred they are in unless something with the same name gets defined inside of the same function. Different files mean a different namespace, even if they get Machine-added by another file.

The only way to get around this for multiple files is currently a public class holding static data. This is indeed less then convenient.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Mon Dec 17, 2007 11:23 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks for clearing that up, I couldnt work out why this was the case.

In your opinion, what would be an alternate way to neaten up pages and pages of code that share variables?

I already have them running in separate functions but as one loooong text file.

Thanks

Rhys
Back to top
View user's profile Send private message
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Mon Dec 17, 2007 2:55 pm    Post subject: Reply with quote  Mark this post and the followings unread

Quote:


The only way to get around this for multiple files is currently a public class holding static data. This is indeed less then convenient.



Thinking along these lines....

Does this mean that the stored static data could be put into another process have its value changed by that process and then put back into the public class that it was taken from so that another process could then receive the updated value for the data.

Or is it just static in the sense that it cant be changed full stop.

Thanks again,

Rhys
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Dec 17, 2007 3:27 pm    Post subject: Reply with quote  Mark this post and the followings unread

Dr. Spankenstein wrote:

Does this mean that the stored static data could be put into another process have its value changed by that process and then put back into the public class that it was taken from so that another process could then receive the updated value for the data.


Yeah, "shared" would be a better name, perhaps. It's "static" amongst instances, not in time. Not just data though, you can have a static event as well which can be used to send data from one file/shred to another.

As for your cleanup; as I see it code doesn't become "clean" by simply spreading it out over files (not saying you wanted to do that!). The most important part will be sorting it into conceptual chunks. For example, it's often a good idea to put data structures in a class and give that class functions that manipulate that data. Also a good idea is to look for structures that you use a lot and re-write those as a single function. Another nice trick is that if you use -say- 10 parallel SndBufs you can also turn those into a single array of type SndBuf. There are likely more tricks you can perform, it heavily depends on what you are doing and how. I think the most important thing is to have a clear image of your programs structure and make sure the actual program is also structured that way.

As I see it a single huge file doesn't *need" to be a mess if you section it and label the section, for example a large article is fine as a big single file. Single files also mean you can search the whole thing in one swoop.

One structure you could use if you have turned your file into a clean structure using -say- classes is that you could make a file that would machine.add() several files containing just a public class followed finally by a last file that would contain the actual program which uses those classes. This could be quite clean but it also means that editing will be spread out over several files which may get confusing. As you can't redefine public classes (once defined they stay put) it also means re-starting the VM a lot during development. How convenient or inconvenient either option is really depends on what you are doing.

I'm sure some people will disagree with some/all of what I said, there are many ways to approach this problem and like with sorting CD's or driving or doing the dishes everybody has a style that he thinks best.... however if you are unhappy with the structure of your code it may indeed be time to re-think it and the above is how I look at it. Personally I don't use classes unless I can point out specifically what sort of "thing" it is, why it makes sense to look at that as a single "thing" and if it makes things more simple. I don't think using classes inherently gives code a cleaner structure, with functions it is easier to tell (at least for me).

It may also be worthwhile to document the structure you pick. Using classes can make a program very well structured but that doesn't mean somebody encountering it will immediately see that structure. This may be you in a year's time...

If you do go with static data in public classes it's probably worthwhile to look into the forum and list archives about some catches in instantiating that data as this is a bit messed up right now. Do shout if you get stuck there.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Fri Dec 21, 2007 6:15 am    Post subject: Reply with quote  Mark this post and the followings unread

So I had a little search on the forum for "static data" and "public classes" but I couldn't find anyway of sharing the data between shreds that were invoked using machine.add.

How would I go about having a variable say "x" (just to be boring) and sharing the value of "x" with other shreds that are in seperate .ck files that themselves, have been (sporked) added by the main shred?

Is this even possible, the way I'm looking at it?

Thanks,

Rhys
Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Fri Dec 21, 2007 9:12 am    Post subject: Reply with quote  Mark this post and the followings unread

Well, a "public class" gets defined across the whole VM. As soon as it's defined all shreds that enter the VM can use it, regardless of how they come into being (spork, machine.add() or manually, etc). I think that's in the manual but if it isn't it really should be as it's important.

So; a static int in a public class is available *to* every shred that wants it and the same *for* every shred and changeable *by* every shred.

A public class of a certain name (because of this) can only be added once and never changed as long as the VM lives.

Hope that helps.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Fri Dec 21, 2007 10:12 am    Post subject: Reply with quote  Mark this post and the followings unread

Yup, that makes sense...

I've implemented it as follows and all works so far:

Code:


public class Metro
{
   130 => float MainTempo;      
   4 => float MainNumberOfBeatsInBar;   
   public float gettempo()
   {
      return MainTempo;      
   }
   public float gettsignature()
   {
      return MainNumberOfBeatsInBar;   
   }
}

Back to top
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Sat Dec 22, 2007 6:07 am    Post subject: Reply with quote  Mark this post and the followings unread

I really think the float values themselves need to be static. Not so sure what static functions referring to values that may be different for every instance will do, I seem to remebember they may throw a error.

In ChucK you don't need the get functions, BTW, the you can directly refer to the member variables though of course you can use them if you think it may save confusion and bugs.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [9 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use