Static or Instance - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Static or Instance


  • Please log in to reply
4 replies to this topic

#1 Bernie

Bernie

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 03 June 2013 - 02:48 PM

I'm not sure this is the right place, but I don't see a forum for general programming issues. 

 

I'm new to Netduino, but write C# for a living. I'm noticing that all the examples run in Main() with everything tagged as static. My general practice is to have the constructor of the main class create an instance of it's self and run from there. Can I assume the examples run as static to keep it easy to understand, but there's no reason not to create an instance and run that way?

 

Thanks for any input.

Bernie

 



#2 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 03 June 2013 - 03:41 PM

I tend to create do as you suggest, as in spin up instances, that said I use a lot of static and readonly variables as it tends to make memory management easier. The microframework tends to make you think about memory managment and garbage collection more than the full framework does, but that isn't a bad thing and have seen improvements in my own full framework code as a result. Nak.

#3 Bernie

Bernie

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 03 June 2013 - 09:55 PM

Nakchak,

Could you say a few words about memory management and garbage collection that is needed on microframework?

 

I'm open to learning all I can.

 

Bernie



#4 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 04 June 2013 - 08:46 AM

My general practice is to have the constructor of the main class create an instance of it's self and run from there. Can I assume the examples run as static to keep it easy to understand

 

 

exactly this.  i find it ugly to make all static, and for me it looks odd and confusing.

 

about gc isnt much to say: it cares about it on its own. memory management: dont push more items in a queue than you remove and your fine :D



#5 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 04 June 2013 - 09:30 AM

I'm with NooM here, try and keep classes as self-contained as possible. Avoid scattering bits and pieces as statics in the main class. Gather and isolate things that belong together. In my opinon, the more globals, the worse the code in terms of clarity, change management, maintainance, porting, scalability, generalizantion, scalability, etc. Be aware of the limited amount of system resources, primarily RAM and processing power (your code is being interpreted at run time, there's no JIT here). Create late and destroy early. Be clever and figure out more efficient ways of doing calculations etc than you perhaps normally would. Optimize, use hash tables. Keep it short, lean and mean. Whenever possible, use event driven constructs (interrupts) rather than polling. Avoid tight loops as much as possible. Use multithreading concepts like producer/consumer relations between threads employing sempahores and mutexes. The scheduler does a really excellent job, sometimes almost magical. If you must do tight stuff, remember to yield once in a while in favor of other threads. Other than that, at least I don't take any particular explicit actions for memory management. In general, the GC normally does a better job when left in peace. Personally, I don't care much for IDispose. I mean, if you for example instanciate a driver class for an LCD, a switch or something like that - would you dispose of it run time? I know I wouldn't. Naturally, transient objects should be disposed of early, don't allow stuff to "build up" over time. EDIT: Of course, don't apply all of the above per sé but selectively when necessary, sensible and justified.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.