Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11765
| Date | 2012-02-06 21:24 -0500 |
|---|---|
| From | Arne Vajhøj <arne@vajhoej.dk> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Blocks for scope control |
| References | <dBCQq.122$d%2.16@newsfe07.iad> <block-20120115171817@ram.dialup.fu-berlin.de> <omitable-20120120163400@ram.dialup.fu-berlin.de> <blocks-20120121032802@ram.dialup.fu-berlin.de> |
| Message-ID | <4f308b43$0$291$14726298@news.sunsite.dk> (permalink) |
| Organization | SunSITE.dk - Supporting Open source |
On 1/20/2012 9:32 PM, Stefan Ram wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> alpha();
>> { /* add a button to the frame */
>> final Button button = new Button();
>> frame.add( button ); }
>> beta();
>
> I forgot to mention that there is an old software-engineering
> principle: »The scope of every identifier should be as small
> as possible.« Blocks help to realize this.
>
> Or, let's look at code posted into some other thread recently:
>
> student<freenow12345@gmail.com> writes:
>> GOval g = makeCircle(centerX, centerY,radiusOuterCircle ,
>> Color.RED);
>> add(g);
>> g = makeCircle(centerX, centerY,radiusMiddleCircle ,
>> Color.WHITE);
>> add(g);
>> g = makeCircle(centerX, centerY,radiusInnerCircle ,
>> Color.RED);
>> add(g);
>
> With blocks, »g« can be made final:
>
> { final GOval g = makeCircle( centerX, centerY, radiusOuterCircle, Color.RED ); add( g ); }
> { final GOval g = makeCircle( centerX, centerY, radiusMiddleCircle, Color.WHITE ); add( g ); }
> { final GOval g = makeCircle( centerX, centerY, radiusInnerCircle, Color.RED ); add( g ); }
>
> this also is more beautiful, because now there is more
> symmetry between the the three actions, which then allows a
> possible refactor to:
>
> { final CircleMaker circleMaker = new CircleMaker( g, centerX, centerY );
> circleMaker.make( radiusOuterCircle, Color.RED );
> circleMaker.make( radiusMiddleCircle, Color.WHITE );
> circleMaker.make( radiusInnerCircle, Color.RED ); }
>
> (this will probably not solve the actual problem of »student«).
>
> Or, to tell the story from its end:
>
> The most important principle is »DRY« - »Don't repeat
> yourself!«. To do this, we abstract repetitions of the same
> principle into a single code location using abstractions.
> But to do this, in turn, we need to be able to see
> repetitions of the same principle. And to be able to see
> them, the code needs to be made as symmetric as possible,
> that is, to do the same thing, the same wording should be
> used.
>
> Or in a single line:
> First, make the repetition obvious, then abstract it.
The rule is OK.
But I consider it a 75% rule. Follow it in 75% of cases
and skip it when it does not make sense.
In this case I think the code became less readable, so I would
not do it.
And I would certainly not do that refactoring as it obfuscates
what the code is doing by hiding the adding to g.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-15 11:50 -0400
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-15 17:10 +0100
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-15 17:32 +0100
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 11:21 -0800
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-16 07:59 +0100
Re: Blocks for scope control markspace <-@.> - 2012-01-15 09:57 -0800
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 11:28 -0800
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 11:00 -0800
Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-15 17:49 -0400
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 15:45 -0800
Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-19 21:54 -0400
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-20 08:41 +0100
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-20 15:24 -0800
Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-20 19:30 -0400
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-20 15:54 -0800
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-16 07:55 +0100
Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-19 21:16 -0400
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-21 13:25 +0100
Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-21 10:41 -0400
Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-22 13:03 +0100
Re: Blocks for scope control Roedy Green <see_website@mindprod.com.invalid> - 2012-01-16 06:09 -0800
Re: Blocks for scope control Henk van Voorthuijsen <voorth@xs4all.nl> - 2012-01-16 07:03 -0800
Re: Blocks for scope control Arne Vajhøj <arne@vajhoej.dk> - 2012-01-17 18:43 -0500
Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-20 11:05 -0800
Re: Blocks for scope control Gene Wirchenko <genew@ocis.net> - 2012-01-20 12:11 -0800
Re: Blocks for scope control Arne Vajhøj <arne@vajhoej.dk> - 2012-02-06 21:24 -0500
csiph-web