Suggestion on naming variables C

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now I am trying to name variables by below method. Any suggestions/comments

1. All vars should of be atleast 3 chars like cnt,loop. Declaring i is incorrect.
2. Datatype of var should be visible from its name Like u32_cnt.

1.Global var: g_u32_loop
2. global constant: gc_u8_loop
3. Static var: s_i16_loop;
4. static constant: sc_c8_loop;
5. local var: f32_loop;
6. local const: c_f64_loop;
7. ptr: p_u16_cnt

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think each and every person has his/her own way of doing this.

you should take/make a method that you feel comfortable with and that you think will work in the end.

I know here at work they are trying to use a single style, but often when I have to help the FW guys I see that that standard is not always maintained/used.

And recently I found out that they have changed the standard a couple of times lately to make it more workable.

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Vindhyachal Takniki wrote:
2. Datatype of var should be visible from its name Like u32_cnt.

But you never use that form - because there will always be a "scope" prefix

 

Quote:
5. local var: f32_loop;
6. local const: c_f64_loop;

These are inconsistent!

 

What about local statics?

 

What about static consts?

Quote:
7. ptr: p_u16_cnt

What about local/static/const pointers?

 

What about volatile?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meslomp wrote:
you should take/make a method that you feel comfortable with and that you think will work in the end.

Indeed.

 

You need to ask yourself: what is the purpose of adopting a naming convention? What value does it add?

 

Your scheme adds a lot of "baggage" to the names - is that valuable, or is it just clutter/noise?

 

Personally, I think it's just clutter/noise: IMO, the purpose of names is to convey what the things do; scope & type are just implementation details which can easily be found just using the features of a decent IDE.

 

So, for example, rather than just mark things "global", I would use a prefix to indicate which module "owns" the symbol.

 

But, again, it's entirely up to you and what works for your specific situation. The important thing is to make sure your system is consistent, complete, and regular. And to use it consistently, completely, and regularly.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Mar 24, 2015 - 07:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Such things are always going to be a personal opinion. For your own code it doesn't really matter. Just use whatever coding standard you feel comfortable with. The key thing is to write code YOU find easy to read and maintain in the future.

 

When coding standards really get important is when you are in a team working collaboratively on code. You need all the code to "look and feel" the same so that any of the team members can easily read and understand the code written by any of the others. Probably the best way to do this is to collaboratively draw up a coding standard (well perhaps one person authors a draft then others provide feedback?) and take the best ideas from everyone and listen to the arguments of other as to why things should not be done in a certain way. Not everyone is going to get exactly what they want (as I say we all have our own personal opinions of such things) but hopefully you come up with something that at least everyone is reasonably happy with. It may be an idea to consult 3rd party literature while iteratively working on such a coding standard. The one we use takes heavily from things like the MISRA standard and "Writing Solid Code" by Steve Maguire.

 

As to your own list above my thoughts:

 

1) Don't agree. Yes variables should have meaningful names but for iterating array indices (say) I think "i", "j" and "k" are perfectly acceptable. Equally if you were processing graphics it's got to be very tempting to use "x" and "y".

 

2) yes and no. It's a nice feature but often the type part can obscure the name part. It used to be the case that you would do this so as you read the code you could instantly see its type. However in any decent IDE editor these days if you just position the mouse over a variable it will usually show you the type declaration anyway. So having to have it as part of the var name is no longer so necessary.

 

3 and on) (actually 1 and 2 repeat!) Again it can be useful to get an idea of scope (global/static/local) but often the IDE will highlight this for your anyway. What we've done in the past in our coding standard (for C not C++ where "namespace" makes this irrelevant) is to prevent name polution we give the externally visible variables names that start with star with a three letter acronym (TLA) that identifies the module they are in like ADC_reading, URT_rcv_byte, DAC_value and so on. In such a scheme we then use the case of the TLA to identify the scope of the variable. So ADC_result with the ADC all in upper case is universally visible (global). Adc_prescaler with the the first letter in upper case and the rest not is "module global". This is something that does not really exist as a concept in C but was an indicator to our programmers to say "although you may be able to see this globally - it belongs to ADC alone and should not be accessed by other modules". We would then use adc_somevar for statics within a single file of the ADC module. For any automatics there were no particular requirements as thei scope was obviously limited to the function it occurred within (often an "i" or a "j" in fact!).

 

BTW in C++ I would prefix members with just "m" or perhap "m_".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For these perplexing questions, i refer to Code Complete. Just about any coding problem (as in the art of coding) is discussed and useful guidelines given.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course the next question is "delimiter separated or letter case separated?". I used to be a dedicated fan of underscores:

int sum_all_values(int a, int b, int c) {...

but these days I tend to use case separation ("CamelCase"):

int sumAllValues(int a, int b, int c) {...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've been accused of mixing both. The boss said 'choose one or the other!' The problem is third party code uses both so i'm caught in the middle. I'm leaning towards camel case at the moment.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I like to name calculated quantities with the name and a units suffix. Example: airspeedkts, airspeedmpersec, airspeedmph, airspeedfps. adraw, advolts etc. Easy to keep like units in expressions.

 

Imagecraft compiler user

Last Edited: Wed. Mar 25, 2015 - 07:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
I like to name calculated quantities with the name and a units suffix.

That's a good idea.

 

Otherwise, at the very least, units should be stated in the comment on the variable definition.

 

Quote:
Example: airspeedkts, airspeedmpersec, airspeecmph, airspeedfps. adraw, advolts

But running multiple words into one with no separation whatsoever is horrible!!

 

no

 

Please, either use underscores: airspeed_kts, airspeed_mpersec, airspeec_mph (sic?), airspeed_fps. ad_raw, ad_volts

 

or CamelCase: airspeedKts, airspeedMpersec, airspeecMph, airspeedFps. adRaw, adVolts

 

But don't just run everything together!!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess it's debatable whether "airspeed" is a single word or not...?

 

wink

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's a good idea.

We do that. In cars there are lots of things like x_m, y_mm, rot_rad, steering_deg and other measurements like that in meters, millimeters, radians and degrees. The common error if you don't do something like this is to interpret _m as _mm or something. Didn't a famous space flight fail because of a metric/imperial conversion of units or something?

 

EDIT: ah yes ... http://en.wikipedia.org/wiki/Mar...

Last Edited: Tue. Mar 24, 2015 - 01:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

In days past they used this type of notation because the IDEs were crappy and it helped on to remember the variables data type but with new IDEs and intellisense I don't see the need

any more.  It's a lot more important to convey what context the variable is used or what it's used for then it's type.

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think, as a rule of thumb, that I dislike naming strategies that highlight properties (scope, type) where the compiler will generate errors when they're incorrect, anyway.

(Note that this means that the schemes might change on languages that don't have strongly typed predefined variables.)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My habit is to ALWAYS use stdint.h types (uint8_t, et al)

And camelBackNames

And rather long names.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:
languages that don't have strongly typed predefined variables

Such as 'C' ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I hate underbars in names. Hard_to_touch_type. Yea, I know, C is for people who don't know how to type

 

 

The largest known prime number: 282589933-1

Without adult supervision.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For variables, I like to use camelCase. For functions/methods I capitalize the first letter as well -- ReadSensor().

 

I also use suffixes to indicate engineering units, like this: tempF, tempC, tempK.

 

For constants and preprocessor macros, I use uppercase: PI, COLUMNS, etc.