Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals
· Free Stuff
· Contests and Sweepstakes
· Black Friday 2013
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 01-19-2013, 04:18 PM   #1
Chaotic42
Lifer
 
Chaotic42's Avatar
 
Join Date: Jun 2001
Location: Mississippi Gulf Coast - 16RBU
Posts: 29,335
Default Coding styles for C-family languages

I was looking around online about various coding styles since I'm actually taking a proper programming class instead of trying to patch together various code from various sites to get something working. I don't really have a style, so I figured I'd read up on what people thought was best and why. I'm finding some interesting debates, so I thought I'd ask you guys if there were any specific idioms (I guess that's the right word) that you all use on C, C++, C#, etc.

The one thing that I found interesting is that people use spaces for indentation instead of tabs. Since I thought this was the entire point of the tab key and since it's customizable in virtually every editor, it seems like a no-brainer to use the tab key. What's your take?

Also, any thoughts on "Camel Case" vs using tons of underscores? I really like the underscore thing for variables but tend to use Camel Case for objects, since that's how most of the code samples I've seen do it.

What are your favorite idioms and what other idioms really bug you?
__________________
Complex numbers are all fun and games until someone loses an i.
Pictures
Chaotic42 is offline   Reply With Quote
Old 01-19-2013, 05:16 PM   #2
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 10,497
Default

Quote:
Originally Posted by Chaotic42 View Post
The one thing that I found interesting is that people use spaces for indentation instead of tabs. Since I thought this was the entire point of the tab key and since it's customizable in virtually every editor, it seems like a no-brainer to use the tab key. What's your take?
An old, old debate . Theoretically everyone should use tabs, and then you could set your local indent to what you want it to be. In practice, for whatever reason, it rarely works that way, and instead you end up with your code structure getting mangled on different machines. People who prefer all spaces (me, for one) would rather just have a set format based on the character width.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 01-19-2013, 05:49 PM   #3
mv2devnull
Senior Member
 
Join Date: Apr 2010
Posts: 742
Default

Herb Sutter has published an opinion or two about style and idioms. You may or may not disagree with him.

Personally, I don't care.
mv2devnull is offline   Reply With Quote
Old 01-19-2013, 07:48 PM   #4
Merad
Golden Member
 
Join Date: May 2010
Location: NC USA
Posts: 1,822
Default

Main main rules are:

Class/struct and function names ShouldBeLikeThis
Prefix virtual function names with a V
Macros and preprocessor symbols SHOULD_BE_LIKE_THIS
Variable names shouldBeLikeThis
Prefix member variables with m_
Prefix global variables with g_
Prefix static variables with s_
Prefix constants and enum values with k_
Other than the above do not use hungarian
Braces always go on a new line
Always use braces, even when optional
All documentation should be Doxygen compatible
Try to keep things to < 80 characters per line

That said you must be flexible. Different languages have different "standard" styles. If I'm writing Java, I write java style. And unless you're the boss, in the real world the odds of you getting everyone you work with to write in your preferred style is just a hair above 0.

Quote:
The one thing that I found interesting is that people use spaces for indentation instead of tabs. Since I thought this was the entire point of the tab key and since it's customizable in virtually every editor, it seems like a no-brainer to use the tab key. What's your take?
As already said, it just doesn't work out that way. Never really bothered to figure out why. I use 2 spaces per tab.

Quote:
Also, any thoughts on "Camel Case" vs using tons of underscores? I really like the underscore thing for variables but tend to use Camel Case for objects, since that's how most of the code samples I've seen do it.
Waste_of_space. And it's actually more difficult to read than camelCase IMO.

Quote:
What are your favorite idioms and what other idioms really bug you?
The one that irks me most is pointer and reference notation. The * or & is part of the type. There's no logical reason to separate it from the the rest of the type name (eg use int* instead of int *). But that's a losing battle.
Merad is offline   Reply With Quote
Old 01-19-2013, 08:28 PM   #5
iCyborg
Golden Member
 
Join Date: Aug 2008
Posts: 1,007
Default

Quote:
Originally Posted by Chaotic42 View Post
The one thing that I found interesting is that people use spaces for indentation instead of tabs. Since I thought this was the entire point of the tab key and since it's customizable in virtually every editor, it seems like a no-brainer to use the tab key. What's your take?
It might not be tab key per se. Most of modern editors support "replace tab with spaces". You can use tabs for your work, but it will not affect others or other editors. At work, I'm actually required to do this. I have 0 problems with this, and it makes sense if you're not the only one working on something.

I've already expressed my opinion on Camel Case etc. here, maybe more than once...
iCyborg is offline   Reply With Quote
Old 01-19-2013, 10:00 PM   #6
douglasb
Diamond Member
 
Join Date: Apr 2005
Posts: 3,156
Default

camelCase for instance variables. CAPITALS_WITH_UNDERSCORES for constants. UpperCamelCase for object properties and methods.

Lots of my coworkers use Hungarian notation but IMO, it is a dated practice and pretty much worthless with modern IDEs. Not to mention, it makes the code harder to read IMO.
douglasb is offline   Reply With Quote
Old 01-21-2013, 07:32 PM   #7
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

http://google-styleguide.googlecode....k/cppguide.xml
Although, as always, style consistency is pretty important too.
degibson is offline   Reply With Quote
Old 01-21-2013, 08:27 PM   #8
Chaotic42
Lifer
 
Chaotic42's Avatar
 
Join Date: Jun 2001
Location: Mississippi Gulf Coast - 16RBU
Posts: 29,335
Default

Quote:
Originally Posted by degibson View Post
http://google-styleguide.googlecode....k/cppguide.xml
Although, as always, style consistency is pretty important too.
Wow, Ice Weasel rendered that beautifully. it's just a block of text with no whitespace.

Wky 'k' for consts and enums? Thanks for the replies, everyone.
__________________
Complex numbers are all fun and games until someone loses an i.
Pictures
Chaotic42 is offline   Reply With Quote
Old 01-21-2013, 09:07 PM   #9
Net
Golden Member
 
Join Date: Aug 2003
Location: Maryland
Posts: 1,502
Default

generally you should follow the style of the language that you are working in and be consistent.

there are a lot of great books about writing clean code that focus on style.

more general see http://en.wikipedia.org/wiki/Coding_conventions

java http://www.oracle.com/technetwork/ja...nv-138413.html
C# http://msdn.microsoft.com/en-us/libr.../ff926074.aspx
...

Last edited by Net; 01-21-2013 at 09:21 PM.
Net is offline   Reply With Quote
Old 01-21-2013, 10:04 PM   #10
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

Quote:
Originally Posted by Chaotic42 View Post
Wky 'k' for consts and enums? Thanks for the replies, everyone.
Well, enums are compile-time constants, so it does sorta follow that enums and compile-time constants would have a similar style suggestion.

As for why kSomething is used instead of SOMETHING for compile-time constants, it's to differentiate from MACROS.
"Until January 2009, the style was to name enum values like macros. This caused problems with name collisions between enum values and macros"
degibson is offline   Reply With Quote
Old 01-22-2013, 03:02 PM   #11
Fallen Kell
Diamond Member
 
Fallen Kell's Avatar
 
Join Date: Oct 1999
Posts: 3,705
Default

I would read some of this article, and of course grab the code it is talking about and take a look. The author has some really good points about the code style used.

http://kotaku.com/5975610/the-except...3s-source-code
__________________
We were all warned of MS problems many years ago; remember how the Magic 8 Ball always said: "Outlook not so good"?
Server
HTPC
Fallen Kell is offline   Reply With Quote
Old 01-22-2013, 10:03 PM   #12
Staples
Diamond Member
 
Join Date: Oct 2001
Posts: 4,148
Default

I use

Tabs
camelCase for variables
Classes, Namespaces and Methods start with capital letters

I rarely use underscores although some languages like Python seem to prefer them.
Staples is offline   Reply With Quote
Old 01-24-2013, 02:17 PM   #13
Chaotic42
Lifer
 
Chaotic42's Avatar
 
Join Date: Jun 2001
Location: Mississippi Gulf Coast - 16RBU
Posts: 29,335
Default

What about project organization in general? I've been working on a program which has a class that is getting kind of large. I'm splitting it up into multiple files:

ClassName.Function1.cs
ClassName.Function2.cs

and so forth. Is this good tradecraft? It seems easier to work on than just having a single class file with several hundred lines of code.
__________________
Complex numbers are all fun and games until someone loses an i.
Pictures
Chaotic42 is offline   Reply With Quote
Old 01-24-2013, 03:35 PM   #14
Scooby Doo
Senior Member
 
Scooby Doo's Avatar
 
Join Date: Aug 2006
Posts: 775
Default

I guess it would depend on the size of the functions. No sense in having tons of little 5 line functions in seperate files, it would make visual scanning annoying.
__________________
Asrock Z77 Extreme 4
i5 3570K with DH14
G.Skill 2x8GB 1600
Corsair HX750
Cheappie Nvidia 610 :-(
Antec 902
Scooby Doo is offline   Reply With Quote
Old 01-24-2013, 03:36 PM   #15
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 10,497
Default

Quote:
Originally Posted by Chaotic42 View Post
What about project organization in general? I've been working on a program which has a class that is getting kind of large. I'm splitting it up into multiple files:

ClassName.Function1.cs
ClassName.Function2.cs

and so forth. Is this good tradecraft? It seems easier to work on than just having a single class file with several hundred lines of code.
I assume this is c#, and you're using partial classes? They are a strange feature, and I'm not sure I like seeing them used this way. They're sort of a hack, imo, to allow the combination of generated and edited code in one class at compile-time, which was necessary originally for ASP.NET and then for WPF/XAML, etc.

I guess there's nothing really wrong with decomposing this way, although if one class is really getting that larger my inclination would be to look at refactoring it, rather than splitting it up. Perhaps it is doing the work of several classes that can be composed.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 01-24-2013, 03:37 PM   #16
Train
Lifer
 
Join Date: Jun 2000
Posts: 12,695
Default

Quote:
Originally Posted by Chaotic42 View Post
What about project organization in general? I've been working on a program which has a class that is getting kind of large. I'm splitting it up into multiple files:

ClassName.Function1.cs
ClassName.Function2.cs

and so forth. Is this good tradecraft? It seems easier to work on than just having a single class file with several hundred lines of code.
I would take that naming convention as a code smell telling you that your functions are way too large.

And then your classes are probably too large as well.
__________________
.
Train is online now   Reply With Quote
Old 01-25-2013, 10:05 AM   #17
Munashiimaru
Junior Member
 
Join Date: Jan 2013
Posts: 23
Default

Quote:
Originally Posted by Merad View Post
Main main rules are:

Prefix member variables with m_
Prefix global variables with g_
Prefix static variables with s_
Prefix constants and enum values with k_
Disclaimer: Most of the below is in regards to java/c# (netbeans/eclipse/VS). Especially with naming anything that's more loosely typed, it is going to be a different beast. If you're doing something like windows API calls in C++, you're going to definitely want something like hungarian notation.

Depending on the IDE doing what's quoted tends to just be extra clutter. Most modern languages/IDEs it's very easy to distinguish these things with a mouseover. Only time I use any sort of type notation (hungarian is what I use) in names is when I'm declaring stuff in a GUI/webpage and then dealing with it in another class where it becomes an immense convience to have all checkboxes prefixed with ckb (or whatever you define in your stands) (especially in VS not so much in Eclipse but still somewhat good).

My personal coding standards are: Never abbreivate unless it's a business abbreviation (any abbreviations used should be defined in the requirements documention ideally). Put some thought into your function/class/variable names. If you're using single letters (besides for indexes) or names like temp, 99% of the time there's a better name that can be used. Declare variables in the highest part of the scope that it's used. You don't want variables that are floating around; it then has to be decyphered where and only where it gets used by later programmers. In the same vein, avoid reusing variable names; it almost never saves you any real memory/performance due to how garbage collection works and makes a place that needs to be decyphered like having variables in a scope where they're useless. For example, a string is immutable. Using the same temp variable 30 times over doesn't save you any processing or memory. Reistantiating objects at best generally just leads to a tiny amount of memory available to be garbages collected microseconds sooner (but it probably won't be).

Always use small functions/classes (prevents ginormous naming and immensely helps readbility). I'm a little looser than each function doing strictly one thing, but never more than one plus other function calls. Rule of thumb is functions shouldn't be much more than a page height and classes no more than 2-3, but that's a very loose rule.

Vertical white space is an asset you spend to get readibility. Using it to seperate logical pieces helps a ton with readibility, but if you use too much it makes it hard to take in what's going on in the overall flow of code.

Comment all classes/functions/global variables with autodocumenting code (/** in eclipse and /// in VS). That way people can get a description of these things with a mouse over. Comment any underlying code that's using some funky logic to get things done (in a perfect world this wouldn't happen but it does especially when dealing with code out of your control). Everything else should be explained by using good names/keeping classes/functions small.

Sticking to the above makes it so that a lot of extra clutter other people tend to add becomes obsolete.

On tabs, when I was using notepad to write java I used spaces. On any decent, IDE you should be using tabs. Basically if tabbing is likely to be a mess don't use them otherwise do.

I never use underscore. It's just added clutter especially for classes/variables where camel/pascal case is the norm. I usually just all cap constants without underscores, but that's really a debatable thing to do.

The number one rule though is to follow the coding standards of the project you're working on even if it's different from what you like. Don't just say screw it I'm doing my own thing whether it fits in or not.
Munashiimaru is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 02:02 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.