Suggestion on naming variables C

Thread Starter

Vindhyachal Takniki

Joined Nov 3, 2014
594
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
 

ErnieM

Joined Apr 24, 2011
8,377
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.
 

nsaspook

Joined Aug 27, 2009
13,270
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:
struct comedi_control {
u8 *tx_buff;
u8 *rx_buff;
struct mutex drvdata_lock;
} ;
static struct comedi_control comedi_ctl;

struct spi_adc_type {
uint16_t range : 1;
uint16_t bits : 1;
uint16_t link : 1;
uint16_t pic18 : 2;
uint16_t chan : 4;
struct spi_device *spi;
} ;
static struct spi_adc_type spi_adc, spi_dac;

struct pic_platform_data {
uint16_t conv_delay_usecs, cmd_delay_usecs;
struct mutex drvdata_lock;
} ;

static struct pic_platform_data pic_info_pic18 = {
.cmd_delay_usecs = 20,
.conv_delay_usecs = 50
};

#define CSnA 0 /* GPIO 8 Gertboard ADC */
#define CSnB 1 /* GPIO 7 Gertboard DAC */

So later when you code the function it's easier to understand what's happening
...
/* convert n samples */
for (n = 0; n < insn->n; n++) {
  /* Make SPI messages for the type of ADC are we talking to */
  /* The PIC Slave needs 8 bit transfers only */
  if (spi_adc.pic18 > 1) { /* PIC18 SPI slave device */
  mutex_lock(&pic_data->drvdata_lock);
  udelay(pic_data->cmd_delay_usecs); /* ADC conversion delay */
  comedi_ctl.tx_buff[0]=CMD_ADC_GO+chan;
  spi_write_then_read(spi_adc.spi,comedi_ctl.tx_buff,1,comedi_ctl.rx_buff,1); /* rx buffer has old data */
  udelay(pic_data->conv_delay_usecs); /* ADC conversion delay */
  comedi_ctl.tx_buff[0]=CMD_ZERO;
  spi_write_then_read(spi_adc.spi,comedi_ctl.tx_buff,1,comedi_ctl.rx_buff,1); /* rx buffer has old data */
....
} else { /* Gertboard device */
  comedi_do_one_message((0b01100000 | ((chan & 0x01) << 4)), CSnA, 2); /* set ADC channel SE, send two bytes */
  data[n] = (comedi_ctl.rx_buff[0]&0b00000011) << 8; /* two bytes were received from the FIFO */
  data[n] += comedi_ctl.rx_buff[1];
}
}
return n;
 
Last edited:

MrChips

Joined Oct 2, 2009
30,805
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
 

MCU88

Joined Mar 12, 2015
358
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.
 
Top