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
Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Chuu posted:

That "Standard module error handler" is where RAISE_ERROR lives, which in and of itself is a bit of a horror because before SQL Server 2012 you could not re-throw errors, which meant using RAISE_ERROR and more ugly boilerplate for reporting.

Are all databases this terrible for developing robust stored procs?

It isn't ordinarily that bad - you're showing an example dealing with nested transactions and try/catch at the same time, and frankly you're probably better off structuring the logic of your stored procs so you don't have to deal with nested transactions if you're at the point where you feel like they are necessary for some reason.

Adbot
ADBOT LOVES YOU

Zamujasa
Oct 27, 2010



Bread Liar

Kim Jong III posted:

Pretty much what Sinestro said, you're almost guaranteed to stumble into a viper's pit of hellish practices. There's just something special about PHP that brings out the worst in people.

Even when you move into other languages! My boss is now doing PHP+VB6+Embedded system stuff and it still has some crazy ideas in it.

Like how our board has 8 inputs, and those are mapped to "input.php?num=0" through 7... while num=8 is reserved for general "heartbeats" that the board sends out every so often. :what:

I got my major start in programming in PHP but I've tried to branch out to other stuff more than once (Javascript + jQuery, Lua, a little bit of 6502 for amusement). Hopefully I'm no longer making really awful horrors. :ohdear:

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
To me that sounds more like EE's trying their hand at software than PHP turning their brains to mush (although that might be a factor)

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Wait, embedded PHP is a thing?

...I think I might need to stop buying appliances.

ymgve
Jan 2, 2004


:dukedog:
Offensive Clock
To derail a little bit, what are some examples of really good code?

akadajet
Sep 14, 2003

ymgve posted:

To derail a little bit, what are some examples of really good code?

"Coding Bliss: post the code that makes your job wonderful." just doesn't seem as interesting.

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.

Internet Janitor posted:

Wait, embedded PHP is a thing?

...I think I might need to stop buying appliances.

My guess is that it's embedded code running on the board -> .NET to talk to a COM driver or serial port -> PHP to chat with the .NET code via a web interface.

Zamujasa posted:

num=8 is reserved for general "heartbeats" that the board sends out every so often. :what:

Heartbeats aren't a bad thing if you're dealing with external hardware, particularly if it's controlling something that is mechanical or potentially dangerous in any way. They let both sides of the connection know if the control line is broken so that everything can shut down to a safe state.

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

ymgve posted:

To derail a little bit, what are some examples of really good code?

In before duff's device.

Also, I really don't think you can call much code "really good" simply because the field is SO young and we are SO bad at it. I'd argue that we're still in the "cave painting" era of computer programming.

pseudopresence
Mar 3, 2005

I want to get online...
I need a computer!
There is no good code, only awful code and REALLY awful code.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

ymgve posted:

To derail a little bit, what are some examples of really good code?
literally none of it, anywhere, ever

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Good code is simple and straightforward, and therefore utterly boring.

Zamujasa
Oct 27, 2010



Bread Liar

PDP-1 posted:

My guess is that it's embedded code running on the board -> .NET to talk to a COM driver or serial port -> PHP to chat with the .NET code via a web interface.

It's actually some embedded language that talks to a PHP script; I meant that the use of PHP for most other tasks by my boss appears to have fried his brain.

quote:

Heartbeats aren't a bad thing if you're dealing with external hardware, particularly if it's controlling something that is mechanical or potentially dangerous in any way. They let both sides of the connection know if the control line is broken so that everything can shut down to a safe state.

I know. It's not the heartbeat that's the :what: in that, it's that it is an actual input number (8) instead of zero or nothing. It also means that we can't change the board, ever, as if we did then we'd have to use 0-7 and 9-16 for input numbers.

That is, mostly getting at the point that the choice he made here is bizarre and does nothing but complicate things in the future, when the correct choice (0 for heartbeat, 1-8 on activity) is comparatively painless and simple, and would require literally no extra work on top of it.

Johnny Cache Hit
Oct 17, 2011

ymgve posted:

To derail a little bit, what are some examples of really good code?

code:
main:
   sll $0, $0, 0
It's all downhill from there :(

PrBacterio
Jul 19, 2000

Plorkyeran posted:

Good code is simple and straightforward, and therefore utterly boring.
Yup, coding is kind of like the game of Go in that regard: You can't actually be *good* at it; the best anyone can hope for is to learn not to make so many drat mistakes anymore. "Good" code, much like a good game of Go, is one that contains few if any mistakes. Which is still incredibly hard to achieve, though.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Perhaps more importantly, most pieces of code that are good and short are uninteresting. A few lines of code that does something neat is most likely clever, and therefore by many standards no longer good, as such approaches are no longer easy to understand at a glance or maintain.

(Also more practically speaking, presenting a chunk of code you consider perfect to a forum of programmers who can and will pick anything apart is sort of leaning into a punch.)

npe
Oct 15, 2004
It's like anything, whether it's music, or movies, or literature. Agreeing on what is good is impossible. Agreeing on what is terrible is way easier.

ymgve
Jan 2, 2004


:dukedog:
Offensive Clock
I wasn't thinking about code snippets, but more larger projects which are cleanly organized and relatively easy to understand.

Johnny Cache Hit
Oct 17, 2011

ymgve posted:

I wasn't thinking about code snippets, but more larger projects which are cleanly organized and relatively easy to understand.

If you'd like to look at a well written C codebase, I'd highly recommend SQLite. drh is a really good programmer and it shows.

1337JiveTurkey
Feb 17, 2005

Honestly you'd probably get more mileage out of a "Code that you know in practice is terrible but still makes you go :smuggo:" thread.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
That could be pretty interesting, actually.

tef
May 30, 2004

-> some l-system crap ->

ymgve posted:

I wasn't thinking about code snippets, but more larger projects which are cleanly organized and relatively easy to understand.

I tend to find code beautiful in theory and rarely in practice. Code can only be as beautiful as the problem it solves, and most of the problems we face are ugly.

tef
May 30, 2004

-> some l-system crap ->

Plorkyeran posted:

That could be pretty interesting, actually.

Isn't that the screenshot thread :3:

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

tef posted:

I tend to find code beautiful in theory and rarely in practice. Code can only be as beautiful as the problem it solves, and most of the problems we face are ugly.

That's why I was tempted to cite jQuery as an example of good code, even though the code itself isn't beautiful. It provides an elegant interface that solves difficult problems, which is often way more important than the underlying implementation.

Opinion Haver
Apr 9, 2007

Courtesy of reddit:

code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title/This is a page title/

hi
The W3C validator passes this with no errors and two warnings.

shrughes
Oct 11, 2008

(call/cc call/cc)
Yeah, that's an SGML-ism that became an issue to some web standards spergs when XHTML self-closing tags came about.

gonadic io
Feb 16, 2011

>>=

1337JiveTurkey posted:

Honestly you'd probably get more mileage out of a "Code that you know in practice is terrible but still makes you go :smuggo:" thread.

code:
 powerSet = filterM (const [True, False]) 

revmoo
May 25, 2006

#basta
php:
<?
$programCount = count($programs);
$column1Count = $column2Count = $column3Count = 0;
if ($programCount%3) $column3Count = - 2 + $column1Count = $column2Count = round($programCount / 3) + 1;
if (!($programCount%3)) $column1Count = $column2Count = $column3Count = $programCount / 3;
?>
I cooked up this columnizing code the other day to taunt my coworkers.

PrBacterio
Jul 19, 2000

1337JiveTurkey posted:

Honestly you'd probably get more mileage out of a "Code that you know in practice is terrible but still makes you go :smuggo:" thread.
Yeah then we could be looking forward to dozens of posts about Duff's device, XOR-integer swap and XOR-linked lists? :eng99:

Actually I do happen to think that the Linux kernel is a good example of good code, from what I've seen of it. It's very unpretentious, the only "best practices" approach it takes being an undogmatic "write what you mean."

tef
May 30, 2004

-> some l-system crap ->

DaTroof posted:

That's why I was tempted to cite jQuery as an example of good code, even though the code itself isn't beautiful. It provides an elegant interface that solves difficult problems, which is often way more important than the underlying implementation.

I think my favourite piece of code is this polyglot quine

code:

ufarn
May 30, 2009

ymgve posted:

To derail a little bit, what are some examples of really good code?
Peter Norvig's Python code is pretty dope.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

PrBacterio posted:

Actually I do happen to think that the Linux kernel is a good example of good code, from what I've seen of it. It's very unpretentious, the only "best practices" approach it takes being an undogmatic "write what you mean."
And then you get things like
code:
blah(*p);

if(!p)
    return;
that inspired their own gcc flag.

KaneTW
Dec 2, 2011

Mustach posted:

And then you get things like
code:
blah(*p);

if(!p)
    return;
that inspired their own gcc flag.

What the hell.

Also, what gcc flag did it inspire?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

KaneTW posted:

What the hell.

Also, what gcc flag did it inspire?

-fdelete-null-pointer-checks
Assume that programs cannot safely dereference null pointers, and that no code or data element resides there. This enables simple constant folding optimizations at all optimization levels. In addition, other optimization passes in GCC use this flag to control global dataflow analyses that eliminate useless checks for null pointers; these assume that if a pointer is checked after it has already been dereferenced, it cannot be null.

Note however that in some environments this assumption is not true. Use -fno-delete-null-pointer-checks to disable this optimization for programs that depend on that behavior.

Some targets, especially embedded ones, disable this option at all levels. Otherwise it is enabled at all levels: -O0, -O1, -O2, -O3, -Os. Passes that use the information are enabled independently at different optimization levels

Vanadium
Jan 8, 2005

Would you be sure that something in blah() didn't set p to NULL after it had already been dereferenced, directly or indirectly? Aliasing and mutable variables are a pain~

nielsm
Jun 1, 2009



Vanadium posted:

Would you be sure that something in blah() didn't set p to NULL after it had already been dereferenced, directly or indirectly? Aliasing and mutable variables are a pain~

C++ code:
blah(*p);

if(!p)
    return;
p has to be of type T* to be dereferencable in the first place. blah() has to take a parameter of type T. How could blah() at all possibly get a chance at modifying the T* in the caller when it only has a T to work with?

Now, this on the other hand would be a completely different matter:
C++ code:
blah(&p);

if(!p)
    return;

Vanadium
Jan 8, 2005

code:
int* p;

void blah(int i) {
  if(i)
    p = NULL;
}

void foo() {
  int i = 42;
  p = &i;

  blah(*p);

  if(!p)
    return;

  assert(*p == 42);
}
Oh look my code snippet is in the right thread already

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

The original kernel bug was something like
code:
void foo(int *p) {
  int i = *p;
  if (!p)
    return;
  /* ... */
}
so there's no aliasing problems here.

pseudorandom name
May 6, 2007

Note that this was a kernel bug that was fixed, but that gcc a) didn't warn about the check-after-dereference and b) deleting the check-after-dereference turned it from a logic error into a local root exploit.

Brain Candy
May 18, 2006

pseudorandom name posted:

Note that this was a kernel bug that was fixed, but that gcc a) didn't warn about the check-after-dereference and b) deleting the check-after-dereference turned it from a logic error into a local root exploit.

To be fair to GCC, this is a case where the C spec explicitly permits demons to fly out your nose. See http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html for why this sort of thing is baked into C.

Adbot
ADBOT LOVES YOU

pseudorandom name
May 6, 2007

There's a qualitative difference between narrowly following the spec and providing a useful tool, and a knee-jerk response of "But the spec allows us to do this!" doesn't ingratiate you with your users.

edit: And to their credit in this case, they did add that compiler switch.

pseudorandom name fucked around with this message at 20:48 on Jun 17, 2012

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