Suggestion on naming variables C

Discussion in 'Embedded Systems and Microcontrollers' started by Vindhyachal Takniki, Mar 24, 2015.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    348
    6
    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
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    3 characters is quite stingy. I give my vars a complete word or two is needed, mixing CapsAndLower cases to make them more readable.

    CONSTANTS are all caps.

    For a loop counter there is nothing wrong with using "i" as it IS the loop counter by tradition.

    What's a static constant? Isn't that a bit redundant?

    I'm not that concerned with putting the type into a name, excepting a leading "p" for a pointer. Type checking is done by the compiler.
     
    tshuck likes this.
  3. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,908
    2,168
    Make variables (data structures and code functions in general) explain themselves so when you look at the code there's no need for comments unless something tricky is happening. A basic skill that's overlooked in programming is good typing and editing speed so when you use long names it's not a chore.

    If you program in C learn to use data structures and pointers to organize programs into readable formats where the physical bit structure details are declared when the variable structure is defined.

    Length does matter. :D

    Code fragment from a chip driver
    Code (Text):
    1.  
    2. struct comedi_control {
    3. u8 *tx_buff;
    4. u8 *rx_buff;
    5. struct mutex drvdata_lock;
    6. } ;
    7. static struct comedi_control comedi_ctl;
    8.  
    9. struct spi_adc_type {
    10. uint16_t range : 1;
    11. uint16_t bits : 1;
    12. uint16_t link : 1;
    13. uint16_t pic18 : 2;
    14. uint16_t chan : 4;
    15. struct spi_device *spi;
    16. } ;
    17. static struct spi_adc_type spi_adc, spi_dac;
    18.  
    19. struct pic_platform_data {
    20. uint16_t conv_delay_usecs, cmd_delay_usecs;
    21. struct mutex drvdata_lock;
    22. } ;
    23.  
    24. static struct pic_platform_data pic_info_pic18 = {
    25. .cmd_delay_usecs = 20,
    26. .conv_delay_usecs = 50
    27. };
    28.  
    29. #define CSnA 0 /* GPIO 8 Gertboard ADC */
    30. #define CSnB 1 /* GPIO 7 Gertboard DAC */
    31.  
    32. So later when you code the function it's easier to understand what's happening
    33. ...
    34. /* convert n samples */
    35. for (n = 0; n < insn->n; n++) {
    36.   /* Make SPI messages for the type of ADC are we talking to */
    37.   /* The PIC Slave needs 8 bit transfers only */
    38.   if (spi_adc.pic18 > 1) { /* PIC18 SPI slave device */
    39.   mutex_lock(&pic_data->drvdata_lock);
    40.   udelay(pic_data->cmd_delay_usecs); /* ADC conversion delay */
    41.   comedi_ctl.tx_buff[0]=CMD_ADC_GO+chan;
    42.   spi_write_then_read(spi_adc.spi,comedi_ctl.tx_buff,1,comedi_ctl.rx_buff,1); /* rx buffer has old data */
    43.   udelay(pic_data->conv_delay_usecs); /* ADC conversion delay */
    44.   comedi_ctl.tx_buff[0]=CMD_ZERO;
    45.   spi_write_then_read(spi_adc.spi,comedi_ctl.tx_buff,1,comedi_ctl.rx_buff,1); /* rx buffer has old data */
    46. ....
    47. } else { /* Gertboard device */
    48.   comedi_do_one_message((0b01100000 | ((chan & 0x01) << 4)), CSnA, 2); /* set ADC channel SE, send two bytes */
    49.   data[n] = (comedi_ctl.rx_buff[0]&0b00000011) << 8; /* two bytes were received from the FIFO */
    50.   data[n] += comedi_ctl.rx_buff[1];
    51. }
    52. }
    53. return n;
    54.  
     
    Last edited: Mar 24, 2015
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    There are many coding conventions out there. It's about what works for you, and that may change with time.

    Personally, I use one similar to what ErnieM is talking about.

    Here is another.

    For more examples, see here.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Don't skimp on the number of letters. Readability is 100 times more important than ease of typing.
    Spending two seconds to type in a long name will save you hours to debugging later on.

    Constants are all caps. SIZE_OF_INPUT_BUFFER

    Global variables begin with upper case. InputBuffer

    Local variables are are lower case. number_of_data_points

    Loop index. this_datai

    Pointer. this_data_ptr
     
  6. MCU88

    Member

    Mar 12, 2015
    360
    35
    Except for constants in all upper case (as Mr.Chips has illustrated) -- I always do this:
    • ledMatrixRow01
    • doGameOver
    • runMatrixScanner
    The same convention as Java. The first letter of the first word is lower case and the rest start with an upper for the first letter of the word.
     
Loading...