Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #72400 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-10-10 10:28 -0700 |
| Last post | 2015-10-18 20:44 -0700 |
| Articles | 20 on this page of 140 — 23 participants |
Back to article view | Back to comp.lang.c
Program to count words, issues with an if condition within a for loop Alla _ <modelling.data@gmail.com> - 2015-10-10 10:28 -0700
Re: Program to count words, issues with an if condition within a for loop Stephen Fisher <sfisher@panix.com> - 2015-10-10 18:53 +0000
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-10 22:23 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-10 23:13 +0200
Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-10 22:04 +0100
Re: Operating Systems in Teaching Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 14:12 -0700
Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-10 22:30 +0100
Re: Operating Systems in Teaching Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-10 18:25 -0400
Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-11 15:02 +0100
Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-11 16:39 +0100
Re: Operating Systems in Teaching Melzzzzz <mel@zzzzz.com> - 2015-10-11 17:50 +0200
Re: Operating Systems in Teaching "Osmium" <r124c4u102@comcast.net> - 2015-10-11 11:29 -0500
Re: Operating Systems in Teaching "Osmium" <r124c4u102@comcast.net> - 2015-10-11 11:48 -0500
Re: Operating Systems in Teaching Bartc <bc@freeuk.com> - 2015-10-11 18:18 +0100
Re: Operating Systems in Teaching "Osmium" <r124c4u102@comcast.net> - 2015-10-11 12:46 -0500
Re: Operating Systems in Teaching Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-10-11 13:57 -0400
Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-11 10:21 +1300
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 14:55 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 00:35 +0200
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 00:01 +0100
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 16:21 -0700
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 01:24 +0100
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-10 17:56 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 03:17 +0200
Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-11 14:43 +1300
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 00:19 -0700
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 11:02 +0100
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 03:47 -0700
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 16:05 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 17:38 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 18:49 +0200
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 12:37 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 21:56 +0200
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 13:34 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-12 10:11 +0200
Re: Program to count words, issues with an if condition within a for loop Geoff <geoff@invalid.invalid> - 2015-10-11 12:59 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 19:15 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 12:48 +0200
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 03:59 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 13:30 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 14:19 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 14:48 +0200
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-11 12:20 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 13:49 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 13:55 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 02:40 +0200
Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-11 14:40 +1300
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-10 22:29 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 00:22 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 00:57 +0200
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 00:46 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 02:23 +0200
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 01:28 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 04:21 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 04:32 +0200
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-11 11:06 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-11 02:55 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:36 -0700
Re: Program to count words, issues with an if condition within a for loop Geoff <geoff@invalid.invalid> - 2015-10-15 14:37 -0700
Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:15 +0000
Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-12 10:37 +0200
Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-12 11:08 +0200
Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:27 +0200
Re: Program to count words, issues with an if condition within a for loop Tonton Th <tTh@nowhere.invalid> - 2015-10-14 14:28 +0000
Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:36 +0200
Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:40 +0200
Re: Program to count words, issues with an if condition within a for loop Rosario19 <Ros@invalid.invalid> - 2015-10-14 16:49 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 14:09 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-16 12:49 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-19 14:28 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 02:03 +0200
Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-20 13:13 +1300
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-19 20:07 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 14:30 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 15:08 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 12:18 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 03:03 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 12:21 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 15:16 +0100
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 22:29 -0700
Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:26 +0000
Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-11 14:09 -0700
Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-14 11:36 +0000
Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-14 10:23 -0700
Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-20 16:06 +0000
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-12 00:19 +0200
Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-11 17:53 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-12 04:15 +0200
Re: Program to count words, issues with an if condition within a for loop Alla _ <modelling.data@gmail.com> - 2015-10-13 08:44 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 07:27 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 08:53 +0200
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 11:13 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 12:35 +0200
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 13:08 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-14 18:11 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 04:58 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-16 15:07 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-16 23:50 +0200
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-17 00:06 +0200
Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-16 15:30 -0700
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-19 13:45 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 02:40 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 12:47 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 15:36 +0100
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 22:37 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 15:52 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 19:10 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-25 19:17 +0100
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 23:05 -0700
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-28 03:59 -0700
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-29 14:04 +0100
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-29 01:07 +0100
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-30 08:31 -0700
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-27 22:48 -0700
Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-20 08:55 +0000
Re: Program to count words, issues with an if condition within a for loop regis <regis.barbanchon@free.fr> - 2015-10-20 13:52 +0200
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-24 14:04 -0700
Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-25 06:44 +0000
Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-10-25 09:09 +0000
Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-25 10:12 +0000
Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-25 03:26 -0700
Re: Program to count words, issues with an if condition within a for loop Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-25 06:38 -0700
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-25 15:23 +0000
Re: Program to count words, issues with an if condition within a for loop supercat@casperkitty.com - 2015-10-25 10:54 -0700
Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-25 12:21 -0700
Re: Program to count words, issues with an if condition within a for loop supercat@casperkitty.com - 2015-10-25 14:16 -0700
Re: Program to count words, issues with an if condition within a for loop Keith Thompson <kst-u@mib.org> - 2015-10-25 14:52 -0700
Re: Program to count words, issues with an if condition within a for loop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 00:58 +0000
Re: Program to count words, issues with an if condition within a for loop Bartc <bc@freeuk.com> - 2015-10-26 11:50 +0000
Re: Program to count words, issues with an if condition within a for loop raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:17 +0000
Re: Program to count words, issues with an if condition within a for loop Les Cargill <lcargill99@comcast.com> - 2015-11-10 19:27 -0600
Re: Program to count words, issues with an if condition within a for loop Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-11 07:39 +0000
Re: Program to count words, issues with an if condition within a for loop Les Cargill <lcargill99@comcast.com> - 2015-11-11 08:22 -0600
Re: Program to count words, issues with an if condition within a for loop Robert Wessel <robertwessel2@yahoo.com> - 2015-11-11 00:37 -0600
Re: Program to count words, issues with an if condition within a for loop Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 08:52 -0700
Re: Program to count words, issues with an if condition within a for loop Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 12:31 -0700
Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-18 15:28 -0700
Re: Program to count words, issues with an if condition within a for loop Ian Collins <ian-news@hotmail.com> - 2015-10-19 11:40 +1300
Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-18 20:41 -0700
Re: Program to count words, issues with an if condition within a for loop asetofsymbols@gmail.com - 2015-10-18 20:44 -0700
Page 2 of 7 — ← Prev page 1 [2] 3 4 5 6 7 Next page →
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-10 16:21 -0700 |
| Message-ID | <6752a4b1-45b5-4912-aa5c-2d5562215817@googlegroups.com> |
| In reply to | #72433 |
On Sunday, October 11, 2015 at 12:01:30 AM UTC+1, Ben Bacarisse wrote:
> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>
> > On Saturday, October 10, 2015 at 10:21:51 PM UTC+1, Ian Collins wrote:
> >>
> >> A small point of style: you are using C99, so you can save clutter by
> >> restricting the loop counter scope to the loop:
> >>
> >> for (int i = 0; string[i] != '\0'; i++)
> >>
> > I find I use the final value of the index counter frequently enough
> > to mean i doesn't have enough scope, and it's better if all
> > the loops look the same.
>
> How odd. That's exactly the reason I'd want to make those loops look
> different.
>
A typical bit of code goes
/*
close the still-open parentheses in an expression
*/
void closeparens(char *str)
{
int Nopen = 0;
int i;
for(i=0;str[i];i++)
{
if(str[i] == '(')
Nopen++;
else if(str[i] == ')')
Nopen--;
assert(Nopen >= 0);
}
while(Nopen--)
str[i++] = ')';
str[i] = 0;
}
However you might easily want to go to dynamic buffers.
In this case, the function is simple enough that we'd just rewrite
from scratch, but let's suppose that the in-loop logic is bit
more tricky, and we need to copy and paste.
So it would become
char *closeparens(char *str)
{
char *answer, *closures;
int Nopen = 0;
int i;
for(i=0;str[i];i++)
{
if(str[i] == '(')
Nopen++;
else if(str[i] == ')')
Nopen--;
assert(Nopen >= 0);
}
closures = repeat(')', Nopen);
answer = concatenate(str, closures);
free(closures);
return answer;
}
we haven't made the function incorrect by failing to use the index
variable. But we've now made the loop use the wrong style, if you
insist on every variable having the smallest possible scope.
It becomes more of a grief as you scale the function complexity up.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-11 01:24 +0100 |
| Message-ID | <87d1wmgskm.fsf@bsb.me.uk> |
| In reply to | #72434 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Sunday, October 11, 2015 at 12:01:30 AM UTC+1, Ben Bacarisse wrote:
>> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>>
>> > On Saturday, October 10, 2015 at 10:21:51 PM UTC+1, Ian Collins wrote:
>> >>
>> >> A small point of style: you are using C99, so you can save clutter by
>> >> restricting the loop counter scope to the loop:
>> >>
>> >> for (int i = 0; string[i] != '\0'; i++)
>> >>
>> > I find I use the final value of the index counter frequently enough
>> > to mean i doesn't have enough scope, and it's better if all
>> > the loops look the same.
>>
>> How odd. That's exactly the reason I'd want to make those loops look
>> different.
>>
> A typical bit of code goes
>
> /*
> close the still-open parentheses in an expression
> */
> void closeparens(char *str)
> {
> int Nopen = 0;
> int i;
>
> for(i=0;str[i];i++)
> {
> if(str[i] == '(')
> Nopen++;
> else if(str[i] == ')')
> Nopen--;
> assert(Nopen >= 0);
> }
> while(Nopen--)
> str[i++] = ')';
> str[i] = 0;
> }
>
> However you might easily want to go to dynamic buffers.
>
> In this case, the function is simple enough that we'd just rewrite
> from scratch, but let's suppose that the in-loop logic is bit
> more tricky, and we need to copy and paste.
> So it would become
>
> char *closeparens(char *str)
> {
> char *answer, *closures;
> int Nopen = 0;
> int i;
>
> for(i=0;str[i];i++)
> {
> if(str[i] == '(')
> Nopen++;
> else if(str[i] == ')')
> Nopen--;
> assert(Nopen >= 0);
> }
> closures = repeat(')', Nopen);
> answer = concatenate(str, closures);
> free(closures);
> return answer;
> }
>
> we haven't made the function incorrect by failing to use the index
> variable. But we've now made the loop use the wrong style, if you
> insist on every variable having the smallest possible scope.
I don't insist at all. I'm just saying what I think is more helpful to
the reader. I'd write something like
char *closeparens(const char *str)
{
int n_open = 0;
for (int i = 0; str[i]; i++)
n_open += (str[i] == '(') - (str[i] == ')');
char *closures = repeat(')', n_open);
char *answer = concatenate(str, closures);
free(closures);
return answer;
}
> It becomes more of a grief as you scale the function complexity up.
I like to tidy up scopes as functions get edited and altered, but with
lots of little functions it's rarely much grief.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-10 17:56 -0700 |
| Message-ID | <9bf9dec8-9dad-4efc-9337-d5d8f8aca26b@googlegroups.com> |
| In reply to | #72439 |
On Sunday, October 11, 2015 at 1:25:07 AM UTC+1, Ben Bacarisse wrote: > Malcolm McLean <malcolm.mclean5@btinternet.com> writes: > > > It becomes more of a grief as you scale the function complexity up. > > I like to tidy up scopes as functions get edited and altered, but with > lots of little functions it's rarely much grief. > But it's not always sensible to write code as lots of little functions. A major problem is "Russian doll" programming, where one user-defined function just calls another and another, doing trivial processing. Since the functions are user-defined, it's not clear what they actually do, and the reader is hopping about in the source, not getting any real answers.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 03:17 +0200 |
| Message-ID | <5619b89c$0$3856$426a74cc@news.free.fr> |
| In reply to | #72445 |
On 10/11/15 02:56, Malcolm McLean wrote: > But it's not always sensible to write code as lots of little > functions. > A major problem is "Russian doll" programming, where one user-defined > function just calls another and another, doing trivial processing. > Since the functions are user-defined, it's not clear what they > actually do, and the reader is hopping about in the source, not > getting any real answers. > Only if they are named with no methodology or if they have no defined single goal, which usually comes together. The role of a function is precisely to isolate a single goal, hide how this goal is achieved, and reveal the goal by its name. Even for the not so great name sqrt(), you don't hop in its source to look how it is implemented. It could be written 100 nested russian dolls, that it would not matter, because we don't even care to open the first doll~: we know it computes the square root by its name and that's all we need to know to read the next line. -- regis
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2015-10-11 14:43 +1300 |
| Subject | Re: Program to count words, issues with an if condition within a for loop |
| Message-ID | <d7tt5sFj978U3@mid.individual.net> |
| In reply to | #72447 |
regis wrote: > On 10/11/15 02:56, Malcolm McLean wrote: >> But it's not always sensible to write code as lots of little >> functions. >> A major problem is "Russian doll" programming, where one user-defined >> function just calls another and another, doing trivial processing. >> Since the functions are user-defined, it's not clear what they >> actually do, and the reader is hopping about in the source, not >> getting any real answers. >> > > Only if they are named with no methodology > or if they have no defined single goal, > which usually comes together. > > The role of a function is precisely to isolate a single goal, > hide how this goal is achieved, and reveal the goal by its name. > > Even for the not so great name sqrt(), you don't hop in its source > to look how it is implemented. It could be written 100 nested > russian dolls, that it would not matter, because we don't even > care to open the first doll~: we know it computes the square root > by its name and that's all we need to know to read the next line. Indeed. The alternative is the pages long functions those of us who have to deal with legacy code have to suffer. I would also add that small, well named functions are often good candidates for collecting together in libraries for later reuse. -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-11 00:19 -0700 |
| Message-ID | <1be5c97d-c2a4-4d21-b695-41b3b8799a76@googlegroups.com> |
| In reply to | #72447 |
On Sunday, October 11, 2015 at 2:17:40 AM UTC+1, regis wrote: > On 10/11/15 02:56, Malcolm McLean wrote: > > But it's not always sensible to write code as lots of little > > functions. > > A major problem is "Russian doll" programming, where one user-defined > > function just calls another and another, doing trivial processing. > > Since the functions are user-defined, it's not clear what they > > actually do, and the reader is hopping about in the source, not > > getting any real answers. > > > > Only if they are named with no methodology > or if they have no defined single goal, > which usually comes together. > > The role of a function is precisely to isolate a single goal, > hide how this goal is achieved, and reveal the goal by its name. > > Even for the not so great name sqrt(), you don't hop in its source > to look how it is implemented. It could be written 100 nested > russian dolls, that it would not matter, because we don't even > care to open the first doll~: we know it computes the square root > by its name and that's all we need to know to read the next line. > Everyone uses sqrt() as an example function. But it's not typical. It's a very basic mathematical operation, it returns a scalar and takes a single scalar, it's often embedded in expressions, everyone with even a small amount of experience understands what it is and what it does. And, as you say, the Russian doll problem doesn't apply. If you need to understand code, and you see return sqrt(a.x *b.x + a.y *b.y) you know that you've got a dot product. Similarly dot(&a, &b) is pretty harmless, as is length(&a). However when we get to anglebetween(Point2D &a, Point2D &b), the issue starts. Firstly, the angle between is defined for three points, not two, so pretty obviously we've been sloppy in the point / vector distinction. Secondly, are we looking for the signed angle, or the unsigned, the smaller or the clockwise / anticlockwise rotation from a to b? What does the function return if a or b are degenerate? Now we're in this position Point2D a = bullet.heading; Point2D b = spaceship.heading; theta = anglebetween(&a, &b); Matrix2D mtx = rotationmatrix(theta); for(i=0;i<bullet.mesh.Npoints;i++) mtx_mul(&bullet.mesh[i], &bullet.mesh[i], &mtx); if that doesn't do what you expect, where do you expect the problem to lie?
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-11 11:02 +0100 |
| Message-ID | <87wputg1ts.fsf@bsb.me.uk> |
| In reply to | #72455 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes: <snip> >> On 10/11/15 02:56, Malcolm McLean wrote: >> > But it's not always sensible to write code as lots of little >> > functions. >> > A major problem is "Russian doll" programming, where one user-defined >> > function just calls another and another, doing trivial processing. >> > Since the functions are user-defined, it's not clear what they >> > actually do, and the reader is hopping about in the source, not >> > getting any real answers. <snip> > And, as you say, the Russian doll problem doesn't apply. If you > need to understand code, and you see return sqrt(a.x *b.x + a.y *b.y) > you know that you've got a dot product. > > Similarly dot(&a, &b) is pretty harmless, as is length(&a). However > when we get to anglebetween(Point2D &a, Point2D &b), the issue starts. > Firstly, the angle between is defined for three points, not two, > so pretty obviously we've been sloppy in the point / vector distinction. > Secondly, are we looking for the signed angle, or the unsigned, > the smaller or the clockwise / anticlockwise rotation from a to b? > What does the function return if a or b are degenerate? > > Now we're in this position > > Point2D a = bullet.heading; > Point2D b = spaceship.heading; > theta = anglebetween(&a, &b); > Matrix2D mtx = rotationmatrix(theta); > for(i=0;i<bullet.mesh.Npoints;i++) > mtx_mul(&bullet.mesh[i], &bullet.mesh[i], &mtx); > > if that doesn't do what you expect, where do you expect the problem > to lie? Now write that out without the little functions and explain why the result is any easier to understand and debug. And how do did anglebetween, rotationmatrix and mtx_mul pass their tests? What would you test in the version without the little functions? -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-11 03:47 -0700 |
| Message-ID | <4a8e7e6d-a1c5-4479-b850-7d19ab918e50@googlegroups.com> |
| In reply to | #72458 |
On Sunday, October 11, 2015 at 11:02:55 AM UTC+1, Ben Bacarisse wrote:
> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> <snip>
> >> On 10/11/15 02:56, Malcolm McLean wrote:
> >> > But it's not always sensible to write code as lots of little
> >> > functions.
> >> > A major problem is "Russian doll" programming, where one user-defined
> >> > function just calls another and another, doing trivial processing.
> >> > Since the functions are user-defined, it's not clear what they
> >> > actually do, and the reader is hopping about in the source, not
> >> > getting any real answers.
> <snip>
> > And, as you say, the Russian doll problem doesn't apply. If you
> > need to understand code, and you see return sqrt(a.x *b.x + a.y *b.y)
> > you know that you've got a dot product.
> >
> > Similarly dot(&a, &b) is pretty harmless, as is length(&a). However
> > when we get to anglebetween(Point2D &a, Point2D &b), the issue starts.
> > Firstly, the angle between is defined for three points, not two,
> > so pretty obviously we've been sloppy in the point / vector distinction.
> > Secondly, are we looking for the signed angle, or the unsigned,
> > the smaller or the clockwise / anticlockwise rotation from a to b?
> > What does the function return if a or b are degenerate?
> >
> > Now we're in this position
> >
> > Point2D a = bullet.heading;
> > Point2D b = spaceship.heading;
> > theta = anglebetween(&a, &b);
> > Matrix2D mtx = rotationmatrix(theta);
> > for(i=0;i<bullet.mesh.Npoints;i++)
> > mtx_mul(&bullet.mesh[i], &bullet.mesh[i], &mtx);
> >
> > if that doesn't do what you expect, where do you expect the problem
> > to lie?
>
> Now write that out without the little functions and explain why the
> result is any easier to understand and debug.
>
> And how do did anglebetween, rotationmatrix and mtx_mul pass their
> tests? What would you test in the version without the little functions?
>
Point2D a = bullet.heading;
Point2D b = spaceship.heading;
len = sqrt(a.x*a.x + a.y*a.y);
a.x /= len; a.y/= len;
len = sqrt(b.x*b.x + b.y*b.y);
b.x /= len; b.y /= len;
costheta = a.x * b.x + a.y * b.y;
side = a.x * b.y - a.x * b.y > 0 ? 1 : -1;
sintheta = sqrt(1 - costheta * costheta);
sintheta *= side;
for(i=0;i<bullet.mesh.Npoints;i++)
{
double tx, ty;
tx = bullet.mesh[i].y * costheta - bullet.mesh[i].y * sintheta;
ty = bullet.mesh[i].x * sintheta + bullet.mesh[i].y * costheta;
bullet.mesh[i].x = tx;
bullet.mesh[i].y = ty;
}
Now we can see where the errors are likely to be
1) how are we going to normalise the heading vectors if they are zero?
2) what's our convention for rotation, and what's the direction - it's
extremely easy to get a sign flipped somewhere.
3) we need a temporary to hold the intermediate rotations.
Those are the three points where the previous function is likely to be wrong.
Also, you'll notice that this code is quite a bit more efficient than the
decomposed code. We're not calling a trig function, just a square root, which is
likely to be faster, and we're not doing unnecessary null translations. Also
of course we have guarantee that the normalisation and rotation is inlined.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-11 16:05 +0100 |
| Message-ID | <87egh1fntk.fsf@bsb.me.uk> |
| In reply to | #72464 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Sunday, October 11, 2015 at 11:02:55 AM UTC+1, Ben Bacarisse wrote:
>> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>> <snip>
>> >> On 10/11/15 02:56, Malcolm McLean wrote:
>> >> > But it's not always sensible to write code as lots of little
>> >> > functions.
>> >> > A major problem is "Russian doll" programming, where one user-defined
>> >> > function just calls another and another, doing trivial processing.
>> >> > Since the functions are user-defined, it's not clear what they
>> >> > actually do, and the reader is hopping about in the source, not
>> >> > getting any real answers.
>> <snip>
>> > And, as you say, the Russian doll problem doesn't apply. If you
>> > need to understand code, and you see return sqrt(a.x *b.x + a.y *b.y)
>> > you know that you've got a dot product.
>> >
>> > Similarly dot(&a, &b) is pretty harmless, as is length(&a). However
>> > when we get to anglebetween(Point2D &a, Point2D &b), the issue starts.
>> > Firstly, the angle between is defined for three points, not two,
>> > so pretty obviously we've been sloppy in the point / vector distinction.
>> > Secondly, are we looking for the signed angle, or the unsigned,
>> > the smaller or the clockwise / anticlockwise rotation from a to b?
>> > What does the function return if a or b are degenerate?
>> >
>> > Now we're in this position
>> >
>> > Point2D a = bullet.heading;
>> > Point2D b = spaceship.heading;
>> > theta = anglebetween(&a, &b);
>> > Matrix2D mtx = rotationmatrix(theta);
>> > for(i=0;i<bullet.mesh.Npoints;i++)
>> > mtx_mul(&bullet.mesh[i], &bullet.mesh[i], &mtx);
>> >
>> > if that doesn't do what you expect, where do you expect the problem
>> > to lie?
>>
>> Now write that out without the little functions and explain why the
>> result is any easier to understand and debug.
>>
>> And how do did anglebetween, rotationmatrix and mtx_mul pass their
>> tests? What would you test in the version without the little functions?
>>
>
> Point2D a = bullet.heading;
> Point2D b = spaceship.heading;
>
> len = sqrt(a.x*a.x + a.y*a.y);
> a.x /= len; a.y/= len;
> len = sqrt(b.x*b.x + b.y*b.y);
> b.x /= len; b.y /= len;
> costheta = a.x * b.x + a.y * b.y;
> side = a.x * b.y - a.x * b.y > 0 ? 1 : -1;
> sintheta = sqrt(1 - costheta * costheta);
> sintheta *= side;
> for(i=0;i<bullet.mesh.Npoints;i++)
> {
> double tx, ty;
> tx = bullet.mesh[i].y * costheta - bullet.mesh[i].y * sintheta;
> ty = bullet.mesh[i].x * sintheta + bullet.mesh[i].y * costheta;
> bullet.mesh[i].x = tx;
> bullet.mesh[i].y = ty;
> }
>
> Now we can see where the errors are likely to be
This is one of those cases where the same evidence induces entirely
different conclusions. I suppose we can just let every one look and
decide for themselves!
I would much prefer to test, maintain and debug the first.
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 17:38 +0200 |
| Message-ID | <561a8269$0$3034$426a34cc@news.free.fr> |
| In reply to | #72464 |
On 10/11/15 12:47, Malcolm McLean wrote: > side = a.x * b.y - a.x * b.y > 0 ? 1 : -1; Typically there, the function name would be useful for the math expr before the ? operator. Because if the function name says it's a cross product, I could fix the error (you're subtracting the same product from itself). But I don't know if it' a cross product that is intended there. So now, It's mere guess work and I have to rely on context. So you're reversing the burden on your reader here. -- regis
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 18:49 +0200 |
| Message-ID | <561a9309$0$3062$426a74cc@news.free.fr> |
| In reply to | #72481 |
On 10/11/15 17:38, regis wrote: > On 10/11/15 12:47, Malcolm McLean wrote: > >> side = a.x * b.y - a.x * b.y > 0 ? 1 : -1; > > Typically there, the function name would be useful for the math expr > before the ? operator. > > Because if the function name says it's a cross product, > I could fix the error (you're subtracting the same product from itself). > > But I don't know if it' a cross product that is intended there. > So now, It's mere guess work and I have to rely on context. > So you're reversing the burden on your reader here. > also, what is the gotcha that prevents us to just use directly the cross product for computing sintheta ?
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-11 12:37 -0700 |
| Message-ID | <658f9f76-c332-433f-b345-253b26d220c9@googlegroups.com> |
| In reply to | #72487 |
On Sunday, October 11, 2015 at 5:49:21 PM UTC+1, regis wrote: > On 10/11/15 17:38, regis wrote: > > On 10/11/15 12:47, Malcolm McLean wrote: > > > >> side = a.x * b.y - a.x * b.y > 0 ? 1 : -1; > > > > Typically there, the function name would be useful for the math expr > > before the ? operator. > > > > Because if the function name says it's a cross product, > > I could fix the error (you're subtracting the same product from itself). > > > > But I don't know if it' a cross product that is intended there. > > So now, It's mere guess work and I have to rely on context. > > So you're reversing the burden on your reader here. > > > > also, what is the gotcha that prevents us > to just use directly the cross product for computing sintheta ? > Someone has started with a version that calls subroutines, realised that that it's not producing the right results, and stripped the subroutines out, but not done a thorough job on optimisation, presumably because he wants to fiddle with the sign because he suspects that's the source of the problem. He's also introduced bugs which are pretty clearly tying errors, it's obviously a first draft that probably hasn't even been compiled yet, much less tested. But you can now fix the code and even optimise it further. Which you couldn't with the previous version.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 21:56 +0200 |
| Message-ID | <561abef2$0$3360$426a74cc@news.free.fr> |
| In reply to | #72508 |
On 10/11/15 21:37, Malcolm McLean wrote:
> But you can now fix the code and even optimise it further.
> Which you couldn't with the previous version.
note that this version is still refactorable in small functions
that preserves the main design...
int
main (void) {
Point2D h= bullet.heading;
Point2D s= spaceship.heading;
double hLength= Point2D_length (h);
double sLength= Point2D_length (s);
Point2D hNormed= Point2D_dividedBy (h, hLength);
Point2D sNormed= Point2D_dividedBy (s, sLength);
Point2D rotator= Point2D_RotatorBetweenNormed (hNormed, sNormed);
Point2D_rotateAllBy (bullet.mesh, bullet.nPoints, rotator);
}
typedef struct {
double x, y;
} Point2D;
double Point2D_cross (Point2D u, Point2D v) {
return u.x * v.y - u.y * v.x;
}
double Point2D_dot (Point2D u, Point2D v) {
return u.x * v.x + u.y * v.y;
}
double Point2D_length2 (Point2D v) {
return Point2D_dot (v, v);
}
double Point2D_length (Point2D v) {
return sqrt (Point2D_length2 (v));
}
Point2D Point2D_dividedBy (Point2D v, double scalar) {
return (Point2D) { .x= v.x / scalar, .y= v.y / scalar };
}
double absSinFromCos (double cosAngle) {
return sqrt (1.0 - cosAngle * cosAngle);
}
Point2D
Point2D_RotatorBetweenNormed (Point2D normedU, Point2D normedV) {
double cosUV = Point2D_dot (normedU, normedV);
double sign = Point2D_cross (normedU, normedV) > 0 ? +1.0 : -1.0;
double sinUV = sign * absSinFromCos (cosUV);
// double sinUV= Point2D_cross (normedU, normedV); ???
return (Point2D) { .x= cosUV, .y= sinUV };
}
double
Point2D_xRotatedBy (Point2D p, Point2D rotator) {
return rotator.x * p.x - rotator.y * p.y;
}
double
Point2D_yRotatedBy (Point2D p, Point2D rotator) {
return rotator.y * p.x + rotator.x * p.y;
}
Point2D
Point2D_rotatedBy (Point2D p, Point2D rotator) {
double x= Point2D_xRotatedBy (p, rotator);
double y= Point2D_yRotatedBy (p, rotator);
return (Point2D) { .x= x, .y= y };
}
void
Point2D_rotateAllBy (Point2D mesh[], int count, Point2D rotator)
{
for(int k= 0; k < count; k++)
mesh[k]= Point2D_rotatedBy (mesh[k], rotator);
}
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-11 13:34 -0700 |
| Message-ID | <f7707f95-211f-4338-b361-00d38a518e28@googlegroups.com> |
| In reply to | #72509 |
On Sunday, October 11, 2015 at 8:56:47 PM UTC+1, regis wrote:
> On 10/11/15 21:37, Malcolm McLean wrote:
> > But you can now fix the code and even optimise it further.
> > Which you couldn't with the previous version.
>
> note that this version is still refactorable in small functions
> that preserves the main design...
>
>
> int
> main (void) {
> Point2D h= bullet.heading;
> Point2D s= spaceship.heading;
>
> double hLength= Point2D_length (h);
> double sLength= Point2D_length (s);
>
> Point2D hNormed= Point2D_dividedBy (h, hLength);
> Point2D sNormed= Point2D_dividedBy (s, sLength);
>
> Point2D rotator= Point2D_RotatorBetweenNormed (hNormed, sNormed);
> Point2D_rotateAllBy (bullet.mesh, bullet.nPoints, rotator);
> }
>
>
It's certainly possible to refactor functions to call more
efficient and better-named subroutines.
On the other hand, the interface to the last two functions is a bit
odd. You have to set up the "rotator", then pass it to rotateAllBy.
Then rotateAllBy only works if the mesh is a straight array of
points, but often meshes have colours or other data attached to the
point list, and fields names x, y. (Templates get round this
problem, but cause other issues).
In reality you probably want normalize, crossproduct and dotproduct
as subroutines, but not the rest. I think that hist the sweet spot,
between too much hiding (as in example one), and no user-defined
subroutines at all (as in example two).
Probably I'd define normalizex, which is a normalization function
that creates {1, 0} when passed a zero-length vector. That tends
to be the best answer. But that's the point where defining good
functions starts to become difficult.
> typedef struct {
> double x, y;
> } Point2D;
>
> double Point2D_cross (Point2D u, Point2D v) {
> return u.x * v.y - u.y * v.x;
> }
>
> double Point2D_dot (Point2D u, Point2D v) {
> return u.x * v.x + u.y * v.y;
> }
>
> double Point2D_length2 (Point2D v) {
> return Point2D_dot (v, v);
> }
>
> double Point2D_length (Point2D v) {
> return sqrt (Point2D_length2 (v));
> }
>
> Point2D Point2D_dividedBy (Point2D v, double scalar) {
> return (Point2D) { .x= v.x / scalar, .y= v.y / scalar };
> }
>
> double absSinFromCos (double cosAngle) {
> return sqrt (1.0 - cosAngle * cosAngle);
> }
>
> Point2D
> Point2D_RotatorBetweenNormed (Point2D normedU, Point2D normedV) {
> double cosUV = Point2D_dot (normedU, normedV);
> double sign = Point2D_cross (normedU, normedV) > 0 ? +1.0 : -1.0;
> double sinUV = sign * absSinFromCos (cosUV);
> // double sinUV= Point2D_cross (normedU, normedV); ???
> return (Point2D) { .x= cosUV, .y= sinUV };
> }
>
> double
> Point2D_xRotatedBy (Point2D p, Point2D rotator) {
> return rotator.x * p.x - rotator.y * p.y;
> }
>
> double
> Point2D_yRotatedBy (Point2D p, Point2D rotator) {
> return rotator.y * p.x + rotator.x * p.y;
> }
>
> Point2D
> Point2D_rotatedBy (Point2D p, Point2D rotator) {
> double x= Point2D_xRotatedBy (p, rotator);
> double y= Point2D_yRotatedBy (p, rotator);
> return (Point2D) { .x= x, .y= y };
> }
>
> void
> Point2D_rotateAllBy (Point2D mesh[], int count, Point2D rotator)
> {
> for(int k= 0; k < count; k++)
> mesh[k]= Point2D_rotatedBy (mesh[k], rotator);
> }
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-12 10:11 +0200 |
| Message-ID | <561b6b15$0$9230$426a74cc@news.free.fr> |
| In reply to | #72512 |
On 10/11/15 22:34, Malcolm McLean wrote: > On the other hand, the interface to the last two functions is a bit > odd. You have to set up the "rotator", then pass it to rotateAllBy. rotator is just the complex number that rotates another complex when it is multiplied with it (and scales it if not lying on the unit circle). The functions: double Point2D_xRotatedBy (Point2D p, Point2D rotator); double Point2D_yRotatedBy (Point2D p, Point2D rotator); Point2D Point2D_rotatedBy (Point2D p, Point2D rotator); could just be renamed: double Point2D_reComplexProduct (Point2D z1, Point2D z2); double Point2D_imComplexProduct (Point2D z1, Point2D z2); Point2D Point2D_complexProduct (Point2D z1, Point2D z2); You prolly want these operations as much as _dot, _cross, ... -- regis
[toc] | [prev] | [next] | [standalone]
| From | Geoff <geoff@invalid.invalid> |
|---|---|
| Date | 2015-10-11 12:59 -0700 |
| Message-ID | <6sfl1ble7i5j318mdm5n5g71ftfbai0o1j@4ax.com> |
| In reply to | #72508 |
On Sun, 11 Oct 2015 12:37:43 -0700 (PDT), Malcolm McLean <malcolm.mclean5@btinternet.com> wrote: >He's also introduced bugs which are pretty clearly tying errors, ----------------------------------------------------typing errors, >it's obviously a first draft that probably hasn't even been >compiled yet, much less tested. Yep.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 19:15 +0200 |
| Message-ID | <561a9948$0$4542$426a34cc@news.free.fr> |
| In reply to | #72464 |
On 10/11/15 12:47, Malcolm McLean wrote: > tx = bullet.mesh[i].y * costheta - bullet.mesh[i].y * sintheta; > ty = bullet.mesh[i].x * sintheta + bullet.mesh[i].y * costheta; mesh[i].x is used once and mesh[i].y is used 3 times ???
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 12:48 +0200 |
| Message-ID | <561a3e6d$0$3355$426a74cc@news.free.fr> |
| In reply to | #72455 |
On 10/11/15 09:19, Malcolm McLean wrote: > when we get to anglebetween(Point2D &a, Point2D &b), the issue starts. > Firstly, the angle between is defined for three points, not two, > so pretty obviously we've been sloppy in the point / vector distinction. You're just saying that you should fix the naming. > Secondly, are we looking for the signed angle, or the unsigned, > the smaller or the clockwise / anticlockwise rotation from a to b? Same. Should also be reflected in the naming. > What does the function return if a or b are degenerate? This one is usually not reflected in the naming. We can't have all. The function call hides this detail but it reveals at least that we trying to compute this angle. Would I be able to derive this goal the calls substituted with the body of the function ? > Now we're in this position > > Point2D a = bullet.heading; > Point2D b = spaceship.heading; > theta = anglebetween(&a, &b); > Matrix2D mtx = rotationmatrix(theta); > for(i=0;i<bullet.mesh.Npoints;i++) > mtx_mul(&bullet.mesh[i], &bullet.mesh[i], &mtx); > > if that doesn't do what you expect, where do you expect the problem > to lie? First, we expect nothing. It's the signature of the function containing these 6 lines that should tell us what the author expects these 6 lines to do, which you did not provide. Second, you seem to suggest I would be in better position if all these 3 function calls all substituted with their bodies. At least here, we have some hint that an angle is computed, a rotation matrix created from this angle, and then applied. Would we be able to derive this with all the calls replaced by the body of their functions? Here, I'm puzzled by mtx_mul() seems to have points and matrix as arguments. So it's obviously not a matrix multiplication, as it would involve 2 matrices. The function is probably misnamed and is applying a transformation to a point instead. In this case, I would also have expected the point in 2nd arg to go after the matrix in 3rd arg. Also, I guess the extra arg in first position is the result you did not want to return using the return channel, a choice not consistent with the one you made previously with anglebetween() and rotationmatrix(). If a problem arise, I would expect that mtx_mul() was not designed to allow the result point to the same object as the point being read, as values are progressively overwritten during the computation. Anyway, the namings in this code do not seems to follow any consistent convention: Npoints, mtx_mul, rotationmatrix... -- regis
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-10-11 03:59 -0700 |
| Message-ID | <3dafd50c-4eae-4e4e-91c2-1a85b7294c0f@googlegroups.com> |
| In reply to | #72465 |
On Sunday, October 11, 2015 at 11:48:21 AM UTC+1, regis wrote: > On 10/11/15 09:19, Malcolm McLean wrote: > > If a problem arise, I would expect that mtx_mul() was not designed to > allow the result point to the same object as the point being read, > as values are progressively overwritten during the computation. > > Anyway, the namings in this code do not seems to follow any consistent > convention: Npoints, mtx_mul, rotationmatrix... > I nodded with mtx_mul. it should be called mul_point or something. In a real program, you'd be unlikely to do the same with something as basic as a transform function. But you could easily make a similar mistake giving a bad name to a more one-off function. It's hard to enforce a convention on something as nebulous as naming styles. In reality the geometry code is likely to be written by one person, the code to import and manage the spaceship meshes, by another, and the function that applies the transform to move the objects about by a third.
[toc] | [prev] | [next] | [standalone]
| From | regis <regis.barbanchon@free.fr> |
|---|---|
| Date | 2015-10-11 13:30 +0200 |
| Message-ID | <561a483a$0$3331$426a74cc@news.free.fr> |
| In reply to | #72466 |
On 10/11/15 12:59, Malcolm McLean wrote: >> >If a problem arise, I would expect that mtx_mul() was not designed to >> >allow the result point to the same object as the point being read, >> >as values are progressively overwritten during the computation. >> > >> >Anyway, the namings in this code do not seems to follow any consistent >> >convention: Npoints, mtx_mul, rotationmatrix... >> > > I nodded with mtx_mul. it should be called mul_point or something. I would use object notation, with class prefixes, so that we know who is the actor and responsible for the action. Here, the object responsible for applying the transformation is clearly the matrix, not the point. e.g: Point2D Matrix2D_transformedPoint (Matrix2D transformMatrix, Point2D point); Matrix2D Matrix2D_makeRotation (double angle); double Point2D_angleFromToAroundOrigin (Point2D start, Point2D end); double Point2D_angleFromToAround (Point2D start, Point2D end, Point2D center); > It's hard to enforce a convention on something as nebulous as naming styles. Java solved this problem 20 years ago ;o)
[toc] | [prev] | [next] | [standalone]
Page 2 of 7 — ← Prev page 1 [2] 3 4 5 6 7 Next page →
Back to top | Article view | comp.lang.c
csiph-web