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


Groups > comp.lang.c > #56029

Re: Is this poor style?

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Is this poor style?
Date 2015-01-08 11:10 +1300
Message-ID <ch5p66FpgmlU8@mid.individual.net> (permalink)
References (2 earlier) <57491de8-6480-47d1-b876-86236262e3da@googlegroups.com> <Y_kqw.363096$be7.149990@fx14.am4> <Dolqw.330807$fX6.290031@fx26.am4> <slrnmam2fv.4et.grahn+nntp@frailea.sa.invalid> <sFPqw.487139$7A5.252993@fx17.am4>

Show all headers | View raw


BartC wrote:
> On 05/01/2015 22:02, Jorgen Grahn wrote:
>> On Mon, 2015-01-05, BartC wrote:
>>> On 05/01/2015 00:35, Richard Heathfield wrote:
>> ...
>>>> Whilst it is certainly true that I've heard that goto isn't good, I actually
>>>> have personal experience of its not being good.
>>>
>>> You might, nevertheless, prefer reading other people's code that uses
>>> gotos sensibly and consistently, to alternates that involve tortuous
>>> logic or where the action keep shifting to dozens of extra functions
>>> (with the complicated interfaces required to allow them to share common
>>> data).
>>
>> I think I was the one who mentioned using functional decomposition
>> instead of goto.  I can assure you, that doesn't end up as "dozens of
>> extra functions", "complicated interfaces" or shared common data.
>
> Could you take any function, and turn any block of block within it into
> a second function? Could you do that several times to create several new
> functions?

Yes, it is a common refactoring known as "extract method".

> I would have these problems:
>
> (1) I would need to think of meaningful names for these function, ones
> that are unique because now I'm in the global name-space.

Not if they are static.  If the block of code is doing something 
specific (which is why you are extracting it), choosing a name should be 
straightforward.

> (2) I would need to decide where to place those functions. Perhaps the
> original function was part of nice, tidy cluster with related
> functionality, but it's not going to be tidy much longer!

Indeed, it will be tidier once all the clutter is removed...

> (3) I'd need to document the functions and explain why each is only
> called from one place. Also what it actually does which is harder
> because it might not be meaningful outside its original context

Very often the cue to extract a function is the need to comment a block 
of code, so (if it is still required) the comment can go with it. 
Choosing a meaningful name for the function will often remove the need 
for the comment.  Why would you have to explain why it is called from 
one place?

> (4) I'd have to document the new calls in the original function.

Why?  Isn't calling them clear enough?

> (5) If the blocks of code made use of local variables (using or setting
> their values) then this is where the interface between the original
> function and the new splinter functions can get awkward (not to mention
> inefficient unless inlined. But relying on inlining a function you went
> to all that trouble to 'out-line' seems perverse!).

In that case there are a couple of options:

1) do nothing
2) if there aren't too many, pass the local variables as parameters
3) if there will be several functions working on the same data, consider 
using a struct.

> (6) If the code is under development, this gets more hairy if it now
> involves statements being moved across functions. (Imagine trying to
> reorganise your bookshelves but half your books are now with your
> neighbours.)

Eh?

If the code is under development, isn't it easier to test smaller chunks?

> (7) If I want to move the entire function, I now have all these extra
> dependencies. And any tools that lists all the functions is going to
> cluttered with all these new functions with ill-defined roles.

Surely the roles are well defined, which is why you extracted the 
functions in the first place.

> In short, creating all this mess for the sake of a avoiding gotos is
> crazy. (There are ways of encapsulating blocks of code but keeping them
> within the function, such as local functions or something like the 'flow
> blocks' that Rick was on about, but those are not standard.)

More often than not functions are extracted to simplify and improve the 
code, not to avoid a goto.  If you work with small functions from the 
beginning, you are far less likely to end up with a function the uses goto.

-- 
Ian Collins

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


Thread

Is this poor style? "S.K.R. de Jong" <SKRdJ@nowhere.net> - 2015-01-04 00:46 +0000
  Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-03 16:58 -0800
    Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-04 20:37 +1300
      Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-04 02:32 -0800
        Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-05 09:39 +1300
          Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-04 22:53 +0000
  Re: Is this poor style? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-04 01:29 +0000
  Re: Is this poor style? Morris Dovey <mrdovey@iedu.com> - 2015-01-04 00:00 -0600
    Re: Is this poor style? Morris Dovey <mrdovey@iedu.com> - 2015-01-04 02:20 -0600
  Re: Is this poor style? August Karlstrom <fusionfile@gmail.com> - 2015-01-04 15:46 +0100
    Re: Is this poor style? Ike Naar <ike@iceland.freeshell.org> - 2015-01-04 15:10 +0000
      Re: Is this poor style? August Karlstrom <fusionfile@gmail.com> - 2015-01-04 17:06 +0100
  Re: Is this poor style? Clark Smith <noaddress@nowhere.net> - 2015-01-04 15:30 +0000
    Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-04 07:44 -0800
      Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 00:35 +0000
        Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-05 01:02 +0000
          Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 01:43 +0000
            Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-05 11:35 +0000
            Re: Is this poor style? raltbos@xs4all.nl (Richard Bos) - 2015-01-05 12:58 +0000
              Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 13:14 +0000
          Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-05 22:02 +0000
            Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-05 14:24 -0800
              Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-06 12:26 +1300
                Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-06 02:40 -0800
              Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-07 19:32 +0000
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 20:03 +0000
                Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-07 20:27 +0000
                Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 09:55 +1300
                Re: Is this poor style? Les Cargill <lcargill99@comcast.com> - 2015-01-07 18:13 -0600
                Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 13:17 +1300
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 21:02 +0000
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 11:22 +0100
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 11:33 +0000
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 13:08 +0100
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 12:54 +0000
                Re: Is this poor style? David Brown <david.brown@hesbynett.no> - 2015-01-08 14:04 +0100
                Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-01-07 13:57 -0800
                Re: Is this poor style? Les Cargill <lcargill99@comcast.com> - 2015-01-07 18:12 -0600
                Re: Is this poor style? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-07 20:56 +0000
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-07 21:18 +0000
                Re: Is this poor style? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-07 21:28 +0000
            Re: Is this poor style? BartC <bc@freeuk.com> - 2015-01-06 11:28 +0000
              Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-07 20:19 +0000
                Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-07 16:05 -0800
                Re: Is this poor style? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-08 08:29 +0000
                Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-12 22:08 +0000
                Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 20:35 -0700
                Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 08:43 -0700
                Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:38 -0700
                Re: Is this poor style? Stephen Sprunk <stephen@sprunk.org> - 2015-03-26 15:43 -0500
                Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 08:57 -0700
                Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 13:23 +1300
                Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-07 16:53 -0800
                Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 14:11 +1300
              Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-08 11:10 +1300
        Re: Is this poor style? John Bode <jfbode1029@gmail.com> - 2015-01-05 15:54 -0800
          Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-06 11:03 +0100
            Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-06 11:10 +0100
            Re: Is this poor style? John Bode <jfbode1029@gmail.com> - 2015-01-06 06:03 -0800
              Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-06 14:44 +0000
                Re: Is this poor style? "Osmium" <r124c4u102@comcast.net> - 2015-01-06 09:10 -0600
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 11:14 +0100
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 11:31 +0000
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 13:06 +0100
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 13:19 +0100
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-08 12:53 +0000
                Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-08 05:49 -0800
                Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-01-08 08:18 -0800
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-09 07:58 +0100
                Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 09:28 -0700
                Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-03-24 13:57 -0700
                Re: Is this poor style? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 08:28 -0700
                Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-03-25 12:17 -0700
                Re: Is this poor style? Öö Tiib <ootiib@hot.ee> - 2015-04-05 17:03 -0700
                Re: Is this poor style? John Bode <jfbode1029@gmail.com> - 2015-01-14 11:25 -0800
                Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-14 19:41 +0000
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-15 09:01 +0100
                Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-15 09:35 +0100
    Re: Is this poor style? James Kuyper <jameskuyper@verizon.net> - 2015-01-04 11:15 -0500
      Re: Is this poor style? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-04 21:38 +0000
      Re: Is this poor style? Dr Nick <nospam-4@temporary-address.org.uk> - 2015-01-05 07:27 +0000
        Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-05 06:33 -0800
    Re: Is this poor style? Vincenzo Mercuri <nomail@yahoo.it> - 2015-01-04 18:56 +0100
    Re: Is this poor style? rouben@shady.(none) (Rouben Rostamian) - 2015-01-04 18:22 +0000
    Re: Is this poor style? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-04 21:35 +0000
    Re: Is this poor style? Ian Collins <ian-news@hotmail.com> - 2015-01-05 10:46 +1300
      Re: Is this poor style? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-04 15:36 -0800
    Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 12:05 -0800
      Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 14:18 -0800
        Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 14:32 -0800
          Re: Is this poor style? asetofsymbols@gmail.com - 2015-01-05 14:37 -0800
        Re: Is this poor style? James Kuyper <jameskuyper@verizon.net> - 2015-01-05 18:23 -0500
          Re: Is this poor style? Richard Heathfield <invalid@see.sig.invalid> - 2015-01-05 23:32 +0000
      Re: Is this poor style? Keith Thompson <kst-u@mib.org> - 2015-01-05 15:31 -0800
    Re: Is this poor style? Phil Carmody <pc+usenet@asdf.org> - 2015-01-13 15:00 +0200
  Re: Is this poor style? Les Cargill <lcargill99@comcast.com> - 2015-01-04 15:07 -0600
  Re: Is this poor style? jadill33@gmail.com - 2015-01-05 15:03 -0800
  Re: Is this poor style? buja <buja66@gmail.com> - 2015-01-06 21:48 +0100
    Re: Is this poor style? Rosario193 <Rosario@invalid.invalid> - 2015-01-08 11:21 +0100
      Re: Is this poor style? buja <buja66@gmail.com> - 2015-01-08 12:42 +0100
  Re: Is this poor style? "S.K.R. de Jong" <SKRdJ@nowhere.net> - 2015-01-08 15:29 +0000
    Re: Is this poor style? James Kuyper <jameskuyper@verizon.net> - 2015-01-08 12:03 -0500

csiph-web