MISRA new rule suggestion

Jun 20, 2005 50 Replies

Usually, you see stuff like that because arrays are not possible, in which case they're just about acceptable... (We have a naming convention for sensors and actuators so we get things like LF3_Flex_Fill or THJ2_Pos - these can be generated mechanically, are broken up as words by editors, and so on - but we then use arrays and references to handle them).

But really, there should be an array struct count { int v; int Min; int Max; int Step; } WidgetCount[]; and another struct count WalkingCount[]

and enums should be used for the access.

It's not like the compiler can't optimise away the overhead.

cheers, Rich.

rich walker | Shadow Robot Company | rw@shadow.org.uk technical director 251 Liverpool Road | need a Hand? London N1 1LX | +UK 20 7700 2487 www.shadow.org.uk/products/newhand.shtml

All right, we won't tell them;)

But for instance with classes, you have *member* variables and they are global for the whole file or class.

On the contrary in methods (or functions) you have local variables. Then you can use short names there is no risk of confusion.

btw, you can also have globals for the whole application. for instance a variable that is the copy of a timer. You only need to make sure they are used appropriately, possibly with critical sections.

This is a matter of agreement among coders.

Personally, I find a for (i=0; i< ...) handy in some cases. IndexOfItemInArray would not add information, just noise. Of course if you have a variable of higher scope that represents an important concept you should use a longer name.

Understood.

FWIW, I avoid single-name variables for one important reason: I can't search for them. I tend to use "index" as a variable name in such cases as you mention.

I've heard this approach criticised on the basis that "i" is quicker to type than "index" - but I always paste variable names anyway, so this seems bogus to me...

Steve

formatting link

A good editor should be able to find uses of a single letter variable, even if the same name is used in more than one scope.

I suspect that it's quicker to type "i" than paste a variable, too. Even if you only use one variable, a paste in most editors requires at least two fingers ;-)

Al Balmer Balmer Consulting removebalmerconsultingthis@att.net

I dislike for() loops that are so long or complicated that a simple "i" is insufficient for the loop variable. Names that are too long are a hindrance to the readability of the code and ease of writing the code - if you have to use copy and paste for variable names, or can't remember the exact name, then you are distracted from what you are writing. Cryptic abbreviations are of no use to anyone.

Yeah, I have a few habits left over from programming in Fortran, too ;)

Kelly

There are 3 or 5 single variable name that are acceptable (to me) as long as they are used in only one contect (each) ..

for (i = 0; ... ) for (j = 0; ... ) for (k = 0; ...) // And you could possibly extend that through l, m, ...

and the other contect is p and q, although more normally pch and qch, as in pch = somestring; qch = someotherstring; while (*pch) *qch++ = *pch++;

although I don't personally like that one and I'd more likely use src and dst, or some such. i, j,a dn k are perfectly acceptable as array indicies, becuase that's the mathematical convention. p and q are an old K&R hangover from when I learned C in the late 70s or early 80's, can't remember when my mind has large bits that no longer parse ... :-)

Nobby

GreatBigLongRunTogetherVariableNames are an abomination, especially when combined with GreatBigRunTogetherVariableNames that aren't much different. Even WidgetCountLH is hard to differentiate from WidgetCountRH at a glance -- There's only one character different. And the variable name emphasizes what is the same rather than what is different.

In the above example, I'd probably use something like num_lefthw and num_righthw to emphasize the differences between the variables. But the control variable of a for loop is generally idx, row, col, id, ctr, or whatever makes sense in context. I avoid single-character control variables because they become difficult to search for in the editor, let alone see...

Underscores are an aid to understanding (I'd be even happier if they were allowed in numeric literals -- I have a 64-bit constant in the code I'm working on now, and I think 0x8000_FF00_1680_0000 would be easier to understand than 0x8000FF0016800000, and even 120_000 vs.

120000).

Regards,

-=Dave

Change is inevitable, progress is not.

I guess this discussion is really about C, but as long as I was mentioning how winning it is that names in Lisp use hyphens (not underscore) for spaces, I thought I'd also pass along the recollection that COBOL also does.

Some sample Lisp names:

ENQUEUE-MESSAGE-CHANNEL-BUFFER WITHOUT-INTERRUPTS STOP-82C54-PERIODIC-TIMER SCSI-MODE-SENSE-BLOCK-DESCRIPTOR-BLOCK-LENGTH MAYBE-WAKEUP-DMA-SUPERVISOR CHANNEL-ENABLE-68562-RECEIVER IS-PLAN-FOR-CONJUNCTIVE-SUBGOALS PARENT-INFERIORS BUBBLE-MUNG-REGION-ARRAY REAL-COMPLEX-W RV-FFT-IMAG-INDEX-ARRAYS

Some sample COBOL names:

LINE-NO REPLENISH-REPORT-HEADING UNITS-ON-HAND

Pretty much the same! People (for the last 20 years) usually write Lisp source code in lowercase, though, although the case doesn't matter. Lisp predates COBOL.

I think I wrote my last Fortran about 1970. However, I avoid i and l, but will bandy about ix and iy. I have fewer objections to j and k, n, m, and p. n and p are relatively common.

Some informative links: news:news.announce.newusers http://www.geocities.com/nnqweb/ http://www.catb.org/~esr/faqs/smart-questions.html http://www.caliburn.nl/topposting.html http://www.netmeister.org/news/learn2quote.html

Prithee tell, how doth one type _ without using the shift key?

If you want an example of extreme laziness, look at C "programmers" who don't bother to lint their code. But I'm crossing subthreads now...

Regards,

-=Dave

Change is inevitable, progress is not.

By using a non english keyboard, at least some. That could explain why underscores are used at some places. Or it could be the other way around.

Actually, they aren't programmers. Those are monkeys.

cheers, Rich.

rich walker | Shadow Robot Company | rw@shadow.org.uk technical director 251 Liverpool Road | need a Hand? London N1 1LX | +UK 20 7700 2487 www.shadow.org.uk/products/newhand.shtml

Underscores in numeric literals are both allowed and encouraged in Ada.

For example

Initial_Balance := 510_020_400_123; Pi := 3.14159_26535_89793_23846;

or

for Control_Register'Address use 16#00F0_A010#;

--Britt

I still like using i - n as counters, left over from my Fortran days, however there is a cool trick a co-worker shared with me over 20 years ago that I still use: If you double up the letters in your simple counters using 'ii', 'jj' or 'cc' for a throw away character, etc. as variable names, you'll have a *much* easier time searching for them in your text editor. For instance try searching for 'i' and 'ii' in this email.

I'm also *absolute* in starting all my globals with a capital, and all local without a capital letter. It can always tell whether a variable is a local or global.

-Zonn

Zonn Moore Remove the ".AOL" from the Zektor, LLC email address to reply. www.zektor.com

There was a set of rather nifty macros a while ago. They are for binary representation, but you get the idea.

Vadim

One doesn't, thus catering to dislikes of excess labor and of the presence of '_'s in source text. If I must use the shift key, at least let me use a normal alpha key with it.

"If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson

Only for rather limited-capability instances of "text editor". A programmers' editor that can't search for 'i' as a *word* (as understood by the programming language being edited) isn't really worth your money.

Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.

Remap your keyboard?

Can''t you just rotate the cap on the -/_ key 180 degrees?

But seriously, one could wonder why no programmers editor/IDE vendor in the world has come up with a "keyboard-in-C-mode" mode, where you actually do not have to press shift to get _.....

For some stupid reasong I always use uderscores when programming in C or assembly, while at the same time I use capitals when programming in Pascal. Weird guys, those programmers...

Meindert

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required