Why not introduce a "but" condition?
Hi,
why not introduce a "but" condition into the Java language?
Here's an example:
if(earth.isUnderAttack()){
牋爂o to 10
}but if(earth.getAttack().getAttacker().equals("alien") && earth.getStatus().equals("nice place")){
牋爂o to 20
}else{
牋爂o to 30
}
Doesn't it look nicer, rather then doing it like this?
if(earth.isUnderAttack()){
牋爄f(earth.getAttack().getAttacker().equals("alien") && earth.getStatus().equals("nice place")){
牋牋牋爂o to 20
牋爙else{
牋牋牋爂o to 10
牋爙
}else{
牋爂o to 30
}
Cheers,
Alexander H鰃lind
alexander.hoglind@solberg.se
Looks nice. It's the sort of thing Perl is fond of. but frankly, we're having a hard enough time getting blatantly useful features like foreach into Java.
> Hi,> why not introduce a "but" condition into the Java> language?> > Here's an example:what about 'else if'
> Looks nice. It's the sort of thing Perl is fond of.> but frankly, we're having a hard enough time getting> blatantly useful features like foreach into> Java.What should foreach do?Monika.
> What should foreach do?I've found the answer in the Generics forum.Monika.
> > What should foreach do?
>
> I've found the answer in the Generics forum.
>
> Monika.
And what do you think of the idea of introducing foreach?
Personally I think it is an essential improvement to the Java language for JDK 1.5 (alongside Generics).
So, with 1.4 you would write:
List colorList = new ArrayList();
nameList.add( "red" );
nameList.add( "green" );
nameList.add( "blue" );
for ( Iterator iter = colorList.iterator(); iter.hasNext(); )
{
String color = (String)iter.next();
System.out.println( "Colour = " + color );
}
With Generics, this would become typesafe:
List<String> colorList = new ArrayList<String>();
nameList.add( "red" );
nameList.add( "green" );
nameList.add( "blue" );
for ( Iterator<String> iter = colorList.iterator(); iter.hasNext(); )
{
String color = iter.next();
System.out.println( "Colour = " + color );
}
and with Generics and foreach, it would become typesafe, compact, easier to understand, and easier to maintain:
List<String> colorList = new ArrayList<String>();
nameList.add( "red" );
nameList.add( "green" );
nameList.add( "blue" );
foreach ( color in colorList )
{
System.out.println( "Colour = " + color );
}
Geoff
Well, the "else if" condition only excecutes if the first "if" condition is false. The "but if" condition will only excecute the first "if" condition if the "but if" condition is false... do you get it?
I think this is the way we speek normaly:
If earth is under attack then we would protect us the normal way. But if the attacker is an alien and earth is a nice place to defend, then we would attack them with our anti-alien force. Else If the earth isn磘 under attack then take a bud and watch the game.
rather then saying like this:
if earth is under attack, then, if the attacker is and alien and earth is a nice place to defend then we would use our anti-alien force, else then we would protect us the normal way.
Else If the earth isn磘 under attack then take a bud and watch the game.
Alexander H鰃lind
alexander.hoglind@solberg.se
if this: do A
but if that: do B
is surely the same as
if that: do B
else if this: do A
Be careful about implying that the language that you code in should reflect the structure of the language that you speak, (particularly with a Swedish address). If you were to listen to the speech pattern of the South London geezers that I'm surrounded by you'd end up with a well dodgy language syntax, (whilst if you reflected the nuances of RP you'd find that you had a simply splendid means of expressing abstract thoughts).
ok... I'll try to explain it on more time...
If the first "if" condition is true AND the "but if" condition is true then excecute "but if". But if the first "if" condition is true and the "but if" condition is false then excecute the first "if".
If the first "if" condition is false, then we don磘 have to test the "but if" condition.
At the momement the only way to do that is like this:
if (isTrue){
if(isTrue)
doCommand(2)
else
doComand(1)
}
My idea is this:
if(isTrue)
doCommand(1)
but if(isTrue)
doCommand(2)
Remember, this is only an idea I got when I was programming and the code was hard to read.
shrink_laureate said something about Perl having it implemented in some kind of way. If they have, then for sure there磗 a reason, don磘 you think....?
And you should also be careful to imply that there is some universal idea of how programming code should look like. And for whatever it磗 worth saying it... don磘 you think that Java or C etc. is reflecting the structure of the language that you speak more than assembly does?
Alexander H鰃lind
alexander.hoglind@solberg.se
I can actually see your point but for some reason it's just not a comfortable construct for me. I think that it's because you're breaking up the sequence of execution, forcing the thread to look ahead to a following statement, (which may not even be there), before determining if the current conditional block should be entered. I'm not saying that this is necessarily a reason to avoid the construct but I'm unconvinced that it'll prove intuitive enough to do anything but cause maintenance problems. I don't know Perl but I'd be interested to hear if it causes problems there. Anyway, isn't this simply a clear place for decomposition? Take the clunky conditional code and stick it somewhere else. Back to work...
if (isTrue){
if(isTrue){
doCommand(2);
}
else{
doComand(1);
}
}
My idea is this:
if(isTrue){
doCommand(1);
}
but if(isTrue){
doCommand(2);
}
Yes, the code I wrote was a little bit clumsy... But whatever. Good that you finally got my point anyway! And yes, I don磘 know if the "but if" condition is a good idea always... It may depend on what kind of programming you磖e doing. And yes, it could actually be harder to read than the way we磖e doing it now...
The reason for me asking for a "but if" condition is that with the "but if" condition, if you磖e using correctly, you磍l have the commands you want to do in the right order. It may make it easier to read...
I'm more interested if the Perl implementation (if it exists) doesn磘 causes problemns there.... ;)
ok.. back to holiday! Cheers!
Can you direct me to some website that show some examples of Perl or whatever language using this kind of condition?
What do you think of it. Could it be useful to have a "but if" condition or would it only confuse? If not now, could it be possible to have it implemented in the future.
Do you know if there is some way for me to help implementing the "but if" condition?
'but if' is a rediculous idea!
I'll tell you why. It means that you have to read *all* of the 'but if' statements before you know what the program is going to do. If you have 15 of them, you have to read all 15 before you know which code is likely to excecute.
With 'else if' you know exactly where you are each time you read the new if statement.
For many people who write extremely bad code, this could mean that the 'but if' could be in a completely different page. (Unortunately, there are such people).
So, if you want to make a language more unreadable, then 'but if' is a good idea. But maybe not for Java.
Rhys
Well... You should only be allowed to use "but if" one time, just as you are only allowed to use "else" one time.
The hard way:
if ( isTest1 ){
if ( isTest3 ){
doCommand(3);
} else{
doCommand(1);
}
} else ( isTest2 ){
if ( isTest3 ){
doCommand(3);
} else {
doCommand(2);
}
} else {
doCommand(4);
}
The "But if" way:
if ( isTest1 ){
doCommand(1);
}else if ( isTest2 ){
doCommand(2);
}but if ( isTest3 ){
doCommand(3);
} else {
doCommand(4);
}
Alex
He doesn't mean that Perl actually has it. But it's the sort of thing that Perl is fond of, and for which people are fond of perl.For example:print "Error!" unless (error == 0);as a shortcut for:if (error != 0) print "Error!";
ok! thanx! that explains... Yes it's verry alike. It would be almost the same if you put it in a condition:if(ERROR) print "error"unless(SKIP)
Isn't 'but if' just a specific case of the preceeding if? In that case isn't it more elegant to start with the generic case and drill down to the specific:
if (earth.isUnderAttack()) {
if (earth.getAttack().isAttackerBlahBlahBlah()) {
// do a specific thing
}
else {
// do the generic thing
}
}
'but if', 'unless', 'provided' all sound backwards, almost like an afterthought while programming to me.
- Berni.
> Well... You should only be allowed to use "but if" one
> time, just as you are only allowed to use "else" one
> time.
>
> The hard way:
>
> if ( isTest1 ){
>if ( isTest3 ){
>doCommand(3);
>} else{
>doCommand(1);
>}
>} else ( isTest2 ){
>if ( isTest3 ){
>doCommand(3);
>} else {
>doCommand(2);
>}
>} else {
>doCommand(4);
>}
>
>
>The "But if" way:
>
>if ( isTest1 ){
>doCommand(1);
>}else if ( isTest2 ){
>doCommand(2);
>}but if ( isTest3 ){
>doCommand(3);
>} else {
>doCommand(4);
>}
>
>
> Alex