|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Peter Pichler <usenet@pichler.co.uk> wrote:
> Ed Jensen wrote: > > > done: > > if (sf) > > fclose(sf); > > if (df) > > fclose(df); > > As you wish. I do not like this approach because it requires all > declarations at the top of the function, which IMO is almost as bad > as making them global. Beg to differ. Beg to differ very greatly, in fact. Declarations scattered through the code hailshot-wise are a great way of ensuring that you'll have to hunt for one sooner or later. > In addition, it requires them initialised to "harmless" values > which can hide bugs later on. No, it doesn't. For example, in the code from which the above is quoted, the initialisations of sf and df are unnecessary: fopen() will always return either a valid FILE *, or a null pointer, never garbage. Richard |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
> Peter Pichler <usenet@pichler.co.uk> wrote: > >> Ed Jensen wrote: >> >> > done: >> > if (sf) >> > fclose(sf); >> > if (df) >> > fclose(df); >> >> As you wish. I do not like this approach because it requires all >> declarations at the top of the function, which IMO is almost as bad >> as making them global. > > Beg to differ. Beg to differ very greatly, in fact. Declarations > scattered through the code hailshot-wise are a great way of ensuring > that you'll have to hunt for one sooner or later. Not with any half decent editor. Besides, locally declared variables don't need to be hunted for. They are there where you need them. Declaring all variables in one go at the top is bad for maintenance and leads to confusion IMO. Declare the variable at the last possible moment. It also keeps the locals display in your debugger cleaner and only showing what is pertinent to the current context. > >> In addition, it requires them initialised to "harmless" values >> which can hide bugs later on. > > No, it doesn't. For example, in the code from which the above is quoted, > the initialisations of sf and df are unnecessary: fopen() will always > return either a valid FILE *, or a null pointer, never garbage. > > Richard |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Richard <rgrdev@gmail.com> wrote:
> rlb@hoekstra-uitgeverij.nl (Richard Bos) writes: > > > Peter Pichler <usenet@pichler.co.uk> wrote: > > > >> Ed Jensen wrote: > >> > >> > done: > >> > if (sf) > >> > fclose(sf); > >> > if (df) > >> > fclose(df); > >> > >> As you wish. I do not like this approach because it requires all > >> declarations at the top of the function, which IMO is almost as bad > >> as making them global. > > > > Beg to differ. Beg to differ very greatly, in fact. Declarations > > scattered through the code hailshot-wise are a great way of ensuring > > that you'll have to hunt for one sooner or later. > > Not with any half decent editor. Ah, yes, I forgot. Desk checks have gone out of fashion. Because they required that you were, you know, competent. Can't be having that. > Besides, locally declared variables don't need to be hunted for. > They are there where you need them. Your confidence in your fellow programmer is endearing, but ill deserved. Richard |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
> Richard <rgrdev@gmail.com> wrote: > >> rlb@hoekstra-uitgeverij.nl (Richard Bos) writes: >> >> > Peter Pichler <usenet@pichler.co.uk> wrote: >> > >> >> Ed Jensen wrote: >> >> >> >> > done: >> >> > if (sf) >> >> > fclose(sf); >> >> > if (df) >> >> > fclose(df); >> >> >> >> As you wish. I do not like this approach because it requires all >> >> declarations at the top of the function, which IMO is almost as bad >> >> as making them global. >> > >> > Beg to differ. Beg to differ very greatly, in fact. Declarations >> > scattered through the code hailshot-wise are a great way of ensuring >> > that you'll have to hunt for one sooner or later. >> >> Not with any half decent editor. > > Ah, yes, I forgot. Desk checks have gone out of fashion. Because they > required that you were, you know, competent. Can't be having that. > >> Besides, locally declared variables don't need to be hunted for. >> They are there where you need them. > > Your confidence in your fellow programmer is endearing, but ill > deserved. Sorry? I am advocating things to improve things. I am not showing confidence in anything. > > Richard |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Richard Bos wrote:
> Peter Pichler <usenet@pichler.co.uk> wrote: >>Ed Jensen wrote: >> >>>done: >>> if (sf) >>> fclose(sf); >>> if (df) >>> fclose(df); <big snip> >>In addition, [this style] requires [all variables] initialised to "harmless" values >>which can hide bugs later on. > > No, it doesn't. For example, in the code from which the above is quoted, > the initialisations of sf and df are unnecessary: fopen() will always > return either a valid FILE *, or a null pointer, never garbage. The context was lost in snipping. It went something like this: type1 var1; type2 var2; /* you need, "= safe_value;" here */ initialize(var1); /* what if this fails? */ if (wrong(var1)) goto end; initialize(var2); if (wrong(var2)) goto end; do_something(var1, var2); end: if (need_deinitialize(var1)) deinitialize(var1); if (need_deinitialize(var2)) /* BANG! (maybe) */ deinitialize(var2); Now, if initializing var1 fails, var2 remains unitialized and your cleanup section is a potential trouble. Therefore you need to initialize var2. With more than two variables, you need to initialize all of them before jumping to end. Declaring variables as local as possible avoids this problem. (I am not fighting a holy war, only explaining.) |
|
![]() |
| Outils de la discussion | |
|
|