EmbeddedRelated.com
Forums

MISRA new rule suggestion

Started by Rob June 20, 2005
On 20 Jun 2005 01:51:47 -0700, "Rob" <mr_horton@yahoo.com> wrote:

>"Never use characters that cannot be pronounced"
This _definitively_ makes sense in situations when telephone support is given. Typical situations would be when someone is instructed over the phone to enter some command line interpreter commands. In such situation it would be quite catastrophic, if the command or parameters would include underscores, hyphens or be case sensitive and the command would contain mixed case characters. On the other hand, how common would it be to dictate for instance a C-program over the phone ? Even if a small change is dictated over the phone, the receiver could check out the previous use or declaration of that name and apply it. Even if the dictation would contain a declaration of a new local variable, the receiver could still use any case/underscore combination at his/her discretion as long as the declaration and use is the same. Paul
"Richard Henry" <rphenry@home.com> writes:

> "Paul Carpenter" <paul$@pcserviceselectronics.co.uk> wrote in message > >> Probably just you, as I particular hate groupings like the following >> >> LeftHandedWidgetCount >> LeftHandedWidgetCountMax >> LeftHandedWidgetCountMin >> LeftHandedWidgetCountStep >> LeftFootedWidgetCount >> LeftFootedWidgetCountMax >> LeftFootedWidgetCountMin >> LeftFootedWidgetCountStep >> LeftFootedWalkingCount >> LeftFootedWalkingCountMax >> LeftFootedWalkingCountMin >> LeftFootedWalkingCountStep > > In a situation like that, one should also check the > (implementation-specific) significant initial character length. In the > examples above, if the compiler only recognizes the first 16 characters, > then LeftHandedWidgetCount and LeftHandedWidgetCountMax refer to the same > variable.
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

Steve at fivetrees wrote:
> "Lanarcam" <lanarcam1@yahoo.fr> wrote in message > news:1119435835.256492.263110@g43g2000cwa.googlegroups.com... > > I once saw a sound advice, that you use the long names for > > globals and the short names for locals. > > Globals? What globals? (I avoid global variables at all costs. I'm hoping > you mean function names.)
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.
> > Something like for(CounterOfLeftHandedWidget=0; ...) is > > overkill. for (ct_lhw=0; ...) is easier to read. You don't > > get lost in noise. If you have only a few local variables > > in your function it is not necessary to have long names. > > I dislike cryptic variable names (e.g. ct_lhw) intensely. Common sense would > suggest a compromise, e.g. WidgetCountLH.
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.
> Steve > http://www.fivetrees.com
"Lanarcam" <lanarcam1@yahoo.fr> wrote in message 
news:1119472222.356396.289670@g43g2000cwa.googlegroups.com...
> 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 http://www.fivetrees.com
On Wed, 22 Jun 2005 23:03:52 +0100, "Steve at fivetrees"
<steve@NOSPAMTAfivetrees.com> wrote:

>"Lanarcam" <lanarcam1@yahoo.fr> wrote in message >news:1119472222.356396.289670@g43g2000cwa.googlegroups.com... >> 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.
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 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... >
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 ;-)
>Steve >http://www.fivetrees.com >
-- Al Balmer Balmer Consulting removebalmerconsultingthis@att.net
Steve at fivetrees wrote:
> "Lanarcam" <lanarcam1@yahoo.fr> wrote in message > news:1119435835.256492.263110@g43g2000cwa.googlegroups.com... > >>I once saw a sound advice, that you use the long names for >>globals and the short names for locals. > > > Globals? What globals? (I avoid global variables at all costs. I'm hoping > you mean function names.) > > >>Something like for(CounterOfLeftHandedWidget=0; ...) is >>overkill. for (ct_lhw=0; ...) is easier to read. You don't >>get lost in noise. If you have only a few local variables >>in your function it is not necessary to have long names. > > > I dislike cryptic variable names (e.g. ct_lhw) intensely. Common sense would > suggest a compromise, e.g. WidgetCountLH. >
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.
David Brown wrote:
> I dislike for() loops that are so long or complicated that a simple "i" > is insufficient for the loop variable.
Yeah, I have a few habits left over from programming in Fortran, too ;) Kelly
On 22 Jun 2005 13:30:22 -0700, Lanarcam wrote:
> 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.
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
On Wed, 22 Jun 2005 14:27:27 +0100, "Steve at fivetrees"
<steve@NOSPAMTAfivetrees.com> wrote:

>"Lanarcam" <lanarcam1@yahoo.fr> wrote in message
[...]
>> Something like for(CounterOfLeftHandedWidget=0; ...) is >> overkill. for (ct_lhw=0; ...) is easier to read. You don't >> get lost in noise. If you have only a few local variables >> in your function it is not necessary to have long names. > >I dislike cryptic variable names (e.g. ct_lhw) intensely. Common sense would >suggest a compromise, e.g. WidgetCountLH.
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.