Re: pseudo-random key generator, not bad says i

From: Tom St Denis (tom_at_securescience.net)
Date: 04/30/04


Date: Fri, 30 Apr 2004 12:12:37 GMT

Mark Nudelman wrote:
> "Tom St Denis" <tom@securescience.net> wrote in message
> news:ITSjc.6579$huU.6564@news04.bloor.is.net.cable.rogers.com...
>
> Some generally good comments, but I have one disagreement.
>
>
>>>prng random;
>>
>>Normally it's not a good idea to define variables in the middle of code
>>blocks.
>
>
> I think it's a bad idea to declare all your variables at the top of a
> function, C-style. A variable should be declared as late as possible, just
> before its first use, so that you don't have remember the variable
> definition when you see its first use, or have to go scroll back up to the
> top of the function to see what the variable is. This style also limits the
> scope of the variable to the necessary minimum, which prevents errors where
> a variable is inadvertently used earlier than it should be used. In a way
> it's a form of data hiding -- you don't let the earlier code in the function
> see variables it doesn't need to know about. What's your objection to
> declaring variables in code?

I write functions like I write algorithms.

INPUT: BLAH
OUTPUT: BLAH
1. do this
2. do that
.
.
.
N. Output [or Return] this.

If you look at my code [more LTM than LTC] I generally do one algorithm
per C function [and usually] per .C file.

As for recalling how it was defined that's why you try not to have more
variables than you can remember but also why organizing the definition
block correctly is important. E.g.

int a, b, c;
char d,e,f;
unsigned long g,h,i;

Is harder to read than say [use monospace font here...]

int a, b, c;
char d, e, f;
unsigned long g, h, i;

In some [limited] cases yes there are going to be special short-term
variables. In LTM for instance, inside the comba multipliers. But I
make it very clear that it is a new code block. It also highlights that
the variables are only used to assist in one place... e.g.

[from one function in LTM...]
   for (ix = 0; ix < pa; ix++) {
       register mp_digit tmpx, *tmpy;
       register mp_word *_W;
       register int iy, pb;

       /* alias for the the word on the left e.g. A[ix] * A[iy] */
       tmpx = a->dp[ix];

       /* alias for the right side */
       tmpy = b->dp;
[...]

What I have against defining variables anywhere in general is that it
makes it harder to tell what variable is what [yes, I'm claiming the
exact opposite of you]. Consider a 300 line C function

int some_func(some_params....)
{
    int idx_counter;
    char ch;

[... 75 lines ....]
    float *dct_terms = w->table[idx_counter+3];
[... 200 more lines ....]
    for (int x = 0; x < 100; x++) dct_terms[x] *= sample_info[x];

Well if you jump in the file and see that last line what types are
dct_terms and sample info? Oh, well look at the start, oops no
definition, ok read through the entire function... This gets worse with
people who don't factor their code properly into a nice modular design.

If you define your variables at the top [or at least at the top of code
blocks] it's much easier to see definitions.

Tom



Relevant Pages

  • newbie, @@error does not seem to work
    ... -- Declare variables to store values originating from the Event Table (And ... Declare @ca3k_Medew_ltv_nr char ... Insert into Event_Log (EDate, Number, Status, Badge, Class, ...
    (microsoft.public.sqlserver.programming)
  • Re: C string array problems (again)
    ... on the char ** var approach. ... scope and being re-initialized with each loop. ... So why did they declare it ... Would have to take a look a the original code to see what was meant. ...
    (microsoft.public.vb.general.discussion)
  • Re: Cursor and Procedure
    ... I need to truncate those strings to the segments with 200 ... Actually problem is ntext hold> 4000 char. ... DECLARE @deptTriDes ntext ...
    (comp.databases.ms-sqlserver)
  • Re: Comments on my code?
    ... value in a char *. ... Since you didn't declare the argument type, ... void *malloc; ... There's simply no good reason not to ...
    (comp.lang.c)
  • Re: Question about unpacking a binary file: endian troubles
    ... > This may be a dumb question; I'm just getting into C language here. ... `char' is always zero or positive, ... include to declare it. ... > int fdi, n; ...
    (comp.lang.c)