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
Trammel
Dec 31, 2007
.

Switzerland posted:

Hail Satan.

iospace posted:

Hail Satan

ultrafilter posted:

Hail Satan.

Could somebody refactor this so we're not just cut'n'pasting code everywhere?

Adbot
ADBOT LOVES YOU

CPColin
Sep 9, 2003

Big ol' smile.
Sail Hatan

Ah poo poo I forgot to write a test first

Carbon dioxide
Oct 9, 2012

CPColin posted:

Sail Hatan

Ah poo poo I forgot to write a test first

My IDE says you have a typo in your variable name, did you mean "SailHating"?

Ola
Jul 19, 2004

redleader posted:

tldr: software is hell, testing won't help, hail Satan.

hail Satan.

e: what the hell happened to the function that was supposed to capitalize sentences?

Soricidus
Oct 21, 2010
freedom-hating statist shill
culture.setPhrases["religious-greeting"].format(culture.findSignificantFigure("devil", "trickster")) // fix POST-42069 cultural sensitivity

Ruggan
Feb 20, 2007
WHAT THAT SMELL LIKE?!


Hi guys, i wrote a better function. Can someone write some test cases and accept my pull request? Thanks

code:
function makeSentenceCase(sentence) {
  let newLetters = [];
  let newSentence = "";
  for (var i = 0; i < sentence.length; i++) {
    let newLetter = '';
    
    newLetter = sentence.charAt(i);
    newLetter = newLetter.toUpperCase();
    newLetters.push(newLetter);
  
    if (i !== 0) { 
      newLetter = sentence.charAt(i);
      newLetters.pop();
      newLetters.push(newLetter);
    } 
  }
  
  for (var i = 0; i < newLetters.length; i++) {
    let nextLetter = '';
    nextLetter = newLetters[i];
    if (newSentence.length > 0) {
      newSentence = newSentence.split("");
      newSentence.push(nextLetter)
      newSentence = newSentence.join("");
    } else {
      newSentence = nextLetter;
    }
  }

  return newSentence;
}

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof
code:
test('makeSentenceCase', () => {
    expect(() => {
        makeSentenceCase('test');
    }).not.toThrow(Error);
});
it's green. ship it

DaTroof fucked around with this message at 16:16 on Jun 4, 2019

euclidian88
Aug 3, 2013

ultrafilter posted:

Something I've been thinking about lately is metamorphic testing, which involves looking at how the output of a function/program changes when the input changes. In a lot of cases it's much easier to reason about what those changes should be like than to figure out what the output should be for a given input.

I have a program that takes in a bunch of (x, y) coordinates and fits a curve to them. For various reasons, I can't use anything off the shelf, and as a result there really isn't any gold standard for what the answer should be. However, I know for a fact that if I go from (x, y) to (x, y + 1) that the output curve should move up by one as well. That's a very simple example (and the actual tests would be a bit more complicated), but I think it at least hints at how useful this is for the problems that I'm working with.

I do a lot of geometry programming and have a few tests like that they end up super useful in checking. The issues you can often run into is floating point numbers whose pathologies will just mess everything up...

You tend to have to end up defining the results up to some tolerance and then most of the results you naively expect to hold have weird edge cases that dont. For example the point of intersection between 2 lines (sensible) can often be a point that cannot be exactly represented in doubles as a point that lies on both lines, if you define everything up to a fixed tolerance you can get potentially several points that are (to tolerance) on both lines but are distinct (to tolerance).

Really the issue is that a lot of geometric methods which are nice and easy to specify tend to have vast numbers of edge cases when transfered to an approimate world and result in methods that are woefully underspecified. Otoh in nearly all actual non pathological cases everything makes sense.

Nth Doctor
Sep 7, 2010

Darkrai used Dream Eater!
It's super effective!


euclidian88 posted:

I do a lot of geometry programming

Wonderful id and post combo.

Linear Zoetrope
Nov 28, 2011

A hero must cook

euclidian88 posted:

I do a lot of geometry programming and have a few tests like that they end up super useful in checking. The issues you can often run into is floating point numbers whose pathologies will just mess everything up...

You tend to have to end up defining the results up to some tolerance and then most of the results you naively expect to hold have weird edge cases that dont. For example the point of intersection between 2 lines (sensible) can often be a point that cannot be exactly represented in doubles as a point that lies on both lines, if you define everything up to a fixed tolerance you can get potentially several points that are (to tolerance) on both lines but are distinct (to tolerance).

Really the issue is that a lot of geometric methods which are nice and easy to specify tend to have vast numbers of edge cases when transfered to an approimate world and result in methods that are woefully underspecified. Otoh in nearly all actual non pathological cases everything makes sense.

Yeah, I wrote a 3D math library once and there were dumb times where I had to test an edge case, get it to fail, spend an hour puzzling out why (or sometimes just see the output of the number) and be like:

code:
// Normally we use 1e-4 but this fails at that threshold, I hand verified it's correct, welcome to floating point
if !float_eq_tolerance(a, b, 1e-3) {
    fail test
}
E: Not to mention the fun of deciding "which notion of floating point equality do I use for my tests?" Relative almost-equality? Absolute difference almost-equality? ULP? Exact equality? (That last one isn't a joke there are times where exact float equality is absolutely the right metric to use but you have to know when that is). Also, you're using a method that doesn't suddenly break near zero, right?

And then, if you were dumb enough to write a clone for both widths of float (even if it's templated code), you can probably reuse all the same tests, but you get all the fun of figuring out your tolerances again.

Linear Zoetrope fucked around with this message at 04:02 on Jun 6, 2019

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Floating point is hell, testing won't help, hail Satan.

Dirt Road Junglist
Oct 8, 2010

We will be cruel
And through our cruelty
They will know who we are

ultrafilter posted:

Floating point is hell, testing won't help, hail Satan.

You're right, and this post is giving me anxiety.

Hail Satan.

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane

ultrafilter posted:

Floating point is hell, testing won't help, hail Satan.

It is known.

iospace
Jan 19, 2038


Give me my embedded land. Only work in ints.

I say that as a project of mine uses trig :cry:

Qwertycoatl
Dec 31, 2008

iospace posted:

Give me my embedded land. Only work in ints.

I say that as a project of mine uses trig :cry:

Do you get to enjoy the many wonders of fixed point arithmetic?

Munkeymon
Aug 14, 2003

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



ultrafilter posted:

Floating point is hell, testing won't help, hail JavaScript.

Hail

iospace
Jan 19, 2038


Qwertycoatl posted:

Do you get to enjoy the many wonders of fixed point arithmetic?

I have in the past but floats, when you're actually dealing with baked in functions, work well enough.

CPColin
Sep 9, 2003

Big ol' smile.
I found a servlet that we still use in Production, but also still store in SVN. It was from when my predecessor started, ten years ago.
Java code:
	    // Create the search controls 		
	    SearchControls searchCtls = new SearchControls();
		
	    //Specify the search scope
	    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
 
	    //specify the LDAP search filter
	    String searchFilter = "(&(objectClass=group)(CN=Foo))";
 
	    //Specify the Base for the search
	    String searchBase = "DC=example,DC=com";
:hmmyes:

Guess they hadn't gotten the hang of what makes a good comment yet.

Dirt Road Junglist
Oct 8, 2010

We will be cruel
And through our cruelty
They will know who we are

CPColin posted:

I found a servlet that we still use in Production, but also still store in SVN. It was from when my predecessor started, ten years ago.
Java code:
	    // Create the search controls 		
	    SearchControls searchCtls = new SearchControls();
		
	    //Specify the search scope
	    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
 
	    //specify the LDAP search filter
	    String searchFilter = "(&(objectClass=group)(CN=Foo))";
 
	    //Specify the Base for the search
	    String searchBase = "DC=example,DC=com";
:hmmyes:

Guess they hadn't gotten the hang of what makes a good comment yet.

On the one hand, that's ridiculous. On the other, I'd rather have that guy on my team than someone who calls everything $var1 $var2 etc and has to ask me what characters start a comment.

raminasi
Jan 25, 2005

a last drink with no ice
I found IMiscellaneousService at work today and I can't stop laughing.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
At least with awful redundant comments you can always just delete them all.

Trammel
Dec 31, 2007
.

ultrafilter posted:

Floating point is hell, testing won't help, hail Satan.

What else are you going to use to store currency in your database?

GreeterFactory.newGreeter.greet("Santa")

xtal
Jan 9, 2011

by Fluffdaddy
I store my currencies in VARCHAR such as "100,000.00 USD" or "88.750,00 EUR", which represent the same value.

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.

xtal posted:

I store my currencies in VARCHAR such as "100,000.00 USD" or "88.750,00 EUR", which represent the same value.

It's also best if you use the current locale for the decimal separator when parsing/saving these strings. Hail Satan.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Bruegels Fuckbooks posted:

It's also best if you use the current locale for the decimal separator when parsing/saving these strings. Hail Satan.

For the longest time our codebase called this function something like a gazillion times per second (all number formatting, all calculations, all database i/o on numeric columns...):

code:
Public Function GetLocalDecimalSeparator() As Char

        'extrapolates the local decimal separator in use from the result of the exact division 1 / 2 = 0.5
        Dim separator As Char = CSng(1 / 2).ToString.Substring(1, 1)

        Return separator

    End Function
I always wondered if the JIT was smart enough to cache the result, or if it had to wonder 'what if somebody has changed the local culture since the last execution'

Soricidus
Oct 21, 2010
freedom-hating statist shill

NihilCredo posted:

For the longest time our codebase called this function something like a gazillion times per second (all number formatting, all calculations, all database i/o on numeric columns...):

code:
Public Function GetLocalDecimalSeparator() As Char

        'extrapolates the local decimal separator in use from the result of the exact division 1 / 2 = 0.5
        Dim separator As Char = CSng(1 / 2).ToString.Substring(1, 1)

        Return separator

    End Function
I always wondered if the JIT was smart enough to cache the result, or if it had to wonder 'what if somebody has changed the local culture since the last execution'

jits aren't magic. it's very unlikely it did either of those things. the tostring implementation is probably quite complex, so it probably won't have inlined it, so it probably won't have had any distinction between this input to it and any other input to it, so it probably won't have done anything fancy at all based on this input being constant.

Cuntpunch
Oct 3, 2003

A monkey in a long line of kings
code:
public static void DivideIntoBatches(List<Foo> collection)
{
    const int maxBatchSize = 35;
    var toBig = true;
    var factor = 2;
    
    while(toBig)
    {
        var half = collection.Take(collection.Count / factor).ToArray();
        toBig = half.Length > maxBatchSize;
        factor++;
    }
    
    Log.Error($"Using a factor of {factor} to divide collection which results in {collection.Count / factor} batches");
    
    var taken = 0;
    
    while(taken < collection.Count)
    {
        var toTake = collection.Count / factor > collection.Count - taken ? collection.Count - taken : collection.Count / factor;
        var batch = collection.Skip(taken).Take(toTake).ToArray();
        taken += batch.Length;
        
        _batches.Add(batch);
    }
}
Found this gem of relatively recent legacy code when I saw that line in the logs. The rest is just icing on the cake.

Xarn
Jun 26, 2015
I was just severely tempted to leave the next guy dealing with this code this

C++ code:
((task.get_board(board_idx).is_bored(p)) ? included : excluded) = true;
Then I decided I won't inflict this particular part of C++ syntax on myself. :v:

Beef
Jul 26, 2004
The only horror there is the board/bored homophone.

CPColin
Sep 9, 2003

Big ol' smile.

Cuntpunch posted:

Found this gem of relatively recent legacy code when I saw that line in the logs. The rest is just icing on the cake.

Add a comment that says "to big or not to big? that is the question."

CPColin
Sep 9, 2003

Big ol' smile.
Trying to figure out what database this super old code connects to and fortunately my predecessor put the connection string and credentials right in the source, since they didn't know better at the time. (Time to add another "scrub credentials from source" to the backlog.) Trying to connect to the purported URL of the database times out. The hell's going on here?

I ended up grabbing the .class file from the server and decompiling it to discover that, at some point, my predecessor decided to override the hard-coded database URL in the dumbest way possible. This:
Java code:
    private UserDAO(String url) {
        this.dbUrl = url;
        _con = getConnection(url);
    }
became this:
Java code:
    private UserDAO(String url) {
        this.dbUrl = "(the new connection string)";
        _con = this.getConnection(url);
    }
Note that the code is still passing the original connection string to the getConnection() method. So how is this working at all? Thankfully, there's a verifyConnection() method that does use that dbUrl field. So the first connection attempt times out, then the second attempt uses the right URL and succeeds. Fantastic.

This change was never checked into source control, by the way, nor was the original connection string ever deleted from the code. My predecessor made the change, compiled it, and dropped the .class file right onto the server. This is also the official way to update the hard-coded list of users who are allowed to use this application.

Time to start from scratch!

Edit: Oh, also, _con is static.

sunaurus
Feb 13, 2012

Oh great, another bookah.
that's a good honest coding horror

Edit: what field are you working in?

sunaurus fucked around with this message at 19:07 on Jun 7, 2019

CPColin
Sep 9, 2003

Big ol' smile.
I work on a college campus for an auxiliary company that has to be separate from the rest of the university for reasons I've never had explained to me properly.

The Fool
Oct 16, 2003


CPColin posted:

I work on a college campus for an auxiliary company that has to be separate from the rest of the university for reasons I've never had explained to me properly.

Probably for-profit vs. non-profit shenanigans

CPColin
Sep 9, 2003

Big ol' smile.

The Fool posted:

Probably for-profit vs. non-profit shenanigans

Possibly, but it's a public university, so who knows. I think it's something about budgeting and being able to accept gifts as a "separate" company. I try to dodge having to explain, any time somebody asks where I work. Same thing happened when I worked at Experts Exchange. "No, we don't answer the questions ourselves. Other people do it. No, they don't get paid."

Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!

CPColin posted:

Trying to figure out what database this super old code connects to and fortunately my predecessor put the connection string and credentials right in the source, since they didn't know better at the time. (Time to add another "scrub credentials from source" to the backlog.) Trying to connect to the purported URL of the database times out. The hell's going on here?

I ended up grabbing the .class file from the server and decompiling it to discover that, at some point, my predecessor decided to override the hard-coded database URL in the dumbest way possible. This:
Java code:
    private UserDAO(String url) {
        this.dbUrl = url;
        _con = getConnection(url);
    }
became this:
Java code:
    private UserDAO(String url) {
        this.dbUrl = "(the new connection string)";
        _con = this.getConnection(url);
    }
Note that the code is still passing the original connection string to the getConnection() method. So how is this working at all? Thankfully, there's a verifyConnection() method that does use that dbUrl field. So the first connection attempt times out, then the second attempt uses the right URL and succeeds. Fantastic.

This change was never checked into source control, by the way, nor was the original connection string ever deleted from the code. My predecessor made the change, compiled it, and dropped the .class file right onto the server. This is also the official way to update the hard-coded list of users who are allowed to use this application.

Time to start from scratch!

Edit: Oh, also, _con is static.

This is obviously horrifying but maybe not as horrifying as having the real creds in source control? Maybe?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Going over old pages in our web interface looking for things to mark for cleanup.

code:
<th class="width50"></th>
<th class="width70"></th>
<th class="width75"></th>
<th class="width205"></th>
<th class="width70"></th>
<th class="width80"></th>
<th class="width80"></th>
<th class="width95"></th>
<th class="width70"></th>
<th class="width70"></th>
<th class="width70"></th>
<th class="width70"></th>
I think this one will be next.

CPColin
Sep 9, 2003

Big ol' smile.

Eggnogium posted:

This is obviously horrifying but maybe not as horrifying as having the real creds in source control? Maybe?

I just found a second set of credentials in the same repo, to another database.

Edit: This one is in a JSP file. Yes, a JSP file is manually opening a connection to a database and doing queries on it, with all of the connection parameters hard-coded right in there.

Edit2: This JSP file is supposed to be implementing an autocomplete popup, but it doesn't work because the place that's supposed to use it uses absolute paths and stuff moved sometime in the last ten years. It's a good thing, too, because the queries being run include a clause for restricting results to those a certain user can see. That user is specified as one of the URL parameters, so not the most secure bit of functionality.

CPColin fucked around with this message at 22:38 on Jun 7, 2019

Harik
Sep 9, 2001

From the hard streets of Moscow
First dog to touch the stars


Plaster Town Cop

CPColin posted:

user is specified as one of the URL parameters, so not the most secure bit of functionality.
My favorite form of security, none at all.

Adbot
ADBOT LOVES YOU

Volguus
Mar 3, 2009

CPColin posted:

I just found a second set of credentials in the same repo, to another database.

Edit: This one is in a JSP file. Yes, a JSP file is manually opening a connection to a database and doing queries on it, with all of the connection parameters hard-coded right in there.

Edit2: This JSP file is supposed to be implementing an autocomplete popup, but it doesn't work because the place that's supposed to use it uses absolute paths and stuff moved sometime in the last ten years. It's a good thing, too, because the queries being run include a clause for restricting results to those a certain user can see. That user is specified as one of the URL parameters, so not the most secure bit of functionality.

Hahaha. This reminds me of another JSP page that I had the pleasure of seeing a few years back: It was holding the currently logged in users in a static java.util.Hashtable. 2 people could not log in/browse at the exact same time or the application would crash. That was in addition to its many other issues.

"Can you fix it?" was the question.
"No" was the only possible answer that i could give.

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