On 10/22/2013 10:46 PM,
ra...@edisonjunior.com wrote:
> Hi c,
>
> Trying to understand somebody else's code.
>
> I look at a simple loop, to write flash memory, a data structure 3 levels deep, and see stuff like this:
>
> if(GSN_FW_APP_0 == fwupCtx->gsnExtFlashFwupPvtCtx.binInProgress)
> {
> fwupCtx->gsnExtFlashFwupPvtCtx.fwupWlanCtrlBlk.APP1Size = fwupCtx->gsnExtFlashFwupPvtCtx.fwupWlanCtrlBlk.APP1Size + buffsize;
> /* Calculate the intermidiate checksum*/
> while(orgbuffsize>0)
> {
> fwupCtx->gsnExtFlashFwupPvtCtx.fwupWlanCtrlBlk.app1Checksum = fwupCtx->gsnExtFlashFwupPvtCtx.fwupWlanCtrlBlk.app1Checksum + (*orgbuff++);
> orgbuffsize--;
> }
>
> }
>
>
>
> It has been a while, do we really write now, with variable names 30+ characters long, in complex data structures, or do we use the preprocessor and tools to manage this?
To my taste, the identifiers are on the long side, and the
multiple appearances of "fwup" look redundant. Tastes vary, though,
and I don't know what other things these names distinguish from.
In any case the longest identifier I see has 21 letters, only
about two-thirds of your "30+".
Although one *could* use macros to abbreviate:
#define GIGGLE gsnExtFlashFwupPftCtx.fwupWlanCtrlBlk
...
fwupCtx->GIGGLE.app1Checksum = ...
... I'd recommend against it. By introducing a second name for
the same thing, you'd add opportunities for confusion: Someone
looking for all uses of GIGGLE.app1Checksum could easily overlook
a reference via the true name.
> I do not see how to read code written this way, it looks tool generated to me.
Doesn't look tool-generated to me (when tools deign to write
comments, they're usually about the mechanics and not about the
purpose), but I suppose it might be. If it is, you should look for
the tool and its input: Work with them, not with their output.
(A guy once sought my help in debugging some code, and I studied
it in vain for any explanation of the symptom he'd seen. It was
utterly inexplicable: There was simply no way his code could produce
the output he showed me. Come to find out he'd been using the
compiler to generate assembly code, hand-"optimizing" the assembly,
and running *that* -- and when it didn't work, he showed me the
original source code ... Don't edit tool output.)
> I will have to rewrite it by hand, and refer to the defined data structures to see whats going on. This is totally illegible.
A few uses of the "+=" operator would make a world of difference.
> Whats been happening since I have been out, for 10+ years?
The explanation is too long for the margin of this post.
--
Eric Sosman
eso...@comcast-dot-net.invalid