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
Vanadium
Jan 8, 2005

fwiw in gcc: http://gcc.gnu.org/gcc-4.7/changes.html

quote:

A string length optimization pass has been added. It attempts to track string lengths and optimize various standard C string functions like strlen, strchr, strcpy, strcat, stpcpy and their _FORTIFY_SOURCE counterparts into faster alternatives. This pass is enabled by default at -O2 or above, unless optimizing for size, and can be disabled by the -fno-optimize-strlen option. The pass can e.g. optimize

code:
char *bar (const char *a)
{
  size_t l = strlen (a) + 2;
  char *p = malloc (l); if (p == NULL) return p;
  strcpy (p, a); strcat (p, "/"); return p;
}


into:

code:
char *bar (const char *a)
{
  size_t tmp = strlen (a);
  char *p = malloc (tmp + 2); if (p == NULL) return p;
  memcpy (p, a, tmp); memcpy (p + tmp, "/", 2); return p;
}


vvvv sorry, that wasn't supposed to be a coding horror but to tie into the discussion on the last page whether redundant calls to strlen are sometimes optimized away.

Vanadium fucked around with this message at 09:26 on Nov 7, 2012

Adbot
ADBOT LOVES YOU

Deffon
Mar 28, 2010


What am I missing here exactly? At first I though that p wasn't NULL-terminated, but then I saw that 2 bytes were copied from "/" to p.

Edit: ^^^Gotcha.

Edit: VVV Yeah I know, I just couldn't find any flaws except for potential lack of NULL-termination.

Deffon fucked around with this message at 12:10 on Nov 7, 2012

Qwertycoatl
Dec 31, 2008

Deffon posted:

What am I missing here exactly? At first I though that p wasn't NULL-terminated, but then I saw that 2 bytes were copied from "/" to p.

"/" is a string with two bytes, a '/' and a null. Copying those two bytes null-terminates p. It doesn't matter that the first memcpy doesn't null-terminate, because it's immediately followed by one that does.

a7m2
Jul 9, 2012


Dicky B posted:

I came across this switch statement indentation style earlier today:
code:
switch(x)
{
        case 1:
    {
    ...
    break;
    }
        case 2:
    {
    ...
    break;
    }
        default:
    {
    ...
    break;
    }
}
This style is used consistently across the entire project.

It's horrible, but at least it's consistent. Inconsistent formatting is the worst thing ever.

corgski
Feb 6, 2007

Silly goose, you're here forever.

How do you scale an 8-bit hex value to a percentage in JavaScript? With a lookup table, of course! :eng99:

gently caress ETC.

code:
var HTOPT  = new Array(

     0,  1,  1,  1,  2,  2,  2,  3,  3,  4,  4,  4,  5,  5,  5,  6, /* 00 - 0f */

     6,  7,  7,  7,  8,  8,  9,  9,  9, 10, 10, 11, 11, 11, 12, 12, /* 10 - 1f */

    13, 13, 13, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 18, 18, 18, /* 20 - 2f */

    19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 24, 25, /* 30 - 3f */

    25, 25, 26, 26, 27, 27, 27, 28, 28, 29, 29, 29, 30, 30, 31, 31, /* 40 - 4f */

    31, 32, 32, 33, 33, 33, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, /* 50 - 5f */

    38, 38, 38, 39, 39, 40, 40, 40, 41, 41, 42, 42, 42, 43, 43, 44, /* 60 - 6f */

    44, 44, 45, 45, 45, 46, 46, 47, 47, 47, 48, 48, 49, 49, 49, 50, /* 70 - 7f */

    50, 51, 51, 51, 52, 52, 53, 53, 53, 54, 54, 55, 55, 55, 56, 56, /* 80 - 8f */

    56, 57, 57, 58, 58, 58, 59, 59, 60, 60, 60, 61, 61, 62, 62, 62, /* 90 - 9f */

    63, 63, 64, 64, 64, 65, 65, 65, 66, 66, 67, 67, 67, 68, 68, 69, /* a0 - af */

    69, 69, 70, 70, 71, 71, 71, 72, 72, 73, 73, 73, 74, 74, 75, 75, /* b0 - bf */

    75, 76, 76, 76, 77, 77, 78, 78, 78, 79, 79, 80, 80, 80, 81, 81, /* c0 - cf */

    82, 82, 82, 83, 83, 84, 84, 84, 85, 85, 85, 86, 86, 87, 87, 87, /* d0 - df */

    88, 88, 89, 89, 89, 90, 90, 91, 91, 91, 92, 92, 93, 93, 93, 94, /* e0 - ef */

    94, 95, 95, 95, 96, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 100  /* f0 - ff */

);



var PTOHT = new Array(

    0x00, 0x03, 0x05, 0x08, 0x0a, 0x0d, 0x0f, 0x12, 0x14, 0x17,  /*  0 -  9 */

    0x1a, 0x1c, 0x1f, 0x21, 0x24, 0x26, 0x29, 0x2b, 0x2e, 0x30,  /* 10 - 19 */

    0x33, 0x36, 0x38, 0x3b, 0x3d, 0x40, 0x42, 0x45, 0x47, 0x4a,  /* 20 - 29 */

    0x4d, 0x4f, 0x52, 0x54, 0x57, 0x59, 0x5c, 0x5e, 0x61, 0x63,  /* 30 - 39 */

    0x66, 0x69, 0x6b, 0x6e, 0x70, 0x73, 0x75, 0x78, 0x7a, 0x7d,  /* 40 - 49 */

    0x80, 0x82, 0x85, 0x87, 0x8a, 0x8c, 0x8f, 0x91, 0x94, 0x96,  /* 50 - 59 */

    0x99, 0x9c, 0x9e, 0xa1, 0xa3, 0xa6, 0xa8, 0xab, 0xad, 0xb0,  /* 60 - 69 */

    0xb3, 0xb5, 0xb8, 0xba, 0xbd, 0xbf, 0xc2, 0xc4, 0xc7, 0xc9,  /* 70 - 79 */

    0xcc, 0xcf, 0xd1, 0xd4, 0xd6, 0xd9, 0xdb, 0xde, 0xe0, 0xe3,  /* 80 - 89 */

    0xe6, 0xe8, 0xeb, 0xed, 0xf0, 0xf2, 0xf5, 0xf7, 0xfa, 0xfc,  /* 90 - 99 */

    0xff

);



function hexToPercent(hex) {
    var ret = 0;

	hex = new Number(hex);

    if ((hex <= 255) && (hex >= 0))
    {
        ret = HTOPT[hex];
    }

    return ret;

}

function percentToHex(percent) {

    var ret = 0;

	percent = new Number(percent);

    if ((percent <= 100) && (percent >= 0))
    {
        ret = PTOHT[percent];
    }

    return ret;

}

The Gripper
Sep 14, 2004
i am winner
That's a little bit amazing. The lookup table is obviously generated, so why go to all the trouble of not just using the formula outright?

hobbesmaster
Jan 28, 2008

The Gripper posted:

That's a little bit amazing. The lookup table is obviously generated, so why go to all the trouble of not just using the formula outright?

Premature optimization?

corgski
Feb 6, 2007

Silly goose, you're here forever.

To be fair, this is from a company that primarily did embedded systems at the time, so maybe they were working with chips that would do lookups faster than 16-bit integer math before being asked to write a web interface for said embedded system (and repeating old habits.)

Dimming and power control should not be this complicated. I should not have to reverse engineer their web interface just to make it work in a browser newer than IE 6.

At least the university is paying me to do it.

corgski fucked around with this message at 21:43 on Nov 7, 2012

A A 2 3 5 8 K
Nov 24, 2003
Illiteracy... what does that word even mean?
code:
// Temporary Hack Part 1 of 3: ...


...


// Temporary Hack Part 3 of 3: ...
There is no Temporary Hack Part 2.

Tamba
Apr 5, 2010

There probably was, but it really was temporary.

Zamujasa
Oct 27, 2010



Bread Liar

A A 2 3 5 8 K posted:

code:
// Temporary Hack Part 1 of 3: ...

...

// Temporary Hack Part 3 of 3: ...
There is no Temporary Hack Part 2.
:ghost:




Looking through some more BossCode™ today. Some obscure feature isn't working. It's getting into our system somehow, with code that flows like this ( :downs: )
code:
 log("Doing something important");
/* big block of stupid code here */
Somehow the big block of code is being run, but depending on what triggers it, the log won't update. There's no reason it shouldn't, but I have no idea why. It's also entirely possible that this thing is talking to a totally different server that has another copy of this code that I can't see, of course. We don't track this sort of thing. :downs:



The rest of the code is even worse. The format we get data in is either plain text ("AAA BB CCCCC D<sep>"), or plain text followed by a binary chunk ("AAA BB FFFFF EE <binary data here><sep>").

BossCode™ solution: Run the whole thing through binhex()! Don't forget to also use hexadecimal when looking for data -- "53484954" is a lot better than binhex("poo poo"), and don't worry about starting halfway into a hexadecimal value.

There's a huge mess of code that literally splits the file into chunks by doing strpos($d, "20"), $d2 = substr(...), strpos($d2, "20"), $d3 = substr(...), strpos($d3, "20") .... for what could have been accomplished with a simple explode(" ", $d, 5). :sigh:



I tried to rewrite (and fix all the bugs in) this and got decently far in two days, but apparently that was too long for my boss, who needed it now now now now now. So his lovely, broken version that he cobbled together over three weeks is what's running now. And we get to support it, of course.


This is like some sick joke.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



A A 2 3 5 8 K posted:

code:
// Temporary Hack Part 1 of 3: ...


...


// Temporary Hack Part 3 of 3: ...
There is no Temporary Hack Part 2.

It's like that old pigs with numbers on them urban legend, but with hacks. Now you'll never know if you found all the hacks.

Polio Vax Scene
Apr 5, 2009



Label your hacks 1, 2, and 4, and commit them to svn...

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

Imagine three hacks on the edge of a cliff...

McGlockenshire
Dec 16, 2005

GOLLOCKS!
PHP works the same way.

Viggen
Sep 10, 2010

by XyloJW
Johnny FiveMySQLescapestrings?

uncleTomOfFinland
May 25, 2008

Ithaqua posted:

If they're using Visual Studio, here's a hint:
Edit -> Advanced -> Format Document
(or if you prefer, CTRL-D, E).

That will take care of a lot of the more egregious formatting problems.

I'm (sorta kinda) coaching a first year programming course and it just boggles the mind that some students wont hit hit alt+shift+f after they are done writing the code. Some seem even downright offended when I gently suggest they do that before showing anything to me. :psyduck:

The Gripper
Sep 14, 2004
i am winner

uncleTomOfFinland posted:

I'm (sorta kinda) coaching a first year programming course and it just boggles the mind that some students wont hit hit alt+shift+f after they are done writing the code. Some seem even downright offended when I gently suggest they do that before showing anything to me. :psyduck:
I don't even know how some people end up with code so poorly formatted. My best guess is that they type hundreds of lines at a time without looking at intellisense screaming about their broken poo poo then add missing braces, colons, semicolons, whatever afterwards and leave the formatting as-is. It's either that or hamfisted fighting with the editor, or working in a plaintext editor (I'll give that a pass, for people learning).

That switch statement above looks like someone's had an extra brace in there and instead of removing it they just added matching braces and it's hosed the formatting up as a result. But document-wide indenting like that? That's the work of a crazy person.

baquerd
Jul 2, 2007

by FactsAreUseless

uncleTomOfFinland posted:

I'm (sorta kinda) coaching a first year programming course and it just boggles the mind that some students wont hit hit alt+shift+f after they are done writing the code. Some seem even downright offended when I gently suggest they do that before showing anything to me. :psyduck:

This can be annoying when your formatting causes version control integration problems. The solution, of course, is to get the entire team on the same formatting scheme, but this is a holy war level of issue with some people.

nielsm
Jun 1, 2009



SQL syntax.

ALTER TABLE foo ALTER COLUMN ...
ALTER TABLE foo CHANGE COLUMN ...
ALTER TABLE foo MODIFY COLUMN ...

These are used for different things.
Why.

(Haven't checked with other DBMS, but MySQL has ALTER COLUMN for changing the default value and nothing else. CHANGE COLUMN is used for renaming and rearranging columns. MODIFY COLUMN changes the data type and a few other things.)

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

baquerd posted:

This can be annoying when your formatting causes version control integration problems. The solution, of course, is to get the entire team on the same formatting scheme, but this is a holy war level of issue with some people.

You need a better merge tool. Perforces merge tool can ignore all whitspace changes which is pretty nice :)

Frozen Peach
Aug 25, 2004

garbage man from a garbage can
Not exactly a horror, but really funny, this has been in the Android 4.2 source code

code:
   /**
     * Used to determine whether the user making this call is subject to
     * teleportations.
     * @return whether the user making this call is a goat 
     */
    public boolean isUserAGoat() {
        return false;
    }

Bunny Cuddlin
Dec 12, 2004

Frozen-Solid posted:

Not exactly a horror, but really funny, this has been in the Android 4.2 source code

code:
   /**
     * Used to determine whether the user making this call is subject to
     * teleportations.
     * @return whether the user making this call is a goat 
     */
    public boolean isUserAGoat() {
        return false;
    }

Could this be related to that Niantic poo poo?

Sereri
Sep 30, 2008

awwwrigami

Bunny Cuddlin posted:

Could this be related to that Niantic poo poo?

Probably this

baquerd
Jul 2, 2007

by FactsAreUseless

Hard NOP Life posted:

You need a better merge tool. Perforces merge tool can ignore all whitspace changes which is pretty nice :)

Yeah, SVN 1.6 doesn't do that.

Hughlander
May 11, 2005

baquerd posted:

Yeah, SVN 1.6 doesn't do that.

You can download the p4 merge tool from their site. Though you agree that you have a license to use it before you can download it. I use it at work for git since one of our projects does use p4!

shadowbolt
Dec 1, 2003

Rubber Ducky, you're the one...

Hughlander posted:

You can download the p4 merge tool from their site. Though you agree that you have a license to use it before you can download it. I use it at work for git since one of our projects does use p4!

I know you're talking about a merge tool and not the VCS's merge command itself, but with Git you can use git merge -Xignore-space-change (or ignore-space-at-eol, or ignore-all-space) on recursive merges, which often resolves the merge in this kind of situation without requiring painstaking manual intervention or a special merge tool.

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


Or get everyone to use a whitespace trimmer setting/plugin in their editor.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
what about when I rewrite a file from Windows-1252 to UTF-8 and commit it and now it's 100% changed

Bonfire Lit
Jul 9, 2008

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

congratulations, all of the bugs in that file are now your fault

Opinion Haver
Apr 9, 2007

Aleksei Vasiliev posted:

what about when I rewrite a file from Windows-1252 to UTF-8 and commit it and now it's 100% changed

Are you saying you have non-latin characters in your source code?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
When I'm implementing algorithms from a scientific paper I often have comments that use unicode symbols to show the notation used in the paper, and I use unicode characters in format strings in prettyprinting routines for essentially the same reason- unicode escapes are awful to read. I think both cases are reasonable justifications for non-latin characters in source files.

baquerd
Jul 2, 2007

by FactsAreUseless

Hughlander posted:

You can download the p4 merge tool from their site. Though you agree that you have a license to use it before you can download it. I use it at work for git since one of our projects does use p4!

I don't personally have a problem merging (the built-in IntelliJ merge tool is pretty good 99% of the time), but our integration guy and a dinosaur are going to use the built in SVN merge and it's a royal pain to resolve all the poo poo you can sometimes cause with that and autoformat. I had a knock down drag out argument with the dinosaur the other week where he was convinced that auto formatting will sometimes break code and it's a risk management thing, and that to use anything other than the built in merge tool is just begging to break the whole repo... somehow. That's kind of a coding horror. So is having an integration guy that's not a developer and knows nothing about the code and therefore doesn't have the capacity to make decisions on merge conflicts.

hobbesmaster
Jan 28, 2008

Internet Janitor posted:

When I'm implementing algorithms from a scientific paper I often have comments that use unicode symbols to show the notation used in the paper, and I use unicode characters in format strings in prettyprinting routines for essentially the same reason- unicode escapes are awful to read. I think both cases are reasonable justifications for non-latin characters in source files.

Inline latex and only read the doxygen output.

Toady
Jan 12, 2009

This has probably been linked at some point in the past, but this exists: http://hatepaste.com/

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


yaoi prophet posted:

Are you saying you have non-latin characters in your source code?

Why not call a ∑ a ∑?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Toady posted:

This has probably been linked at some point in the past, but this exists: http://hatepaste.com/

http://hatepaste.com/paste/811902ee

lol so witty

PrBacterio
Jul 19, 2000

Doc Hawkins posted:

Why not call a ∑ a ∑?
Well, just call it sigma then? :confused:

I might be old-fashioned, but in my opinion the only places non-ASCII characters should appear in source code is inside of comments or string literals.

vvv Again, just write lambda or something, I don't see the problem. About the anglocentricism I'll just note that as long as we're using languages with English-language keywords there's no way around that anyway, which you'll note is also not a problem. Programming languages are formal languages defined on their own terms, after all, and a limited alphabet to derive valid and, most importantly, unambiguous sentences from is a helpful feature to have. Strictly speaking, though, I'm not opposed in principle to a language using a well-defined, limited set of symbols outside of the ASCII range, in particular for use as keyword tokens for things like λ, , , , , and so on. But just permitting any arbitrary character out of the entire Unicode range to be used in identifiers or whatnot seems excessive to me, and just opens the door to all kinds of confusion and silliness like variables with names like i vs. í vs. і vs. ı vs. ι. To which you might say, well, if someone does something like that it's their own fault but then again there's really no need for the added complexity to make that even possible in the first place, and any programming language you might look at will pretty much have to have a well-defined and limited subset of valid, permitted characters to be used in identifiers anyway.

PrBacterio fucked around with this message at 02:58 on Nov 16, 2012

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Honestly, I find code a lot easier to read when I can indicate a lambda with λ instead of \ or whatever.

"Ascii-only" also seems pretty anglocentric, not everyone likes to contort their natural language into a bastardized 7-bit representation just to give something a name.

Adbot
ADBOT LOVES YOU

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

PrBacterio posted:

Well, just call it sigma then? :confused:

I might be old-fashioned, but in my opinion the only places non-ASCII characters should appear in source code is inside of comments or string literals.

Oh, you're clearly not the target market for some of the operator craziness Scala can let you get into. Dick on Java Posse was talking a while back about how's he's consulting for a company with a lot of math-focused people. Apparently the idea that they can just type "x ∆ y" or "∑ (0 to 10)( x => x*x)" really gets them hard.

Edit: Those are some pretty simple examples that just happen to be more clear if you're used to thinking in deltas and whatnot. Scala can also become a real, real horror with this poo poo. When adding ★ as an operator starts to feel sane, it's time to step back. You're just creating a horror at that point.

ultramiraculous fucked around with this message at 02:52 on Nov 16, 2012

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