Main Area and Open Discussion > General Software Discussion
Good coding conventions - Discussion
Josh:
OK, a little background on the reason behind this post.
I have recently started teaching myself Java in an effort to start dabbling in Android development. I am using the Eclipse IDE in combination with the Sam's Teach Yourself Java in 21 days - Covering Java 7 and Android book.
As a course of habit, I find myself skimming each chapter so that I can see what things I might possibly miss by assuming I knew all of the material (yes, even variable types. I actually learned something reading through the types). This is where the discussion I want to have arises from.
When coding, I know common coding conventions such as coding indentation for readability, CONST/Finals being written in ALL CAPS in order to identify them as such. I was wondering, what other practices do each of you follow as you code and which are considered industry best practice?
Renegade:
Ah... I see someone is looking to start a war! ;D
Pascal for this, camel for that, CAPS for another, and _these get underscores, while strThose get a pretty prefix, and so do btnThese. :D
Object.toString() or Object.ToString() or Object.tostring()?
I stick mostly to C#, Java, C and VB naming conventions. I like a bit of this and a bit of that. e.g. I like private field names with an _underscore and camel case:
_somePrivateFieldName
I'm not fond of:
m_something
I like public properties in Pascal case:
SomePublicPropertyName
I also like method names in Pascal case.
I like controls in Hungarian:
btnGoButton
pbxBigPictureBox
wbrMainWebBrowser
lblInputLabel
txtTextbox
etc.
While this is short:
if (condition)
DoSomething();
I prefer:
if (condition
{
DoSomething();
}
It's just easier to see/read when skimming. I'm not usually very fond of:
condition ? DoSomething() : DoNothing();
It has its uses, but often it's harder to read. Sometimes it is easier... depends... No right answers as far as I can see. Verbose and terse both serve a purpose.
I think following conventions is more important when you work in larger teams, and especially when other people, that you will never meet, end up using/modifying your code.
I do like lots of comments though.
Reading on variable types really is essential for a language though. Some languages do things that you would NEVER expect with some types, and as you're reading through, you end up going, "Ah... now I see what that didn't work..." They usually all make sense in their own contexts. Just different flavours. e.g. If you allocate memory for an integer but don't assign a value, what is the value? zero? null? Both are valid assumptions. Same for a string. Is it null or empty?
Ath:
+1 on all Renegade said :up:
@Josh, when working with Eclipse, I always use the default format layout (Ctrl-Shift-F), as it's a Java-layout standard on it's own, only I've made line and comment widths a bit wider then the default 80 characters, 160 is usually most optimal.
Aligning variable declarations does make source more readable, so that's an option I tend to enable for projects with larger sources as well.
wraith808:
another +1 on what renegade said. But in the end, I think it's a matter of audience and consistency when dealing with any project rather than slavish attention to anyone else's rules.
Other than I hate the rule we have at work with no underscores! so private variables are thisIsAProperty and public ones are ThisIsAProperty. I know they did it because we should be using autoimplemented properties now... but in some cases you can't and that rule sucks!
TaoPhoenix:
Ah... I see someone is looking to start a war! ;D
Pascal for this, camel for that, CAPS for another, and _these get underscores, while strThose get a pretty prefix, and so do btnThese. :D
...
I stick mostly to C#, Java, C and VB naming conventions. I like a bit of this and a bit of that. e.g. I like private field names with an _underscore and camel case:
_somePrivateFieldName
I'm not fond of:
m_something
-Renegade (December 30, 2012, 11:41 AM)
--- End quote ---
Heh I'm no coder, so allow me to be a pest as a Humanities type intruding where I don't belong! ;D
I like CamelCaps aesthetically. I hate Under_scores. : )
Navigation
[0] Message Index
[#] Next page
Go to full version