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


Groups > comp.lang.java.programmer > #29642 > unrolled thread

local constants with switch

Started byAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
First post2014-08-12 16:56 +0000
Last post2014-08-16 09:17 +0000
Articles 10 on this page of 30 — 12 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-12 16:56 +0000
    Re: local constants with switch Donkey Hottie <donkey@fredriksson.dy.fi> - 2014-08-12 20:30 +0300
    Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-12 21:15 +0000
      Re: local constants with switch Silvio <silvio@internet.com> - 2014-08-12 23:20 +0200
      Re: local constants with switch Silvio <silvio@internet.com> - 2014-08-12 23:22 +0200
    Re: local constants with switch Lew <lewbloch@gmail.com> - 2014-08-15 15:22 -0700
      Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-16 00:24 +0000
        Re: local constants with switch Roedy Green <see_website@mindprod.com.invalid> - 2014-08-18 23:46 -0700
          Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-21 13:57 +0000
            Re: local constants with switch Joerg Meier <joergmmeier@arcor.de> - 2014-08-21 18:52 +0200
              Re: local constants with switch Mike Amling <mamling@chaff.us> - 2014-08-21 13:19 -0500
                Re: local constants with switch Joerg Meier <joergmmeier@arcor.de> - 2014-08-21 22:41 +0200
              [OT] Re: local constants with switch Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-21 14:42 -0400
                Re: [OT] Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-21 19:21 +0000
                  Re: [OT] Re: local constants with switch Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-08-21 22:30 -0500
                    Re: [OT] Re: local constants with switch Silvio <silvio@internet.com> - 2014-08-23 14:01 +0200
                      Re: [OT] Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-23 15:22 +0000
                Re: [OT] Re: local constants with switch Arne VajhĂžj <arne@vajhoej.dk> - 2014-08-23 22:12 -0400
            Re: local constants with switch Jim Gibson <jimsgibson@gmail.com> - 2014-08-21 17:04 -0700
              [OT] Re: local constants with switch Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-08-21 20:50 -0400
                Re: [OT] Re: local constants with switch Martin Gregorie <martin@address-in-sig.invalid> - 2014-08-22 11:48 +0000
    Re: local constants with switch Joerg Meier <joergmmeier@arcor.de> - 2014-08-16 01:18 +0200
      Re: local constants with switch Arne VajhĂžj <arne@vajhoej.dk> - 2014-08-15 22:44 -0400
        Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-16 09:01 +0000
          Re: local constants with switch Joerg Meier <joergmmeier@arcor.de> - 2014-08-16 12:08 +0200
          Re: local constants with switch Arne VajhĂžj <arne@vajhoej.dk> - 2014-08-16 20:39 -0400
            Re: local constants with switch Joerg Meier <joergmmeier@arcor.de> - 2014-08-17 13:08 +0200
        Re: local constants with switch Joerg Meier <joergmmeier@arcor.de> - 2014-08-16 12:07 +0200
    Re: local constants with switch Arne VajhĂžj <arne@vajhoej.dk> - 2014-08-15 22:42 -0400
      Re: local constants with switch Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-08-16 09:17 +0000

Page 2 of 2 — ← Prev page 1 [2]


#29800 — Re: [OT] Re: local constants with switch

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2014-08-22 11:48 +0000
SubjectRe: [OT] Re: local constants with switch
Message-ID<lt7amc$gsn$1@dont-email.me>
In reply to#29797
On Thu, 21 Aug 2014 20:50:12 -0400, Eric Sosman wrote:

> On 8/21/2014 8:04 PM, Jim Gibson wrote:
>> In article <slrnlvbume.fp3.avl@login.logic.tuwien.ac.at>, Andreas
>> Leitgeb <avl@auth.logic.tuwien.ac.at> wrote:
>>
>>
>>> PS: I agree, that "constant variable" is an oxymoron.
>>>    A better term might have been "(constant) vari-un-able" ;-)
>>>
>>>
>> What about "const-able"?
> 
>      "Constable, how would you like to join the River Police?"
> 
>       "Oid loik thet very much, Sir!"
> 
>       "Huhhh-URK!"
> 
>       FX: "Splash"
> 
>       "Thenk yew verry much, Sir!"
> 
>       -- "Dishonoured" (The Goons)

And there's plenty more where THAT came from. 

http://www.thegoonshow.net/


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [next] | [standalone]


#29680

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-08-16 01:18 +0200
Message-ID<myenuyioa1ym$.39yw0wm1njk2.dlg@40tude.net>
In reply to#29642
On Tue, 12 Aug 2014 16:56:18 +0000 (UTC), Andreas Leitgeb wrote:

> It never before occurred to me, that one could define a local constant
> within a method (e.g.: final int foo = 42;) AND use it for a case label
> in a switch statement.

> I'm a bit curious about whether this is an old-age feature that I've
> been missing out for a very long time, or if this just came in recently...

I remember a very early piece of code I was trying to write, Java had
enums, where I stumbled across this restriction, which sadly made the code
I intended to write like this impossible:

class Bla {

private static final int i = 0;
private static final int LEFT = i++;
private static final int RIGHT = i++;
private static final int UP = i++;
private static final int DOWN = i++;

private void whatever() {
    doStuff(RIGHT);
}

private void doStuff(int direction) {
    switch (direction) {
        case LEFT: ...

As I had a lot of int constants, I was worried about keeping track of what
numbers were in use, and not interested in the specific number, nor
serialization, so that would have been a nice pattern, had it not been for
that restriction, that I never understood (and still don't, why can't javac
just compile it to a series of if statements instead of whining ?).

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#29684

FromArne VajhĂžj <arne@vajhoej.dk>
Date2014-08-15 22:44 -0400
Message-ID<53eec579$0$302$14726298@news.sunsite.dk>
In reply to#29680
On 8/15/2014 7:18 PM, Joerg Meier wrote:
> On Tue, 12 Aug 2014 16:56:18 +0000 (UTC), Andreas Leitgeb wrote:
>> It never before occurred to me, that one could define a local constant
>> within a method (e.g.: final int foo = 42;) AND use it for a case label
>> in a switch statement.
>
>> I'm a bit curious about whether this is an old-age feature that I've
>> been missing out for a very long time, or if this just came in recently...
>
> I remember a very early piece of code I was trying to write,

> private static final int i = 0;
> private static final int LEFT = i++;
> private static final int RIGHT = i++;
> private static final int UP = i++;
> private static final int DOWN = i++;

> As I had a lot of int constants, I was worried about keeping track of what
> numbers were in use,

If all the constants are the same place and in numeric order, then
it should not be that hard.

Arne

[toc] | [prev] | [next] | [standalone]


#29688

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2014-08-16 09:01 +0000
Message-ID<slrnluu7ft.fp3.avl@login.logic.tuwien.ac.at>
In reply to#29684
Arne VajhĂžj <arne@vajhoej.dk> wrote:
> On 8/15/2014 7:18 PM, Joerg Meier wrote:
>> I remember a very early piece of code I was trying to write,
>> private static final int i = 0;
>> private static final int LEFT = i++;
>> private static final int RIGHT = i++;
>> private static final int UP = i++;
>> private static final int DOWN = i++;
>> As I had a lot of int constants, I was worried about keeping track of what
>> numbers were in use,
>
> If all the constants are the same place and in numeric order, then
> it should not be that hard.

Jörg's problem is about that code being under work,
and new constants being added *not* only at the end.

I guess, enums might actually help.
(The enum itself can be privately nested in current class)

[toc] | [prev] | [next] | [standalone]


#29691

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-08-16 12:08 +0200
Message-ID<1njdafpg1dvjm.1y2aq5m0fylhx.dlg@40tude.net>
In reply to#29688
On Sat, 16 Aug 2014 09:01:49 +0000 (UTC), Andreas Leitgeb wrote:

> I guess, enums might actually help.
> (The enum itself can be privately nested in current class)

Yes, enums would have been the perfect solution. But that was back in 1.4.2
times (I think) and enums weren't around yet.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#29702

FromArne VajhĂžj <arne@vajhoej.dk>
Date2014-08-16 20:39 -0400
Message-ID<53eff9c4$0$294$14726298@news.sunsite.dk>
In reply to#29688
On 8/16/2014 5:01 AM, Andreas Leitgeb wrote:
> Arne VajhĂžj <arne@vajhoej.dk> wrote:
>> On 8/15/2014 7:18 PM, Joerg Meier wrote:
>>> I remember a very early piece of code I was trying to write,
>>> private static final int i = 0;
>>> private static final int LEFT = i++;
>>> private static final int RIGHT = i++;
>>> private static final int UP = i++;
>>> private static final int DOWN = i++;
>>> As I had a lot of int constants, I was worried about keeping track of what
>>> numbers were in use,
>>
>> If all the constants are the same place and in numeric order, then
>> it should not be that hard.
>
> Jörg's problem is about that code being under work,
> and new constants being added *not* only at the end.

So you want to add new constant in the middle and push the higher
one up in numeric value?

 > I guess, enums might actually help.

Yes.

Arne

[toc] | [prev] | [next] | [standalone]


#29704

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-08-17 13:08 +0200
Message-ID<103t0htju0e73$.1m66z8c74gbjv.dlg@40tude.net>
In reply to#29702
On Sat, 16 Aug 2014 20:39:26 -0400, Arne VajhĂžj wrote:

> On 8/16/2014 5:01 AM, Andreas Leitgeb wrote:
>> Arne VajhĂžj <arne@vajhoej.dk> wrote:
>>> On 8/15/2014 7:18 PM, Joerg Meier wrote:
>>>> I remember a very early piece of code I was trying to write,
>>>> private static final int i = 0;
>>>> private static final int LEFT = i++;
>>>> private static final int RIGHT = i++;
>>>> private static final int UP = i++;
>>>> private static final int DOWN = i++;
>>>> As I had a lot of int constants, I was worried about keeping track of what
>>>> numbers were in use,
>>> If all the constants are the same place and in numeric order, then
>>> it should not be that hard.
>> Jörg's problem is about that code being under work,
>> and new constants being added *not* only at the end.
> So you want to add new constant in the middle and push the higher
> one up in numeric value?

We just wanted to add new constants in the middle and not worry about the
numeric value, as long as they were unique (so we could use them in the
switch statements). Hence the counter.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#29690

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-08-16 12:07 +0200
Message-ID<dv7zvlzithd0$.wxog620tm0mg$.dlg@40tude.net>
In reply to#29684
On Fri, 15 Aug 2014 22:44:06 -0400, Arne VajhĂžj wrote:

> On 8/15/2014 7:18 PM, Joerg Meier wrote:
>> On Tue, 12 Aug 2014 16:56:18 +0000 (UTC), Andreas Leitgeb wrote:
>>> It never before occurred to me, that one could define a local constant
>>> within a method (e.g.: final int foo = 42;) AND use it for a case label
>>> in a switch statement.

>>> I'm a bit curious about whether this is an old-age feature that I've
>>> been missing out for a very long time, or if this just came in recently...
>> I remember a very early piece of code I was trying to write,

>> private static final int i = 0;
>> private static final int LEFT = i++;
>> private static final int RIGHT = i++;
>> private static final int UP = i++;
>> private static final int DOWN = i++;

>> As I had a lot of int constants, I was worried about keeping track of what
>> numbers were in use,
> If all the constants are the same place and in numeric order, then
> it should not be that hard.

The list was sorted alphabetically and under development, so it got a bit
muddy after a while.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#29683

FromArne VajhĂžj <arne@vajhoej.dk>
Date2014-08-15 22:42 -0400
Message-ID<53eec51f$0$302$14726298@news.sunsite.dk>
In reply to#29642
On 8/12/2014 12:56 PM, Andreas Leitgeb wrote:
> It never before occurred to me, that one could define a local constant
> within a method (e.g.: final int foo = 42;) AND use it for a case label
> in a switch statement.
>
> I'm a bit curious about whether this is an old-age feature that I've
> been missing out for a very long time, or if this just came in recently...

A different perspective here.

I am somewhat skeptical about the usefulness of this feature.

A single method that defines local constants and use them as
case labels is very likely a code smell.

If the variable being switches on is external, then the constants
should be non-local and used both inside and outside.

If it is internal and first there are some logic to assign one
the constants to the variable and then later in the code comes
the switch, then I think there is a good chance that the method
is too long and does too much.

Arne


[toc] | [prev] | [next] | [standalone]


#29689

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2014-08-16 09:17 +0000
Message-ID<slrnluu8ck.fp3.avl@login.logic.tuwien.ac.at>
In reply to#29683
Arne VajhĂžj <arne@vajhoej.dk> wrote:
> I am somewhat skeptical about the usefulness of this feature.
>
> A single method that defines local constants and use them as
> case labels is very likely a code smell.
>
> If the variable being switches on is external, then the constants
> should be non-local and used both inside and outside.
>
> If it is internal and first there are some logic to assign one
> the constants to the variable and then later in the code comes
> the switch, then I think there is a good chance that the method
> is too long and does too much.

Unfortunately, I have to agree...

The involved code indeed smells badly, and afterall I apparently
didn't really miss all that much, before stumbling over it.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.java.programmer


csiph-web