Computer Science Canada Coding Style |
Author: | wtd [ Thu Jul 14, 2005 7:11 pm ] | ||||||||||||||||||
Post subject: | Coding Style | ||||||||||||||||||
Introduction I'm not going to speak from a podium, because I've certainly been guilty of bad practices both in the distant past and recently. That out of the way, let's look at some bad practices, and what we can do to improve them. Variable names need meaning All too often we use abbreviated variable names. Such names make it immensely difficult to discern the meaning of the variable, and why it was created. This extends to variables as simple as loop counters. I recently used the classic "get 10 grades" example in another tutorial so I'll use it here. Bad:
Good:
Declare variables only when you need to Bad:
Good:
Don't reinvent the wheel if there's an existing library function to do it for you Bad:
Good:
Short code is good code Bad:
Good:
Or:
Contribute Feel free to discuss these and suggest other good practices with regard to coding style. |
Author: | md [ Thu Jul 14, 2005 7:28 pm ] |
Post subject: | |
Another big thing, no matter what your variable naming scheme; don't change it half way though a file! Chose something and stick to it. |
Author: | Hikaru79 [ Thu Jul 14, 2005 7:36 pm ] |
Post subject: | |
How about sticking to your particular language's naming conventions? In Java, for example, it is customary to use camel case for fieldAndMethodNamesLikeThisOne, except with a small first letter, using capitalized first letters in class names, and ALL_CAPS_WITH_UNDERSCORES for final variables (constants). It just make it easier to detect on sight what everything is when reading code, and it drives me crazy when someone sends me a file like test.java with methods like Getname(). |
Author: | wtd [ Thu Jul 14, 2005 7:40 pm ] | ||||||||
Post subject: | |||||||||
Respect the teeming masses Many languages have communities with accepted naming conventions. Embrace these naming conventions. For instance, the Java community has very specific style guidelines. Follow them. Use whitespace Bad:
Good:
Bad:
Good:
|
Author: | wtd [ Sat Jul 30, 2005 10:23 pm ] |
Post subject: | |
The Biggest Tip I Can Offer Don't look on these tips as a burden. Look at them and see how they'll make your life easier. |
Author: | [Gandalf] [ Mon Aug 01, 2005 5:20 pm ] | ||
Post subject: | |||
To me, having good looking, well syntaxed code makes coding easier and more fun. You don't look at something and say "omg I have to spend a day just to understand that persons coding". Here's something which bothers me. Even if you don't follow standards, follow your own standard! I really don't like it when people do things like:
Keep the spacing the same! |