[RESOLVED] Misc Variable Questions
Hi all,
A few questions came up while I was dabbling in C#:
When naming variables (I guess they're called data now) is it still best practice to use: frmMain, txtUserName, strTemp, intMaxUsers, etc?
I declared my VB6 variables at the beginning of my functions/subs, in the C# code I have seen they are declared where ever needed. What is the best practice for this? I liked having the variables at the beginning of the function, it made it easy to tell what variables I had.
If variable are embedded within a function do you normal try to dispose of them (if you can) as soon as you don't need them anymore or do you just leave them alone until you get out of the function?
Thanks Steve
Re: Misc Variable Questions
There is no rule saying you should use txt and so on in front of your controlls in C#. But I tend to do it no matter what language I use, because I find it easy to read. But when you are working on big projects it is not always you that desides. So if you work on a big project, and people tell you not to do it, then of course you can't do it. But as long as you are just writing code for your self. I can't see any reason not to do it. Better safe then sorry.
"For every coder, there is a coder that doesn't like his coding style." - Greg Snook
When you say disposing variables. I guess you mean objects. Yes it is a good idea to call dispose when you are done with it. An other smart thing to do is to use the Using statement when ever it is possible. Then you are sure that it will be disposed when it should. An other thing to notice is that if you make a class, and you have member objects that is derived from idisposable, then you need to dispose these member variables before your object is disposed. If not, then the GC will never dispose your object at all.
If you are wondering about a few techniques on how to look for memory leaks in C# and Visual Studio. Then I suggest you take a look at this short movie. It should give you some hints.
http://go.microsoft.com/?linkid=3325538
Hope that helps a bit. Have a good evening Mr.
- ØØ -
Re: Misc Variable Questions
Microsoft recommends not using Hungarian notation (prefixes that indicate type) on variables and also declaring variables when they're needed rather than all at the start of a method. I've adopted these recommendations and I do prefer them, but it's all a matter of personal preference, as NoteMe says. The thing I really hate is when people prefix a class name with "cls". Hungarian notation is for variables, not types. The other thing I hate is the use of the "obj" prefix. It tells you nothing so what's the point? Steer clear of those two and I'll still be your friend. :D
With regards to Disposing, the idea is to release resources so it is a good idea to do it as soon as possible. In a short method it's not really a big deal, but if an object might live for hours unused, then you should probably Dispose it as soon as you know it is no longer needed.
Re: Misc Variable Questions
I have to agree with jmcilhinney on the use of hungarian notation on buildt in datatypes and objects. But I don't fully agree on not using the using statement when you have the possibility. Tom Miller have stated secera, times that it is good practice to use it. So I will continue to use it, and continue to suggest that others uses it.
- ØØ -
Re: Misc Variable Questions
Since no-one else answered this bit
Quote:
Originally Posted by steve65
I declared my VB6 variables at the beginning of my functions/subs, in the C# code I have seen they are declared where ever needed. What is the best practice for this? I liked having the variables at the beginning of the function, it made it easy to tell what variables I had.
If you declare them all at the beginning of the function, then it is a lot easier to find them and see exactly what they are when you come across them in the code. But then, if you declare them inline then it saves a few lines of code, and it looks cool :) Up to you.
Re: Misc Variable Questions
Quote:
Originally Posted by penagate
Since no-one else answered this bit
What do you call this then:
Quote:
Originally Posted by jmcilhinney
Microsoft recommends... declaring variables when they're needed rather than all at the start of a method. I've adopted these recommendations and I do prefer them, but it's all a matter of personal preference...
Re: Misc Variable Questions
Quote:
Originally Posted by jmcilhinney
What do you call this then:
An oversight :)
Re: Misc Variable Questions
Quote:
Originally Posted by penagate
An oversight :)
:lol: :lol: :lol:
Re: [RESOLVED] Misc Variable Questions
Thanks for all of the info!
Re: [RESOLVED] Misc Variable Questions
I stumbeled over this tutorial on coding standards in C# and .NET. Pretty ok. Woudn't say I agree 100%. But no one that has coded for a while would agree with someone anyway. We all have our small charming syntax styles.
Well for the tutorial:
http://www.dotnetspider.com/Technolo...Practices.aspx
Enjoy
- ØØ -
Re: [RESOLVED] Misc Variable Questions
Not bad.
Three points from that one that I strongly agree with:
- Never ever assume things. That is, your code should be as abstract as possible.
- Never catch general exceptions, or in fact any exceptions unless absolutely necessary, otherwise you just end up hiding mistakes.
- Write comments only when necessary to explain the code, and strive to write code that requires no commenting ;)
I used to comment my code all the time, now I never do. If it's not self-explanatory I re-write it until it is.
Re: [RESOLVED] Misc Variable Questions
Just one more comment about comments..:) It is nearly as obvious that you don't think about it, but it is really easy to mess up like this. The rule is:
"It is better with no comment then a wrong comment"
Explenation:
- Choose your words carefully when commenting
- If you change a function for some reason after a while, it is easy to forget to change the comment. Leading to a comment that will only confuse.
- If copy pasting a function because it is very similar. Never copy the comment. It will be to easy to forget to change it.
Just as a reviel by my self. I often comment my code after I have written it... :blush: Like for D# now, it is only the classes that is 90% + done that has comments at all...and classes that I have stoped working on that I won't work on again before in a few weeks. Then it is also easy to forget what actualy happens...
- ØØ -
Re: [RESOLVED] Misc Variable Questions
Thank you for the info on the web site it helps a lot. Reading through the site though has me questioning if I am doing something right in my code. If you have the time could you take a look at this thread?
Thanks Steve