PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Goto still considered ful
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Goto still considered ful

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 11h56   #1
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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
  Réponse avec citation
Vieux 19/10/2007, 12h20   #2
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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

  Réponse avec citation
Vieux 19/10/2007, 12h39   #3
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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
  Réponse avec citation
Vieux 19/10/2007, 12h46   #4
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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

  Réponse avec citation
Vieux 19/10/2007, 21h53   #5
Peter Pichler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Goto still considered ful

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.)
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14022 seconds with 13 queries