Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #71592

Re: two's complement idea

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: two's complement idea
Date 2020-01-26 14:58 +0100
Organization A noiseless patient Spider
Message-ID <r0k5um$375$1@dont-email.me> (permalink)
References (10 earlier) <K2WWF.227060$vS6.23920@fx24.am4> <h93gm8F25s3U7@mid.individual.net> <h41XF.367918$Arx.178294@fx08.am4> <r0jumc$p8e$1@dont-email.me> <mQfXF.125675$Bf2.15315@fx39.am4>

Show all headers | View raw


On 26/01/2020 13:44, Bart wrote:
> On 26/01/2020 11:54, David Brown wrote:
>> On 25/01/2020 20:56, Bart wrote:
> 
>> Let's be clear here.  I "avoid using goto in C" in the same sense as I 
>> avoid writing 2000 line functions, avoid using variable names in 
>> Swahili, and avoid 8 layers of nested indentation.
>>
>> You make it sound like my code - being C - should naturally contain 
>> gotos, and I then go to extraordinary efforts to remove the gotos.  I 
>> don't.  They were never there.  I structure my code - gotos do not 
>> occur in structured code.  They occur only when you try to use a code 
>> structure that does not match the structure of the task.
> 
> No, the use-cases for goto are always there: 

No, they are not.

> common code in a function, 

There are lots of ways to handle common code.  I've never felt "goto" to 
be a good way.  (That doesn't mean I don't think it can't occur - merely 
that I don't think it is often a good way, and I've never felt its need 
in my own code.)


> nested loop control,

Gotos are most certainly not needed for that.

Again, I'd suggest that if you think "goto" is necessary (in manually 
written code) to handle the flow you need, then your code structure is 
probably not ideal.

And if you think it is the only efficient solution because extra tests, 
duplicated calculations, local flag variables, etc., would have a 
run-time cost, then you are not using the right tools or not using them 
in the right way.  Either efficiency doesn't matter, so the cost is 
irrelevant, or you use good tools and let the compiler handle the 
optimisation from flags to gotos.  It is very rare that you need maximal 
code efficiency and have no choice but to use poor tools.

> usual code flow

"goto" is not part of the usual code flow in any structured programming. 
  It is, at most, for very /unusual/ code flow requirements.

> (plus the temporary uses during 
> development which ought to be eventually removed but which doesn't 
> always happen).

If it ought to be removed, it ought to be removed.  "I should have done 
this but I didn't" is hardly an excuse!

And again, this shows your misunderstanding.  Avoiding goto is not about 
/removing/ gotos from code you write - it is about not writing "goto" in 
the first place.  And avoiding goto is not the aim in itself - avoiding 
unstructured coding is the aim (which itself is a consequence of the 
higher aim of writing clear, legible and maintainable code).

> 
> I don't know how /you/ get around those, except by preempting those 
> situations, which to me sounds like premature refactorisation.

I don't get myself into those situations.

My first programming language was BASIC, but I moved on to structured 
coding.  (And even in my BASIC programming, I preferred structured BASICs.)

> 
> (It's not as though my own code is bristling with gotos. Average 
> incidence is 1 per 4-500 lines. Here, in this rare example of manually 
> written C, there is one per 800 lines:
> 
>    https://github.com/sal55/langs/blob/master/bignum.c
>    https://github.com/sal55/langs/blob/master/bignum.h
> 

You don't use goto much, yet you think it is inconceivable that others 
don't use it at all?

> (No macros either in that project.)

Only you consider that a good thing.

> 
> The first one ought to be an extra loop (the sort that executes either 
> once, or twice), but I left like that as I thought it was clearer.
> 
> The second is for a nested loop exit, since the program was hand-ported 
> from my own language, where those 1600 lines of code have only the one 
> goto.

I haven't looked at your code.  But I have already mentioned that 
automated code generation and translation into C may be /good/ reasons 
for using goto.  The same would apply (but to a lesser extent) when you 
are trying to do manual literal translations from another language to C. 
  Literal translations rarely end up with good quality code as the result.

> 
> 
>>> The purpose of my mentioning Linux sources is that there are a range 
>>> of programming styles even in C: Linux kernel may be a prolific user 
>>> of gotos, DB is at the other end of the scale, and I would say 
>>> extreme (actually you can't be more extreme without using a negative 
>>> number of gotos!).
>>
>> I am "extreme" in the same way that people who don't eat Brussels 
>> sprouts are extreme.  I don't find "goto" useful in the coding I do, 
>> and I think any attempt to use it would make my code worse.
> 
> So do I; when I use 'goto', I feel bad about it. But not bad enough to 
> write more elaborate code to eliminate it, because /that/ would make it 
> worse. I might, however, think of a language features to eliminate that 
> use-case.
> 

I don't avoid goto because it makes me feel bad - I avoid bad coding 
because it is bad coding.  That naturally results in negligible use of 
goto, because goto has negligible use in good coding.

And C has plenty of ways to write code well (leading, of course, to 
plenty of ways to write code badly) - it doesn't need more.  If I want 
more ways to write code, I use a different language (like C++).  And 
while C++ certainly has improved by making it easier to structure code 
in different ways, none of these has been with the aim of "avoiding goto".


> Yesterday I looked at one 5000-line module (not C), which had 4 gotos. I 
> identified one that ought to have been replaced by a loop control 
> statement (to restart a loop; you don't have that in C). The other 3 
> could be replaced by the 'recase' feature I mentioned yesterday, but 
> which hasn't yet been rolled out to this language.
> 
> So I'm doing something about it, but in ways that make the code simpler 
> and clearer, not longer and with extra bits.
> 
>> I am also willing to accept that for some of the code you write, 
>> "goto" might be the best choice.  Certainly if you take as written all 
>> your other coding preferences (huge files, minimal locality, old C, no 
>> pre-processor, minimal types, etc.),
> 
> (I don't write huge files. What you have in mind are the monolithic 
> generated-C files that I've used for distribution, or possibly the 
> amalgamated original-source files, also generated, used to simplify 
> transmission (eg. upload to github or copy to an RPi via a memory-stick).
> 
> The average module size of my original source is around 1000 lines.)
> 

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 12:19 -0800
  Re: two's complement idea "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-11-29 00:14 +0100
    Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2019-11-29 09:15 +0100
      Re: two's complement idea Bo Persson <bo@bo-persson.se> - 2019-11-29 10:58 +0100
        Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2019-11-29 12:49 +0100
    Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-12-07 07:53 -0800
      Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2019-12-07 08:22 -0800
        Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-10 20:44 -0800
          Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-10 23:44 -0800
            Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-14 07:08 -0800
              Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-14 08:42 -0800
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-21 02:09 -0800
                Re: two's complement idea James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-21 05:14 -0800
                Re: two's complement idea Daniel <danielaparker@gmail.com> - 2020-01-21 13:08 -0800
                Re: two's complement idea James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-22 00:07 -0500
                Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-21 23:21 -0800
                Re: two's complement idea Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-22 01:51 -0800
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-22 11:28 +0100
                Re: two's complement idea Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-22 11:52 +0100
                Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-22 04:47 -0800
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 07:19 -0700
                Re: two's complement idea James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-04-02 08:07 -0700
                Re: two's complement idea boltar@nowhere.org - 2020-01-21 16:44 +0000
                Re: two's complement idea Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-21 20:01 +0000
                Re: two's complement idea boltar@nowhere.org - 2020-01-22 11:26 +0000
                Re: two's complement idea Christian Gollwitzer <auriocus@gmx.de> - 2020-01-22 20:04 +0100
                Re: two's complement idea Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-22 11:30 -0800
                Re: two's complement idea Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2020-01-22 19:57 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-23 10:03 +1300
                Re: two's complement idea boltar@nowhere.org - 2020-01-23 09:24 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-24 07:29 +1300
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-23 10:03 +1300
                Re: two's complement idea "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2020-01-23 10:09 +0100
                Re: two's complement idea boltar@nowhere.org - 2020-01-23 09:29 +0000
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-24 01:08 +0000
                Re: two's complement idea boltar@nowhere.org - 2020-01-24 12:27 +0000
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-24 01:06 +0000
                Re: two's complement idea Bo Persson <bo@bo-persson.se> - 2020-01-24 10:07 +0100
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-24 11:36 +0200
                Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-24 17:34 -0800
                Re: two's complement idea Siri Cruise <chine.bleu@yahoo.com> - 2020-01-24 17:57 -0800
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-24 12:19 +0000
                Re: two's complement idea Bo Persson <bo@bo-persson.se> - 2020-01-24 14:19 +0100
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-24 14:11 +0000
                Re: two's complement idea Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-24 17:26 +0000
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-24 17:35 +0000
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-24 22:27 +0200
                Re: two's complement idea James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-24 23:21 -0500
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-25 11:06 +1300
                Re: two's complement idea Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-24 22:12 +0000
                Re: two's complement idea James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-24 23:24 -0500
                Re: two's complement idea Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-25 09:04 +0000
                Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-25 04:15 -0800
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-25 19:55 +0100
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-25 00:05 +0100
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-25 01:19 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-25 19:50 +1300
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-25 11:57 +0000
                Re: two's complement idea Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2020-01-25 13:42 +0000
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-25 14:58 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-26 07:58 +1300
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-25 20:34 +0100
                Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-25 12:41 -0800
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-26 16:08 +0200
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-27 02:20 -0800
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-27 15:03 +0200
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-15 06:44 -0800
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-03-06 10:18 +0200
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-02 07:05 -0700
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-04-02 20:03 +0300
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-04 12:37 -0700
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-26 07:45 +1300
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-25 19:56 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-26 09:49 +1300
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-25 21:33 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-26 11:00 +1300
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-25 22:23 +0000
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-26 11:46 +1300
                Re: two's complement idea Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-26 10:55 +0200
                Re: two's complement idea Öö Tiib <ootiib@hot.ee> - 2020-01-26 03:47 -0800
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-27 01:28 -0800
                Re: two's complement idea Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-25 22:22 +0000
                Re: two's complement idea James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-25 18:24 -0500
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-26 12:54 +0100
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-26 12:44 +0000
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-26 14:58 +0100
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-26 14:53 +0000
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-26 19:10 +0100
                Re: two's complement idea Robert Wessel <robertwessel2@yahoo.com> - 2020-01-26 13:20 -0600
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-26 20:51 +0000
                Re: two's complement idea Robert Wessel <robertwessel2@yahoo.com> - 2020-01-26 21:02 -0600
                Re: two's complement idea Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-26 19:06 -0800
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-27 18:50 +0000
                Re: two's complement idea Robert Wessel <robertwessel2@yahoo.com> - 2020-01-27 14:18 -0600
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-27 22:13 +0100
                Re: two's complement idea Bart <bc@freeuk.com> - 2020-01-27 21:38 +0000
                Re: two's complement idea Robert Wessel <robertwessel2@yahoo.com> - 2020-01-27 19:37 -0600
                Re: two's complement idea Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-27 19:15 -0800
                Re: two's complement idea "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2020-01-28 11:45 +0100
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-27 16:31 +1300
                Re: two's complement idea Manfred <noname@add.invalid> - 2020-01-26 18:50 +0100
                Re: two's complement idea Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-26 18:57 +0000
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-26 20:46 +0100
                Re: two's complement idea Robert Wessel <robertwessel2@yahoo.com> - 2020-01-26 13:14 -0600
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-26 22:15 +0100
                Re: two's complement idea Robert Wessel <robertwessel2@yahoo.com> - 2020-01-27 21:06 -0600
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-28 13:08 +0100
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-27 01:50 -0800
                Re: two's complement idea Ian Collins <ian-news@hotmail.com> - 2020-01-27 08:21 +1300
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-25 20:14 +0100
                Re: two's complement idea David Brown <david.brown@hesbynett.no> - 2020-01-25 20:12 +0100
                Re: two's complement idea Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-27 07:48 -0800
                Re: two's complement idea Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-18 18:46 +0100

csiph-web