Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Zombywuf
Mar 29, 2008

0x1C to 0x1F are clear the best bytes to delimit data with.

Adbot
ADBOT LOVES YOU

npe
Oct 15, 2004
I should have clarified, the file has to be a valid "text" file in single encoding. While you could delimit the data like that, it would be mixed encoding and would piss off text editors.

In other words, the 0xFE bytes are tripping complaints from programs reading them about that byte not being valid utf8, and this is where the wtf complaints come from... "I thought anything could be unicode".

npe fucked around with this message at 13:59 on Jun 14, 2008

Zombywuf
Mar 29, 2008

0x1C to 0x1F are valid ASCII, and valid utf-8 encodings. Whether or not your text editor will like them I don't know.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
From a test I recently corrected.


__m128 y = _mm_mul_ps(x, _mm_set1_ps(1.0f));

This is an SSE y = x*1;

__m128 rcp = _mm_rcp_ps(_mm_set1_ps(1.0f));

rcp = 1/1;


Wait, there's worse

__m128 rcp = rcp_nr(_mm_set1_ps(1.0f));

Hrmm. SSE reciprocal is not very accurate. I know, let's use a newtown rhapson iteration to increase the accuracy of our 1/1 calculation!

ZorbaTHut
May 5, 2005

wake me when the world is saved
I wrote a 3000-line function once. It included a "goto".

No, I'm not going to paste it in here.

Took about five minutes to compile. I think I determined that GCC's optimizer was O(n^2) in the number of lines.

I cleaned it up once it was finished and split it into three or four functions (yes, still behemonths of functions, but they did big complex things and splitting it up further would have just made it less readable.)

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

ZorbaTHut posted:

I wrote a 3000-line function once. It included a "goto".

3000 lines is :psyduck:, but there's nothing wrong with using the occasional goto. "Never ever use goto" is generally promulgated by people who've heard of Dijkstra's article but never read the drat thing.

Factor Mystic
Mar 20, 2006

Baby's First Post-Apocalyptic Fiction

Zakalwe posted:

3000 lines is :psyduck:, but there's nothing wrong with using the occasional goto. "Never ever use goto" is generally promulgated by people who've heard of Dijkstra's article but never read the drat thing.

I'm skeptical that goto has a valid place outside of perhaps some simple scripting. I challenge you to show me a valid example of goto use in modern object oriented code.

tripwire
Nov 19, 2004

        ghost flow

Zakalwe posted:

3000 lines is :psyduck:, but there's nothing wrong with using the occasional goto. "Never ever use goto" is generally promulgated by people who've heard of Dijkstra's article but never read the drat thing.

Gotos have no place in OO (or really any kind of structured) programming. Maybe you'd feel more at home with fortran?

ehnus
Apr 16, 2003

Now you're thinking with portals!

Factor Mystic posted:

I'm skeptical that goto has a valid place outside of perhaps some simple scripting. I challenge you to show me a valid example of goto use in modern object oriented code.

Two that come immediately to mind are breaking out of nested loops in C/C++/C# and cleaning up/freeing resources upon failure in environments that do not provide exceptions. I've used them when I've written parsers too as there was no other clear way of providing similar functionality.

Linus Torvalds addresses the use of "goto" in the Linux kernel with some good points here.

Yes, they can turn code into an unreadable mess but, sometimes, they can also make code more readable and more intuitive.

edit: I'm inclined to think that people who believe that "goto" has no place in modern programming languages have not been programming for long enough.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

ehnus posted:

Two that come immediately to mind are breaking out of nested loops in C/C++/C# and cleaning up/freeing resources upon failure in environments that do not provide exceptions. I've used them when I've written parsers too as there was no other clear way of providing similar functionality.

DING DING DING.

Allie
Jan 17, 2004

Factor Mystic posted:

I'm skeptical that goto has a valid place outside of perhaps some simple scripting. I challenge you to show me a valid example of goto use in modern object oriented code.

They're useful in C for cleanup in the case of errors, and they're usually more readable than the alternatives. Other than that, I don't know that I've ever used them for anything else, and in that case they usually aren't necessary or all that useful in languages with exception handling capabilities.

POKEMAN SAM
Jul 8, 2004

ehnus posted:

Two that come immediately to mind are breaking out of nested loops in C/C++/C#

The one thing I did like about PHP was that they offer the break X construct that lets you break out of nested loops as far as you want.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

Ugg boots posted:

The one thing I did like about PHP was that they offer the break X construct that lets you break out of nested loops as far as you want.

Until you add another loop and WHOOPS, you're not where you think you should be.

tef
May 30, 2004

-> some l-system crap ->

tripwire posted:

Gotos have no place in OO (or really any kind of structured) programming. Maybe you'd feel more at home with fortran?

There is the interesting paper "Structured programming with Gotos" by donald knuth.

http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf

The horror of goto is using it in place of while, for and other looping and structured constructs. However there are occasions where a direct jump is needed and cannot be elegantly done with the existing constructs.

Even so knuth does suggest that goto not be used in high level languages.


Edit:

Perl had the nice thing of goto &function which was occasionally useful, and also allowed you to label loops so you could do break LOOP_NAME

floWenoL
Oct 23, 2002

tripwire posted:

Gotos have no place in OO (or really any kind of structured) programming. Maybe you'd feel more at home with fortran?

Here's a nickel, kid, go buy yourself a C book.

Zakalwe posted:

Until you add another loop and WHOOPS, you're not where you think you should be.

Modern (non-lovely *cough*) languages like Java and Perl provide labeled loops and ways to break out of them, so that removes one of the two remaining use cases for gotos nowadays, and exceptions remove the other one.

floWenoL fucked around with this message at 04:19 on Jun 15, 2008

Zombywuf
Mar 29, 2008

tef posted:

Perl had the nice thing of goto &function which was occasionally useful, and also allowed you to label loops so you could do break LOOP_NAME

Why the past tense?

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

floWenoL posted:

Here's a nickel, kid, go buy yourself a C book.


Modern (non-lovely *cough*) languages like Java and Perl provide labeled loops and ways to break out of them, so that removes one of the two remaining use cases for gotos nowadays, and exceptions remove the other one.


break my_label is no different than goto my_label as far as breaking out of loops is concerned. It just removes the functionality of one "evil" and sticks it in another.

zootm
Aug 8, 2006

We used to be better friends.

Zakalwe posted:

break my_label is no different than goto my_label as far as breaking out of loops is concerned. It just removes the functionality of one "evil" and sticks it in another.
This is largely correct but the essential point here is that goto should be limited to the point where execution paths can be easily determined. Gotos within local scope for breaking out of loops and the like are theoretically fine, but jumping between functions, jumping to arbitrary points to emulate flow control, and so on is craziness. There's definitely valid uses within C from what I've seen, though.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

zootm posted:

This is largely correct but the essential point here is that goto should be limited to the point where execution paths can be easily determined. Gotos within local scope for breaking out of loops and the like are theoretically fine, but jumping between functions, jumping to arbitrary points to emulate flow control, and so on is craziness. There's definitely valid uses within C from what I've seen, though.

Agreed.


Here's another coding horror. A student of mine refused to use a for loop. He wrote everything as a while loop


for(int i=0; i<5; ++i){
do_stuff(i);
}

was

int i=0;
while(i<5){
do_stuff(i);
++i;
}


Now they'll compile down to the same code 99.99% of the time, but it takes up more space, reads strangely to anyone else and the iterator now has scope outside of the loop. I never got a satisfactory answer from him as to why he did this.

That Turkey Story
Mar 30, 2003

zootm posted:

This is largely correct but the essential point here is that goto should be limited to the point where execution paths can be easily determined. Gotos within local scope for breaking out of loops and the like are theoretically fine, but jumping between functions, jumping to arbitrary points to emulate flow control, and so on is craziness. There's definitely valid uses within C from what I've seen, though.

Amen. I can't remember when I've ever used a goto, but specifically for jumping out of multiple levels of loops/switches it is pretty much the only solution other than possibly a return statement in some languages (yes in OO-code too). Multi-level or labled breaks are great, but again, unfortunately not all languages have that option (*cough* C, C++). In those cases, goto can be the "correct" thing to do.

Don't get caught into a religious hatred of a particular language feature; it's the naive way of becoming a better programmer. Prematurely ruling out a tool like goto may help you decide more quickly on an implementation for whatever it is you are trying to do since you are decreasing your number of options, but sometimes you may be needlessly throwing away the perfect tool for the job. I'm an atheist, deal with it.

Zakalwe posted:

Here's another coding horror. A student of mine refused to use a for loop. He wrote everything as a while loop


for(int i=0; i<5; ++i){
do_stuff(i);
}

was

int i=0;
while(i<5){
do_stuff(i);
++i;
}

If that's the worst of your problems when teaching a programming class then you're doing a very good job :p

Mikey-San
Nov 3, 2005

I'm Edith Head!

Zakalwe posted:


Here's another coding horror. A student of mine refused to use a for loop. He wrote everything as a while loop


for(int i=0; i<5; ++i){
do_stuff(i);
}

was

int i=0;
while(i<5){
do_stuff(i);
++i;
}


Now they'll compile down to the same code 99.99% of the time, but it takes up more space, reads strangely to anyone else and the iterator now has scope outside of the loop. I never got a satisfactory answer from him as to why he did this.

It probably made more sense to him but he couldn't express why or how. That's sad, because I believe a majority of problems people have with code stem from an inability to delineate the nature of their thinking and the problems their thinking attempts to solve.

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
3rd year college level CS course, not a general programming course I'm afraid. The kid was very bright, but I got an air of "self-taught programmer" from him which would probably explain why he did things that way. The same course, although not the same person, yielded the above SIMD 1/1 and y=x*1 code snippets.

I know I've been guilty of the same thing. When learning C years ago (self taught) I had a habit of using (*foo).bar rather than foo->bar which some people would call horrific.

Mikey-San
Nov 3, 2005

I'm Edith Head!

Zakalwe posted:

I know I've been guilty of the same thing. When learning C years ago (self taught) I had a habit of using (*foo).bar rather than foo->bar which some people would call horrific.

Hey, at least you knew how to use pointers. :v:

more falafel please
Feb 26, 2005

forums poster

Gotos are a better way of cleaning up in error conditions than big nested ifs in situations where exceptions either are not available or cannot be used for reasons such as performance.

This is essentially Linus' argument.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Here's a coding horror, buttbot! :rimshot: (:glomp:)

Zakalwe posted:

there's nothing wrong with using the occasional goto

Zakalwe posted:

the occasional goto

Zakalwe posted:

occasional goto

Zakalwe posted:

BOOK DEPOSITORY

:siren: You heard it here first, folks, liberal use of goto endorsed by professional programmers!

6174
Dec 4, 2004
Speaking of gotos, I ran across this today when diagnosing a crash.

code:
for i=0, nlay-1 do begin  
;  print,i
  j=-1  
  next1:  
;  print, i,j
  j=j+1  
  if(z(j) gt laybnd(0,i))then goto,next1  
  indbnd(0,i)=j  
  next2:  
  j=j+1  
  if(j eq nlev-1)then goto, bottom  
;  print, i,j,z(j),laybnd(1,i)
  if(z(j) gt laybnd(1,i))then goto,next2  
  indbnd(1,i)=j-1 
  goto,loop 
  bottom:  
  indbnd(1,i)=j 
  loop: 
endfor  
That is in IDL (a bastardized version of Fortran that kept all the bad parts and tacked on graphics), and yes IDL has the standard if/else conditionals and all the types of loops you could want.

Randomosity
Sep 21, 2003
My stalker WAS watching me...
Error handlers are just another form of control structure!
code:
 //TODO to stop mike having an appaplexy, cause this to crash so we can find the break!
try
{
     Integer.parseInt( cc_text_id_split[i] );
}
catch( NumberFormatException nfe )
{
    //continued logic
}
I want to kill the people whose code I now maintain. And wtf is an 'appaplexy'

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Randomosity posted:

I want to kill the people whose code I now maintain. And wtf is an 'appaplexy'
I would assume it's a misspelled apoplexy.

ashgromnies
Jun 19, 2004

Randomosity posted:

I want to kill the people whose code I now maintain. And wtf is an 'appaplexy'

An application-induced apoplexy.

dancavallaro
Sep 10, 2006
My title sucks

floWenoL posted:

Modern (non-lovely *cough*) languages like Java and Perl provide labeled loops and ways to break out of them, so that removes one of the two remaining use cases for gotos nowadays, and exceptions remove the other one.

Wow, I feel stupid. I've been programming in Java for like 5 years and I had no idea it had a labeled break. That would have saved me some hassles.

That Turkey Story
Mar 30, 2003

floWenoL posted:

Modern (non-lovely *cough*) languages like Java and Perl provide labeled loops and ways to break out of them, so that removes one of the two remaining use cases for gotos nowadays, and exceptions remove the other one.

Whoa, whoa, backup. Are you saying Java isn't a lovely language?

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

That Turkey Story posted:

Whoa, whoa, backup. Are you saying Java isn't a lovely language?

Oh come on when is this poo poo going to end? It's not that bad.

JediGandalf
Sep 3, 2004

I have just the top prospect YOU are looking for. Whaddya say, boss? What will it take for ME to get YOU to give up your outfielders?

Randomosity posted:

Error handlers are just another form of control structure!
code:
 //TODO to stop mike having an appaplexy, cause this to crash so we can find the break!
try
{
     Integer.parseInt( cc_text_id_split[i] );
}
catch( NumberFormatException nfe )
{
    //continued logic
}
I want to kill the people whose code I now maintain. And wtf is an 'appaplexy'
Ah we have that too in our code. I've seen it before and I shared your exact reaction. I'll probably go back tomorrow and rework it.

more falafel please
Feb 26, 2005

forums poster

code:
Foo* FindFooFromFooID(FooID id)
{
    Foo* pFoo = 0;
    for (list<foo>::iterator it = fooList.begin(); it != fooList.end(); ++foo)
    {
        if ((*it).GetID() == id)
        {
            pFoo = &(*it);
            return pFoo;
        }
    }
    return 0;
}

Foo& GetFooFromFooID(FooID id)
{
    Foo* pFoo = 0;
    for (list<foo>::iterator it = fooList.begin(); it != fooList.end(); ++foo)
    {
        if ((*it).GetID() == id)
        {
            pFoo = &(*it);
        }
    }
    if (pFoo != 0)
    {
        return *pFoo;
    }
    else
    {
        assert(FALSE, "Trying to get a nonexistent Foo");
    }
}
Now aside from the copy-paste mentality of this code, it's not *too* bad. Use FindFooFromFooID when you might be looking up something that isn't there, use GetFooFromFooID when you know it should be there.

Every usage of either of these functions looks like this:
code:
Foo* pFoo = FindFooFromFooID(ID);
if (pFoo)
{
    Foo& Foo = GetFooFromFooID(ID);
    // do stuff
}
// ELSE COMPLETELY HIDE PROGRAMMING OR WORSE YET ART ERRORS
I got to get rid of this today. This was a win.

Zombywuf
Mar 29, 2008

On the subject of exception based control flow:
code:
class A {
  ...
};

class a : public A {
  ...
};

class b : public A {
  ...
};

void dispatch(const A &obj) {
  try {
    throw obj;
  } catch (a &a_obj) {
    do_stuff_with_a(a_obj);
  } catch (b &b_obj) {
    do_stuff_with_b(b_obj);
  }
}

Smackbilly
Jan 3, 2001
What kind of a name is Pizza Organ! anyway?

Zombywuf posted:

On the subject of exception based control flow:
code:
class A {
  ...
};

class a : public A {
  ...
};

class b : public A {
  ...
};

void dispatch(const A &obj) {
  try {
    throw obj;
  } catch (a &a_obj) {
    do_stuff_with_a(a_obj);
  } catch (b &b_obj) {
    do_stuff_with_b(b_obj);
  }
}

haha oh god... at least that gets creativity points. Who needs RTTI when you have exceptions?

The Noble Nobbler
Jul 14, 2003
int newNum = Utils.PrestoChango(x);

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Smackbilly posted:

haha oh god... at least that gets creativity points. Who needs RTTI when you have exceptions?

On the subject, I got to extend a code base for a student project that did stuff like this:

code:
if(successCondition)
{
  //do some cleanup
  throw new WhateverIdoHappenedToWorkException(returnValue);
}
else
{
  /*do the same cleanup with a couple things possibly 
    commented out for some reasonbecause copy and paste 
    is like a control structure, right?*/
  throw new WhateverIdoFailedException();
}
And the calling code had a big try/catch/catch/catch... block to handle the 'result'. Some had multiple success or failure 'conditions' that had to be caught. This technique was used instead of events, or at least mostly in places events should have been used*, in C#. It was all written by a 4th year CS undergrad.

*Class functions would throw exceptions on completion (or error) to be caught by other class functions. There were also a few places where whoever wrote it clearly could have used a switch if it had occurred to him.

more falafel please
Feb 26, 2005

forums poster

Munkeymon posted:

On the subject, I got to extend a code base for a student project that did stuff like this:

code:
if(successCondition)
{
  //do some cleanup
  throw new WhateverIdoHappenedToWorkException(returnValue);
}
else
{
  /*do the same cleanup with a couple things possibly 
    commented out for some reasonbecause copy and paste 
    is like a control structure, right?*/
  throw new WhateverIdoFailedException();
}
And the calling code had a big try/catch/catch/catch... block to handle the 'result'. Some had multiple success or failure 'conditions' that had to be caught. This technique was used instead of events, or at least mostly in places events should have been used*, in C#. It was all written by a 4th year CS undergrad.

*Class functions would throw exceptions on completion (or error) to be caught by other class functions. There were also a few places where whoever wrote it clearly could have used a switch if it had occurred to him.

Exceptions are like goto, only they're just "gosomewhere".

Adbot
ADBOT LOVES YOU

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



more falafel please posted:

Exceptions are like goto, only they're just "gosomewhere".

Plus they're objects and can carry a payload of other objects. It's the perfect fusion of functional and object oriented 'features' in one disturbing package.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply