🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #7618

Re: Newbie Wants Advice on C-Programming

#7618
From: Richard M. Hartman
Date: Tue, 12 Jan 1993 17:05
141 lines
6094 bytes
In article <1993Jan11.131838.12844@cbnewsj.cb.att.com> davet@cbnewsj.cb.att.com (Dave Tutelman) writes:
>In article <FISCHER.93Jan9030817@orange.iesd.auc.dk> fischer@iesd.auc.dk (Lars Peter Fischer) writes:
>>
>>>>>>> "Michael" == Michael Malak (malak@grebyn.com)
>>>>>>> "Lars" == fischer@iesd.auc.dk (Lars Peter Fischer)
>>>>>>> "John" == John MAX Skaller (maxtal@extro.ucc.su.OZ.AU)
>>
>>John> 	Seems to me that the simple concept of
>>John> nesting the symbol table so that symbols go out of scope at the
>>John> end of the block is very nice.
>
>C and C++ have this, too.
>
>My organization's coding standard (adopted explicitly to make our software
>easier to read and maintain) calls for declaring and initializing
>variables as close as possible to their point of first use.  In the ability
>to meet this goal:
>	C++ good.
>	Pascal bad.

I am a C person and I have to disagree with your standard.  In C++
(and, I think, ANSI C although I am not certain) it is possible to
declare new variables almost anywhere.  e.g.:

	main()
	{
	cout << "stuff" << endl;
	int i = 2;
	cout << "i is " << i << endl;
	}

This, you would agree, is declaring as close to the usage as possible,
but let's say the function is a little longer than that.  I want to
add a loop w/ the control variable "i".  Where can I check to see if
it has already been declared or not if all the variable declarations
are scattered throughout the code?

The biggest concession I will make to this practice is declaring 
variables at the top of the nearest enclosing block.  Which I
don't think Pascal can do anyway, aren't they limited to the 
enclosing function/procedure?

>As someone who has a few years real-world programming experience with
>each of C and Pascal, the ONLY times I've ever wished for nested
>procedures in C involved a combination of:
> - Repetitive operations of the same pattern within a single function,
>   that weren't repeated in any other function.

static function.

> - A lot of function-specific state information (i.e.- local variables)
>   required for this operation... too many to make each one an argument
>   of a procedure.

struct.

>In such cases, I use macros in C; not as elegant as nested procedures,
>but they do the job.

that works too.  (actually I use macros more than static functions, 
but then I don't always care about poluting the name space as much
as I should...)



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Blasting, bursting, billowing forth with	|
the power of ten billion butterfly sneezes,	|	-Richard Hartman	
Man, with his flaming fire,			|	hartman@uLogic.COM
has conquered the wayword breezes.		|
----- News saved at 12 Jan 93 17:04:17 GMT
In article <1993Jan11.131838.12844@cbnewsj.cb.att.com> davet@cbnewsj.cb.att.com (Dave Tutelman) writes:
>In article <FISCHER.93Jan9030817@orange.iesd.auc.dk> fischer@iesd.auc.dk (Lars Peter Fischer) writes:
>>
>>>>>>> "Michael" == Michael Malak (malak@grebyn.com)
>>>>>>> "Lars" == fischer@iesd.auc.dk (Lars Peter Fischer)
>>>>>>> "John" == John MAX Skaller (maxtal@extro.ucc.su.OZ.AU)
>>
>>John> 	Seems to me that the simple concept of
>>John> nesting the symbol table so that symbols go out of scope at the
>>John> end of the block is very nice.
>
>C and C++ have this, too.
>
>My organization's coding standard (adopted explicitly to make our software
>easier to read and maintain) calls for declaring and initializing
>variables as close as possible to their point of first use.  In the ability
>to meet this goal:
>	C++ good.
>	Pascal bad.

I am a C person and I have to disagree with your standard.  In C++
(and, I think, ANSI C although I am not certain) it is possible to
declare new variables almost anywhere.  e.g.:

	main()
	{
	cout << "stuff" << endl;
	int i = 2;
	cout << "i is " << i << endl;
	}

This, you would agree, is declaring as close to the usage as possible,
but let's say the function is a little longer than that.  I want to
add a loop w/ the control variable "i".  Where can I check to see if
it has already been declared or not if all the variable declarations
are scattered throughout the code?

The biggest concession I will make to this practice is declaring 
variables at the top of the nearest enclosing block.  Which I
don't think Pascal can do anyway, aren't they limited to the 
enclosing function/procedure?

>As someone who has a few years real-world programming experience with
>each of C and Pascal, the ONLY times I've ever wished for nested
>procedures in C involved a combination of:
> - Repetitive operations of the same pattern within a single function,
>   that weren't repeated in any other function.

static function.

> - A lot of function-specific state information (i.e.- local variables)
>   required for this operation... too many to make each one an argument
>   of a procedure.

struct.

>In such cases, I use macros in C; not as elegant as nested procedures,
>but they do the job.

that works too.  (actually I use macros more than static functions, 
but then I don't always care about poluting the name space as much
as I should...)



=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Blasting, bursting, billowing forth with	|
the power of ten billion butterfly sneezes,	|	-Richard Hartman	
Man, with his flaming fire,			|	hartman@uLogic.COM
has conquered the wayword breezes.		|
Technical Details
Message-ID: <853@ulogic.UUCP>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
References: <1992Dec31.043002.24014@ucc.su.OZ.AU> <FISCHER.93Jan9030817@orange.iesd.auc.dk> <1993Jan11.131838.12844@cbnewsj.cb.att.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
From: hartman@ulogic.UUCP (Richard M. Hartman)
Newsgroups: comp.os.msdos.programmer,comp.lang.c++,comp.lang.c
Subject: Re: Newbie Wants Advice on C-Programming
Message-ID: <853@ulogic.UUCP>
Date: 12 Jan 93 17:05:24 GMT
References: <1992Dec31.043002.24014@ucc.su.OZ.AU> <FISCHER.93Jan9030817@orange.iesd.auc.dk> <1993Jan11.131838.12844@cbnewsj.cb.att.com>
Followup-To: comp.os.msdos.programmer
Organization: negligable
Lines: 142