-
May 29th, 2009, 10:29 AM
#1
[RESOLVED] Local arrays in methods
A variable declared in a method gives me a place to store a value. If it is an array, it must be instanciated with new and is initialized to 0 by the compiler. Does the compiler do that at each call?
The alternative is global declaration at class level. But a lot of those variables, only used in some small method somewhere, not even used to keep values between calls, would impair the readability of the program.
So what would be good programming style for small, local arrays in methods?
-
May 29th, 2009, 10:52 AM
#2
Re: Local arrays in methods
Originally Posted by hreba
A variable declared in a method gives me a place to store a value. If it is an array, it must be instanciated with new and is initialized to 0 by the compiler. Does the compiler do that at each call?
The alternative is global declaration at class level. But a lot of those variables, only used in some small method somewhere, not even used to keep values between calls, would impair the readability of the program.
So what would be good programming style for small, local arrays in methods?
There is nothing against to create them at every method call as long as you do not need them outside this method.
In contradiction I would say.: Never create any field for arrays, structures, values, classes outside of a method, when you only need them in the method. This can only bring up confusion and unreadable code
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
June 1st, 2009, 02:56 AM
#3
Re: Local arrays in methods
I back Johny's opinion.
In addition to your question: if the variable (the value it holds) is local to the method, you have to always initialize it regardless it is defined as temporary in method, or "global" in class. Unless absolutely neccessary, e.g. profiler points you to specific rountine, always preferre maintability before performance. And what's more, if you declare the variable on class level, you need to take care about synchronization if multithreading comes to play. Method level temporary variables are easier to handle with in multihreading.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
June 1st, 2009, 08:27 AM
#4
Re: Local arrays in methods
My original question referred to small arrays, that is a few bytes, and that case is clear now, thanks for the hints. On some of my projects however I work with images which are typically byte[1024*768] and I declare them globally. How would you handle these?
-
June 1st, 2009, 08:44 AM
#5
Re: Local arrays in methods
Originally Posted by hreba
My original question referred to small arrays, that is a few bytes, and that case is clear now, thanks for the hints. On some of my projects however I work with images which are typically byte[1024*768] and I declare them globally. How would you handle these?
This doen't depend on size. If that are Images they are classes and classes are reference Types. so your array never will cotain the pictures but only pointers to the real picture anywhere in the heap. Simple do it in an array which is declared at method level. If they need lots of loading time and you need to have them on screen very quick then you need to decide if you want to have a collection anywhere in the datalayer and you are maybe only accessing your datalayer then. So there is no 'general wrong' or 'always corrrect' solution. It depends on your design, how often the data needs to be accessed Loading time and all that. Maybe you are starting your program and while starting loading resources to a datalayer class in the background.
I think, there are sme books on the market regarding C# design patterns. Maybe take a look in such stuff which will always be helpful when doing design decisions before starting to code a program. If you invest some time in these sort of knowledge you will spare lots of time which sometimes are wasted when you find out a design is not workable after investedsome days into it.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
June 1st, 2009, 06:38 PM
#6
Re: Local arrays in methods
My question is whether an array declared in a method is allocated and initialized at each call anew and whether this overhead might justify a global declaration for large arrays.
-
June 1st, 2009, 07:12 PM
#7
Re: Local arrays in methods
Originally Posted by hreba
My question is whether an array declared in a method is allocated and initialized at each call anew and whether this overhead might justify a global declaration for large arrays.
And the answer is no. Write code for clarity first. *If* you find you have a performance problem, then optimize,m but in this case that would not even be an issue.
-
June 2nd, 2009, 04:34 AM
#8
Re: Local arrays in methods
Originally Posted by BigEd781
And the answer is no. Write code for clarity first. *If* you find you have a performance problem, then optimize,m but in this case that would not even be an issue.
I simple have to confirm BigED781
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
June 2nd, 2009, 07:35 AM
#9
Re: Local arrays in methods
Ok, thanks guys for your hints.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|