Go to Post You should take in to consideration who you are putting on to the field, not what. - NorviewsVeteran [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-12-2007, 00:00
Capt. Quirk Capt. Quirk is offline
Registered User
AKA: Bill Mazzacane
FRC #1704 (Robotic Steelers)
Team Role: Mentor
 
Join Date: Aug 2007
Rookie Year: 2007
Location: SoCalif
Posts: 64
Capt. Quirk will become famous soon enoughCapt. Quirk will become famous soon enough
Programming style When to use Caps and Underscores?

I have been trying to find a document that explains the Capital letter and Underscore conventions for the standard C18 robot code. To help readability

Thanks
  #2   Spotlight this post!  
Unread 25-12-2007, 00:20
wt200999's Avatar
wt200999 wt200999 is offline
Texas Instruments
AKA: Will Toth
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2004
Location: Dallas, Texas
Posts: 325
wt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud of
Send a message via MSN to wt200999
Re: Programming style When to use Caps and Underscores?

Are you asking how you should do it or how they have it in the default code?
  #3   Spotlight this post!  
Unread 25-12-2007, 00:25
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: Programming style When to use Caps and Underscores?

I choose something like this:

Variables: Lower case with underscores. Lead with a letter.
Code:
int a_variable;
Functions: Lead with lower case letter, change words with caps.
Code:
x = getValue(y);
Macros: All caps.
Code:
#define    INPUT_A     5
Structures: Start with caps, underscore.
Structure instances: same as variable. (you could make this caps as well)
Code:
Motor_Controller left_side;
left_side->sensor = getEncoder(INPUT_A);

Last edited by Tom Bottiglieri : 25-12-2007 at 10:15.
  #4   Spotlight this post!  
Unread 25-12-2007, 00:45
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Programming style When to use Caps and Underscores?

The "classic" is the Indian Hill standard. A google of "Indian Hill" will yield hundreds of documents stemming from the IH standard and customized for a particular organization. Here is a good place to start.
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #5   Spotlight this post!  
Unread 25-12-2007, 01:26
wt200999's Avatar
wt200999 wt200999 is offline
Texas Instruments
AKA: Will Toth
FRC #3005 (Robochargers)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2004
Location: Dallas, Texas
Posts: 325
wt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud ofwt200999 has much to be proud of
Send a message via MSN to wt200999
Re: Programming style When to use Caps and Underscores?

I learned that whatever you do, stick to it. Don't jump around. I.E.

I like to do my variable names like this
Code:
int MyVariableName;
other like

Code:
int My_Variable_Name;
and I do the same with functions

any kind of #define is always all caps and underscores

I usually see macros as _CAPS
  #6   Spotlight this post!  
Unread 25-12-2007, 04:31
ay2b's Avatar
ay2b ay2b is offline
Registered User
AKA: Andy
FRC #2928
Team Role: Mentor
 
Join Date: Mar 2004
Rookie Year: 1994
Location: Seattle, WA
Posts: 211
ay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant future
Re: Programming style When to use Caps and Underscores?

Use whichever you prefer - just be consistent. The rule for the code at work is "make it match the rest of the file". Asking which to use is like asking any of these other questions:
- tabs or spaces for indenting
- two spaces or four spaces for indenting
- braces on the same line as the conditional or the following line
- "while" on the same line as the closing brase in a do...while or the following line

In other words, it's just a matter of style - use whichever you prefer; the compiler doesn't care. It will help you (and anyone else reading the code) if you are consistent, however. Maybe use underscores for variables and camel case (that's the "capsInTheMiddle") for functions; maybe do the reverse. Just make sure all the code looks the same.
__________________

2011 - SD Quarterfinalists (980), LA Quarterfinalists (980)
2010 - LA (2404) Finalists (980), AZ Motorola Quality (980)
2009 - LA Semifinalists (980); Las Vegas Quarterfinalists (980); SD (2404); IRI #1 Seed, Finalist (980)
2008 - SD Quarterfinalists (980), LA Champions (980), LA Rookie Inspiration Award (2404); CalGames Finalists
2007 - So.Cal Finalists (980), SD Quarterfinalists (980); CalGames Finalists
2006 - So.Cal Regional Champion (4), Toronto Judge's Award Day 1 (4)
2005 - SVR Champions, Delphi "Driving Tomorrow's Technology" (980); AZ Xerox Creativity (980); So.Cal Finalists, RadioShack Innovation in Control (980); Championship Archimedes Division Semifinalists; IRI Finalists (980)
2004 - So.Cal Regional Champions, Leadership in Controls (980); AZ GM Industrial Design (980); Championship Galileo Division #2 Seed; IRI Champions
2003 - PNW Semi-finalists (488)
2002 - PNW Finalists (488)
2000 - X-bot / 488 - Mentor / Founder
1994 - Sunny Delight - Driver - champion
  #7   Spotlight this post!  
Unread 25-12-2007, 04:39
ay2b's Avatar
ay2b ay2b is offline
Registered User
AKA: Andy
FRC #2928
Team Role: Mentor
 
Join Date: Mar 2004
Rookie Year: 1994
Location: Seattle, WA
Posts: 211
ay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant future
Re: Programming style When to use Caps and Underscores?

While it won't help with variable and function names, you might find the program called "indent" useful. It will take C/C++ source code and re-indent it to match your prefered style (as defined by command line switches, or a .indent file). It can modify all the common things that people argue over - tabs vs. spaces, number of spaces, where to put open/close brases, what column to put comments in, what declarations need to be lined up, etc.

(And if you ever work in perl, there's a similar program called "perltidy". I'm sure just about every language out there has such a program - you just have to know what it's called and find it.)
__________________

2011 - SD Quarterfinalists (980), LA Quarterfinalists (980)
2010 - LA (2404) Finalists (980), AZ Motorola Quality (980)
2009 - LA Semifinalists (980); Las Vegas Quarterfinalists (980); SD (2404); IRI #1 Seed, Finalist (980)
2008 - SD Quarterfinalists (980), LA Champions (980), LA Rookie Inspiration Award (2404); CalGames Finalists
2007 - So.Cal Finalists (980), SD Quarterfinalists (980); CalGames Finalists
2006 - So.Cal Regional Champion (4), Toronto Judge's Award Day 1 (4)
2005 - SVR Champions, Delphi "Driving Tomorrow's Technology" (980); AZ Xerox Creativity (980); So.Cal Finalists, RadioShack Innovation in Control (980); Championship Archimedes Division Semifinalists; IRI Finalists (980)
2004 - So.Cal Regional Champions, Leadership in Controls (980); AZ GM Industrial Design (980); Championship Galileo Division #2 Seed; IRI Champions
2003 - PNW Semi-finalists (488)
2002 - PNW Finalists (488)
2000 - X-bot / 488 - Mentor / Founder
1994 - Sunny Delight - Driver - champion
  #8   Spotlight this post!  
Unread 25-12-2007, 11:14
Guy Davidson Guy Davidson is offline
Registered User
AKA: formerly sumadin
FRC #0008 (Paly Robotics)
Team Role: Alumni
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Ra'anana, Israel
Posts: 660
Guy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to beholdGuy Davidson is a splendid one to behold
Send a message via ICQ to Guy Davidson Send a message via AIM to Guy Davidson Send a message via MSN to Guy Davidson
Re: Programming style When to use Caps and Underscores?

Note on correct indentation - if you use Eclipse, it can automatically correct indentations in code for you. Makes for much more readable code. I haven't tried it with Python yet, but there it could potentially mess around with things. the joy of Python

As for naming, pick a system that works for you, and to second, third, and fourth everyone else, STICK WITH IT. I usually go ALL CAPS for #define's, and myVariableName style naming for variables and functions.
  #9   Spotlight this post!  
Unread 25-12-2007, 14:18
Phalanx's Avatar
Phalanx Phalanx is offline
Formerly Team 1089 (Mercury)
AKA: Michael Reffler
FRC #5431 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 1999
Location: Lewisville, TX (previously NJ)
Posts: 384
Phalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond reputePhalanx has a reputation beyond repute
Re: Programming style When to use Caps and Underscores?

As others have stated there is no "official" style. Again as others have stated pick a style and stick to it. Being consistent throughout is a bigger benefit than it is usually given credit for.

There's an old saying that I found to be true...
"Programmers don't need to spell correctly, just consistently"

What we have opted for is below with a few exceptions.
lower_case_variable_name
UPPER_CASE_DEFINE_MACRO
Mixed_Case_Function_Call()
We indent everything at each level for clarity as well.

We've also adapted a philosophy of modular code design so we can have different programmers working on different functions concurrently and independently. All functions have "Get" and "Set" functions to allow programmers to access values from each function without needing to access a variable directly.

some examples...

Code:
#DEFINE ZERO 0
int x = 0;
x = Get_Left_Encoder_Count();
Set_Left_Encoder_Count(ZERO);
if (p1_y == 127)
    {
    pwm01 = 127;
    }
else
    {
    pwm01 = p1_y;
    }
__________________
Don't just ask the experts, become one!
Leadership is not about ability. It's about responsibility!
Diagonally Parked in a Parallel Universe. It's okay we do Quantum Physics



Last edited by Phalanx : 25-12-2007 at 14:27.
  #10   Spotlight this post!  
Unread 25-12-2007, 14:39
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
Re: Programming style When to use Caps and Underscores?

I know the question was specifically about C18, but you can use whatever style you want. I think its worth picking a style that can cross multiple platforms, but its up to you. Over the last few years at work we've been moving towards a very simple set of standards that are easy to remember. I'm not a big fan of using prefix notation to add metadata to a variable name. We use c#, but this is a simple convention:

- everything is in CamelCase.
ex) robotSpeed, GetSensorValue(), DoWackyDance()

- private members scoped to a class begin with underscore and lower case.
ex) private int _speed;

- parameters begin with a lower case letter
ex) public int GetSensorValue(int sensorPort)

- property, method, class, and interface declarations start with upper case.
- locally scoped variables begin with an lower case letter.
- methods that return a value begin with a verb (i.e GetCredentials())
  #11   Spotlight this post!  
Unread 26-12-2007, 00:56
Capt. Quirk Capt. Quirk is offline
Registered User
AKA: Bill Mazzacane
FRC #1704 (Robotic Steelers)
Team Role: Mentor
 
Join Date: Aug 2007
Rookie Year: 2007
Location: SoCalif
Posts: 64
Capt. Quirk will become famous soon enoughCapt. Quirk will become famous soon enough
Re: Programming style When to use Caps and Underscores?

Thank you for all your suggestions.

I was trying to come up with some consciences to use with the kids. In the past I would use the recommendations by Parallax, but they don't quite fit here.

Thanks again

Bill
  #12   Spotlight this post!  
Unread 26-12-2007, 16:28
11Mort11's Avatar
11Mort11 11Mort11 is offline
Registered User
AKA: Alex Joss
FRC #0011 (MORT)
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Mount Olive
Posts: 94
11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold11Mort11 is a splendid one to behold
Send a message via AIM to 11Mort11
Arrow Re: Programming style When to use Caps and Underscores?

http://www.web-hits.org/txt/codingunmaintainable.html
this should answer your questions

/**/

not really

Last edited by 11Mort11 : 26-12-2007 at 16:29. Reason: need
  #13   Spotlight this post!  
Unread 26-12-2007, 16:52
billbo911's Avatar
billbo911 billbo911 is offline
I prefer you give a perfect effort.
AKA: That's "Mr. Bill"
FRC #2073 (EagleForce)
Team Role: Mentor
 
Join Date: Mar 2005
Rookie Year: 2005
Location: Elk Grove, Ca.
Posts: 2,381
billbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond reputebillbo911 has a reputation beyond repute
Re: Programming style When to use Caps and Underscores?

Quote:
Originally Posted by 11Mort11 View Post
http://www.web-hits.org/txt/codingunmaintainable.html
this should answer your questions

/**/

not really
OK, I took the liberty to emphasize Alex's joke.

The link is a rather riotous to read if you are in the right mood, and understand it is tongue in cheek. But by all means, DO NOT ACTUALLY FOLLOW THE INSTRUCTIONS FOUND THERE. If you do, it's at your own peril!!


Thanks Alex, that really is a jewel.
__________________
CalGames 2009 Autonomous Champion Award winner
Sacramento 2010 Creativity in Design winner, Sacramento 2010 Quarter finalist
2011 Sacramento Finalist, 2011 Madtown Engineering Inspiration Award.
2012 Sacramento Semi-Finals, 2012 Sacramento Innovation in Control Award, 2012 SVR Judges Award.
2012 CalGames Autonomous Challenge Award winner ($$$).
2014 2X Rockwell Automation: Innovation in Control Award (CVR and SAC). Curie Division Gracious Professionalism Award.
2014 Capital City Classic Winner AND Runner Up. Madtown Throwdown: Runner up.
2015 Innovation in Control Award, Sacramento.
2016 Chezy Champs Finalist, 2016 MTTD Finalist
  #14   Spotlight this post!  
Unread 30-12-2007, 00:14
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: Programming style When to use Caps and Underscores?

Here's where I jump in and preach against the use of naked constants. Use #define <name> <constant> instead.

So instead of

Code:
  motorSpeed = 127;
use

Code:
#define STOPPED (127) // pwm value for zero speed

...

  motorSpeed = STOPPED;
There are three good reasons for this:

1. It's self-documenting. The macro STOPPED tells the reader what the constant means.

2. If you use a constant more than once and need to change all instances, you only need to change it in one place, instead of searching around for all the '127's in your code that should be changed. And you know you're gonna miss one!

3. The compiler will catch the error in

Code:
  motorSpeed = STOPED;
but will not catch

Code:
  motorSpeed = 122;
because both 127 and 122 are valid. But "STOPED" is a symbol presumably unknown to the compiler, and it will halt the build and tell you exactly what is wrong: it doesn't understand "STOPED".

Here's my real-life story on this issue. I worked on a project that processed streaming MPEG video in real time. If the code crashed it meant the TV station would go off the air and lose a lot of money.

A line of code checked for a special value indicating a special mode:

Code:
  if ( vbv_delay == 0xFFFF )
  {
     ...
  }
This test was performed in several places in my code. The code worked fine for months. One day someone on the project came to me to ask about a crash that looked like it originated in my code. I tracked it down to a typo. One of the tests was

Code:
  if ( vbv_delay == 0xFFF )
  {
     ...
  }
Oops. The compiler couldn't help me: 0xFFF and 0xFFFF are both valid constant values.

If I had used

Code:
#define VBR_MODE (0xFFFF)

  if ( vbv_delay == VBR_MODE )
  {
     ...
  }
not only would it have been easier to understand by someone else (and the code is now being maintained by someone else) the compiler would have immediately flagged something like

Code:
#define VBR_MODE (0xFFFF)

  if ( vbv_delay == VBR_MOD )
  {
     ...
  }
Fortunately we caught this error before we shipped any units, but we were lucky.

The moral: AVOID NAKED CONSTANTS!
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
use the Title Line when you post Ken Patton General Forum 11 20-12-2007 22:18
Camera stops working when we use camera_set_servos()... SeanCassidy Programming 16 13-02-2005 03:08
could use some programming help. mtaylor Programming 2 23-01-2005 10:54
When would I use an interrupt? Apollo Clark Programming 7 18-01-2004 13:29
Use of Reputation When People Disagree Joe Matt CD Forum Support 9 11-12-2003 16:40


All times are GMT -5. The time now is 18:34.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi