Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

goto, not much worse than break/continue

Name: Anonymous 2005-02-21 17:56

Discuss.

Name: Anonymous 2005-02-21 19:14

Print

Name: Anonymous 2005-02-21 23:33 (sage)

Name: Anonymous 2005-02-22 2:47

>>3

You fail at getting the point.

Name: Anonymous 2005-02-22 2:48

for ( blah; blah; blah ) {
    blah;
    if ( ! error ) {
        blah;
        if ( ! error ) {
            blah;
            if ( error )
            else if ( done )
                break;
            else
                continue;
        }
    }
    fprintf( stderr, "zomg errors!" )
    break;
}

Name: Anonymous 2005-02-22 14:17

>>5
Poor.

for ( blah; blah && done; blah ) {
    blah;
    if (error) {
        zomg = true;
        break;
    }
    blah;
    if (error) {
        zomg = true;
        break;
    }
    blah;
    if (error) {
        zomg = true;
        break;
    }
}

if (zomg) {
    fprintf( stderr, "zomg errors!" )
}

Name: deitarion 2005-02-23 5:57

I'm 50/50 on this one. Break and Continue are rather ugly compared to some of the more elegant syntactic constructs in use but they are still useful and at least they're less vague than an equivalent goto.

Name: Christy McJesus !DcbLlAZi7U 2005-02-23 10:12

break/continue does indeed provide the user with ways to bring back the evils of goto, as do exceptions. Technically it's always possible to structure your loop so you don't need a break, but in practice it's not worth the extra wrangling just for the sake of some elegance.

One would have to make a determined effort to use breaks, continues, exceptions etc to create bad code.

Name: deitarion 2005-02-23 20:15

I beg to differ. I find it a fun exercise to avoid break/continue. In fact, I've only ever used break once and that was in this Python loop.

for name, position in queuedFiles:
    if checkCriteria(name):
        print name, "queued at position", position
        break

In fact, I later made it into a function called findNextWhatever() and replaced the break with a return so I guess that doesn't really count.

Name: Anonymous 2005-02-23 21:54

>>8

Actually, I find breaks more elegant than if(){if(){if(){ and 500 chars wide code. I think break (common), continue (rarely necessary), and goto (extreme situations like several nexted loops) are all acceptable, as long as you Don't Suck™, i.e. everything good if not misused.

Name: Anonymous 2005-02-27 8:23

>>9

Avoiding a simple break makes code harder and more annoying to write and read, IMHO

Name: Anonymous 2005-02-27 12:32

Breaks in the context of switch statements can sometimes be fugly, but switch statements can execute faster than long lines of if-else if-else for simple values, since the variable is only evaluated once, rather than at each if or else if statement.   As >>10 said, if you are good and know what you are doing, then using break/continue isn't that bad.

Name: Anonymous 2005-02-27 14:05

With goto its possible to have to flip back and forth through multiple pages of code find where you are supposed to wind up.  That only happes with break/continue if your loops contain multiple pages of code which is bad practice anyway.  Also, goto gives you no restrictions on where the program flows, with break/continue you always know exactly where you should go next.

Name: Anonymous 2005-03-01 0:46

>>12
Suitably smart compilers (TM) nowadays handle chained and sequential ifs which test the same term and optimize it to the head of the chain or sequence.

Name: Anonymous 2005-03-01 10:22

>>14

It's not about execution speed, it's about reading and writing programs speed (business terms: development and maintenance)

Name: Anonymous 2005-03-11 6:57

>>16
However, unless they can actually convert it into a switch-like structure, they can't use jump tables to speed up execution.  Generally, ten conditional branches are more expensive than one branch that takes a value from a memory address.

Name: Anonymous 2005-03-13 20:13

I wonder if compilers fully investigate the term first to see if repeatedly evaluating it would change its value.

Name: JoeOsborn 2005-03-13 20:38

If you ever need to use break or continue or goto rather than a simple return, your functions/methods are too big and you ned to refactor.  You lose the meaning of the code in the irksome details of the control structures.

Name: Anonymous 2005-03-14 2:23

>>18
refactor into clunky functions as opposed to a few breaks?

Name: Christy McJesus !DcbLlAZi7U 2005-03-14 5:44

>>19
If your functions are clunky you're using the wrong language.

Name: vans 2005-03-14 7:30

>>19 , >>20
I suggest Io.  http://www.iolanguage.com
Liberate yourself from clunkitude.
"Methods" sounds cooler than "functions" anyway...

Name: Anonymous 2005-03-14 9:09

From the Io FAQ:

-----

How production stable is it?

It hasn't been terribly well tested and isn't being used in any production environments that I know of. Please help us by testing things out!

-----

So no, try again in a few years when your language has matured a bit.

Name: JoeOsborn 2005-03-14 9:55

>>22
Try using the language a bit?  Maybe you'll like it.  The language isn't its implementation, and the guts are a mere 10 kloc of C code.

The FAQ is also a bit out of date -- all the built-in objects now have automated unit tests run on them, and the language is several years old by now.

That said, the language does constantly improve and become more consistent and therefore better.  I present it as an example of a good language that can illustrate some excellent points wrt simplicity being fundamentally equal to power.

How many languages have you seen that can express exception handling try/catch style exclusively in the language itself?  No VM support for it at all.  And it can do it in 14 lines.

For languages that are both pretty-good and production stable, I'd assert Smalltalk and Objective-C.  There's no reason anyone should have to do high-level programming in C++ or Java.

Name: JoeOsborn 2005-03-14 10:01

>>22
Whoops, quote a bit further in the faq:
----
I generally release fixes to any bugs found in a matter of days. The up side of Io being young language (born in April 2002) is that your help and input can have a significant effect on it's future.
----
I'm fairly active on the Io mailing list, and it's true that bug fixes come out very soon after bugs are reported.

Name: Christy McJesus !DcbLlAZi7U 2005-03-14 14:45

>>23

Automated unit tests eh? D has those. They seem like a neat idea. Better than those annoying toy main functions I always find myself writing to test Java classes.


Ahhhhhh I can't wait until the end of my studies when I can stop using Java... at least until I get a job. "... require 3 years commercial experience in Java, VB, VB.NET, Windows NT..."

Anyone else feel depressed when they see all the pages and pages of job adverts that look like that?

Name: JoeOsborn 2005-03-14 17:22

>>25
I do, Christy, if it's any consolation.  Higher level languages are still a few years off for business purposes... the inertia of static typing and the obsession with the waterfall model of software development are really quite strong.  UML is evidence of that.
As for D -- I like it if only because some really awesome shmups have been implemented in it by Kenta Cho.  rRootage, Noiz2sa, Torus Trooper, &c.

Name: Christy McJesus !DcbLlAZi7U 2005-03-15 6:18

rrootage is in D? zomg I must emerge it!

Name: Anonymous 2007-06-16 15:11 ID:8Xoo8mDo

"How many languages have you seen that can express exception handling try/catch style exclusively in the language itself?"
hint, it starts with "common"

Name: Anonymous 2007-06-16 15:39 ID:Heaven

>>28
COmmon Business Oriented Language

Name: Anonymous 2007-06-16 17:14 ID:GyEJqUIq

The goto statement is just a tool, but if you insist on avoiding it when it makes everything easier then so are you.

http://www.ecn.purdue.edu/ParaMount/papers/rubin87goto.pdf
- a good argument of when to use goto.

http://www.ecn.purdue.edu/ParaMount/papers/acm_may87.pdf
- the argument continues, and the example gets longer and longer... the guy can't even write the C example correctly, lol.

http://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1009.html
- dickstra blabbers again and "derives" a solution using his gotoless method, and comes up with an even more complex solution. LOL.

/prog/, how would you write the example?

I pose the following problem to a group of EXPERT PROGRAMMERS: “Let X be an N x N matrix of integers. Write a program that will print the number of the first all-zero row of X, if any.”

Name: Anonymous 2007-06-16 19:02 ID:0/5TECUP

>>30
|[ var c : bool; var i : int
 ; c,i := true,0
 ; do c ∧ i ≠ N →
        |[ var d : bool; var j : int
        ; d, j := true,0
        ; do d ∧ j ≠ N → d,j := X[i,j] = 0, j+1 od
        ; c,i := ¬d, i+1
        ]|
    od
 ; if c → skip ⌷ ¬c → print (i–1) fi
]|

Name: Anonymous 2007-06-16 19:23 ID:YRoeEt7l

private int zeroRow(int[][] x) {
  for(int i=0; i<x.length; i++)
    if(allZero(x[i])) return i;
  return -1;
}

private boolean allZero(int[] a) {
  for(int i: a) if(i != 0) return false;
  return true;
}

//elsewhere, x contains the matrix
int i = zeroRow(x);
if(i >= 0) System.out.println(i);


The reason gotos are bad is they make you look for the target; return/break/etc on the other hand jump to an easily predictable location (raise doesn't really, but the way it's meant to be used you don't need to know where control flow continues).

Name: Anonymous 2007-06-17 4:10 ID:gC0xetKQ

>>22
..is a fucking idiot and a namefag

Name: Anonymous 2007-06-17 4:16 ID:Heaven

[m][code]
CL-USER> (defparameter mat '((0 0 3 3)
                 (0 2 3 0)
                 (0 0 0 0)
                 (0 0 3 8)))
MAT
CL-USER> (position t (mapcar (lambda (row) (every (lambda (item) (= item 0)) row)) mat))
2
[/m][/code]

Name: Anonymous 2007-06-17 4:17 ID:gC0xetKQ

>>34

CL-USER> (defparameter mat '((0 0 3 3)
                 (0 2 3 0)
                 (0 0 0 0)
                 (0 0 3 8)))
MAT
CL-USER> (position t (mapcar (lambda (row) (every (lambda (item) (= item 0)) row)) mat))
2

idiot.

Name: Anonymous 2007-07-05 6:57 ID:u8cyqa4P

wtf

Name: Anonymous 2009-01-22 6:47

________♥
_______♥♥♥
______♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
_♥♥♥♥♥_____♥♥♥♥♥♥
__♥♥♥♥_____♥♥♥♥♥
_♥♥♥♥♥______♥♥♥♥♥
♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥
______♥♥♥♥♥
_______♥♥♥
________♥

Name: Anonymous 2009-03-06 17:38

Test

Name: Anonymous 2009-03-06 17:40

>>38
What, pray tell, were you testing?

Name: Anonymous 2009-03-06 19:29

>>39
Your mother, for STDs

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List