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

bitprophet posted:

So what, you say? Sure, that's no different at this level. But what if, instead of the file not existing, it does exist, but you don't have the right permissions? Does your file_exists() function test for that? Does it make sure the "file" isn't actually a directory? What about a broken symlink? A device file? Etc.

Generally, exception classes cover a much broader swathe of potential problems, which is one reason why catching them after the fact can be more useful than attempting to forecast what might go wrong beforehand.

Tell me, how does this distinguish file does not exist, file is a directory, file is not readable and (my personal favorite) EAGAIN.

Python really struggles to carry some things to completion.

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Zombywuf posted:

Tell me, how does this distinguish file does not exist, file is a directory, file is not readable and (my personal favorite) EAGAIN.

The point is, you don't really need to distinguish them. In most cases, your resolution will be "specify a valid file, dummy".

The real problem is that you need to address all of them somewhere, so you can either explicitly check for them before opening the file (and then duplicate it in the exception logic in case it changes between the check and the actual access), or you can just try it and if it doesn't work, figure out how to deal with it then.

Actually, the parenthetical statement is another important part. You have to write that exception logic anyway. Why not just skip the initial checks and deal with anything that goes wrong, when it actually goes wrong?

baquerd
Jul 2, 2007

by FactsAreUseless

Jabor posted:

The point is, you don't really need to distinguish them. In most cases, your resolution will be "specify a valid file, dummy".

The real problem is that you need to address all of them somewhere

Well, hopefully instead of laying out everything that can go wrong with a file you can use existing functionality and find a library function that does the work for you.

Also, your exception advice is OK in Python but makes my Java eyes cringe.

Zombywuf
Mar 29, 2008

Jabor posted:

The point is, you don't really need to distinguish them. In most cases, your resolution will be "specify a valid file, dummy".

I'm going to take it from that that you don't know what EAGAIN means.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Zombywuf posted:

I'm going to take it from that that you don't know what EAGAIN means.

I'm going to take it from that that you don't know what "most" means.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

I'm not a Pythonista by any account so don't take this as a defence of the language, but checking for EAGAIN can be rolled quite nicely into the IOError catch block, since errno is a field in the exception object you'll get passed. I think part of this argument stems from bitprophet being slightly off the mark when he had "print "No file :-("" in his catch block, since really all you know at that stage is that something went wrong, not that the file is missing (but, that's what e.errno is for!)

Destroyenator
Dec 27, 2004

Don't ask me lady, I live in beer

JediGandalf posted:

code:
/*
 * I'm leaving this block of fail for everyone to gaze upon and realize you do NOT USE try/catch for logic flow.
 *
 * try
 * {
 *   lstCategories.SelectedValue = lds.CategoryID.ToString();
 * }
 * catch (Exception)
 * {
 *   lstCategories.SelectedValue = "-1";
 * }
 */
FAIL COUNT: 1

Using a try/catch block for logic flow. "lds" (no, doesn't mean Latter Day Saint) is the only nullable object in question. CategoryID is an Int32. Basically if lds throws a NullReferenceException it will use -1 for a selected value.

Both of these made it into production.
I would have thought this was for "SelectedValue does not exist in DropDownList" exceptions from old/bad/inconsistent CategoryIDs. Any category ID it loads that doesn't exist in the DropDownList defaults to the safe -1 "(Please select)" option or whatever. Same with the above code with the empty catch for using the default DropDownList value.

You can check for those too but it's not just NullReferenceExceptions that are handled by that block.

Zombywuf
Mar 29, 2008

Jabor posted:

I'm going to take it from that that you don't know what "most" means.

I've seen more EAGAIN IOErrors in production than all other kinds of IOError combined.

litghost
May 26, 2004
Builder

Zombywuf posted:

I've seen more EAGAIN IOErrors in production than all other kinds of IOError combined.

On what kind of application? I mostly see file missing, file already exists, and file permission. What kind of system are you on where EAGAIN is the most common IOError. Or are those others already handled, and you are talking about the remaining slice of errors?

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?
What is this ubiquitous EAGAIN error of which you all speak?

Vanadium
Jan 8, 2005

It is when you do a syscall and the syscall is happily doing its thing and nothing is going wrong and then it suddenly returns -1 anyway because you received a signal or something dumb, and then you have to just do the same thing again and hope you do not get a syscall this time. Or a timeout, or whatever.

Zombywuf
Mar 29, 2008

litghost posted:

On what kind of application? I mostly see file missing, file already exists, and file permission. What kind of system are you on where EAGAIN is the most common IOError. Or are those others already handled, and you are talking about the remaining slice of errors?

Web, with many simultaneous processes.

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

JediGandalf posted:

What is this ubiquitous EAGAIN error of which you all speak?

:google::hf::manpage:

Fly
Nov 3, 2002

moral compass
Sometimes job candidates give weird answers, but why someone would do this escapes me. It's like he's trying to write himself a minivan:

code:
/**
 * Compares o1 and o2.
 * @return -1 if o1's first name, middle name are lexically less
 * than o2's first name, middle name, 0 if o1's first name 
 * and middle name are lexically the same as o2's,
 * and 1 otherwise.
 */
public int compare(Object o1, Object o2) {
    // Write your code here. 

	
	result = -1;

	if (o1 != null && o1 instanceof Person &&
		o2 != null && o2 instanceof Person)
	{
		String o1_name = “”;
		String o2_name = “”;
		String o1_firstname = ((Person)o1).getFirstName();
		String o1_middlename = ((Person)o1).getMiddleName();
		String o2_firstname = ((Person)o2).getFirstName();
		String o2_middlename = ((Person)o2).getMiddleName();

		// first name
		if (o1_firstname != null &&
 			o1_firstname.trim().length > 0)
		{
			o1_name = o1_name.concat(o1_firstname);
		}
		if (o2_firstname != null && 
			o2_firstname.trim().length > 0)
		{
			o2_name = o2_name.concat(o2_firstname);
		}

		o1_name = o1_name.concat(“ “);
		o2_name = o2_name.concat(“ “);

		// middle name
		if (o1_middlename != null &&
 			o1_middlename.trim().length > 0)
		{
			o1_name = o1_name.concat(o1_middlename);
		}
		if (o2_middlename != null && 
			o2_middlename.trim().length > 0)
		{
			o2_name = o2_name.concat(o2_middlename);
		}

		// compare
		If (o1_name.equals(o2_name))
		{
			result = 0;
		}
		Else
		{
			int i = 0;
			Boolean stringIsEqual = true;
     while (i < o1_name.length()
					&& i < o2_name.length())
				{
					char compareChar1 = o1_name.charAt(i);
					char compareChar2 = o2_name.charAt(i);
					if (compareChar1 == compareChar2)
					{
						i++;
						if (i >= o1_name.length() || i >= o2_name.length())
						{
							result = (o1_name.length() < o2_name.length()) ? -1 : 1;
							stringIsEqual = false;
							break;
						}
					}
					else
					{
						result = (compareChar1 < compareChar2) ? -1 : 1;
						stringIsEqual = false;
						break;
					}
				}
				if (stringIsEqual)
				{
					result = 0;
			 }
		}
	}

	return result;
}

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

FinkieMcGee
May 23, 2001

Look, I have to go identify our dead father's body. I'm sorry you're having a bad drug experience, but deal with it.
Just found out in one of our applications database credentials are being passed via a querystring through an iframe.

spiritual bypass
Feb 19, 2008

Grimey Drawer
I bet you just can't wait to see what happens on the backend!

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

FinkieMcGee posted:

Just found out in one of our applications database credentials are being passed via a querystring through an iframe.

You mean the user enters credentials and they're submitted this way, or...?

Argue
Sep 29, 2005

I represent the Philippines
Okay, so our client uses a proprietary (ie: made up on the spot) internal xml format for exchanging data between its many many servers. This xml format contains a list, and each item in this list is a complex data type. You'd think at the very least the list items would look something like:
code:
<item index="1"></item>
<item index="2"></item>
...
Right? Noooo. Instead we have:
code:
<ITEM0></ITEM0>
<ITEM1></ITEM1>
...
I also took a look at their xml validator class. All it does is check for the existence of a start tag, and the existence of an end tag. Naturally, spaces screw it up.

That should have been enough of a red flag, but I hoped they would represent the contents of each item in a saner format. Instead we got:
code:
<ITEM0>|123456|USD|data|moreData|blahblah|</ITEM0>
They're just making up the goddamn format as they go along anyway so there's absolutely no excuse for using pipe-delimited strings instead of xml; we can't even determine what those values represent without referring to the accompanying hundred page document detailing what each of those fields is.

After much begging, we finally got access to the code that builds these pipe-delimited strings. Apparently, these pipe-delimited strings are built by a stored proc in the database. The code retrieves this already pipe-delimited string from the db, PARSES OUT EACH ELEMENT, THEN RE-CONCATENATES THEM and returns the resulting string, which is of course exactly the same.

Zombywuf
Mar 29, 2008

Argue posted:

That should have been enough of a red flag, but I hoped they would represent the contents of each item in a saner format. Instead we got:
code:
<ITEM0>|123456|USD|data|moreData|blahblah|</ITEM0>
They're just making up the goddamn format as they go along anyway so there's absolutely no excuse for using pipe-delimited strings instead of xml; we can't even determine what those values represent without referring to the accompanying hundred page document detailing what each of those fields is.

Are you working with airlines by any chance?

Deep Dish Fuckfest
Sep 6, 2006

Advanced
Computer Touching


Toilet Rascal

Argue posted:

After much begging, we finally got access to the code that builds these pipe-delimited strings. Apparently, these pipe-delimited strings are built by a stored proc in the database. The code retrieves this already pipe-delimited string from the db, PARSES OUT EACH ELEMENT, THEN RE-CONCATENATES THEM and returns the resulting string, which is of course exactly the same.

If I were to build a factory where one of the assembly lines took objects apart and rebuilt them exactly the same, I'd probably be considered mad and sent to an asylum.

Somehow the same does not apply to software engineering.

spiritual bypass
Feb 19, 2008

Grimey Drawer
Unless you tell your PHB that it's recycling and it saves money

Fly
Nov 3, 2002

moral compass

YeOldeButchere posted:

If I were to build a factory where one of the assembly lines took objects apart and rebuilt them exactly the same, I'd probably be considered mad and sent to an asylum.

Somehow the same does not apply to software engineering.

Perhaps the intent was to validate the parts before rebuilding the string?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."

Fly posted:

Perhaps the intent was to validate the parts before rebuilding the string?

But if you break a string apart and validate the pieces, why would you need to reassemble the string? You just friggin' had it!

mobby_6kl
Aug 9, 2009

by Fluffdaddy
Maybe they were removing some invalid characters from one or more sections in the process?

Ugh, I feel bad for trying to justify what is almost certainly a horrible, pointless trainwreck.

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy
The real horror is that you KNOW that at some point it has the information in discrete enough components to emit well-formed XML. In fact, it may have done that at one point in its lifetime.

Habnabit
Dec 30, 2007

lift your skinny fists like
antennas in germany.

Vanadium posted:

It is when you do a syscall and the syscall is happily doing its thing and nothing is going wrong and then it suddenly returns -1 anyway because you received a signal or something dumb, and then you have to just do the same thing again and hope you do not get a syscall this time. Or a timeout, or whatever.

A little late, but. It's EINTR that's returned when a blocking syscall is interrupted by a signal. EAGAIN means a whole plethora of different things. Frequently it seems to overlap with EWOULDBLOCK.

e: I would guess it stands for 'try again'.

Habnabit fucked around with this message at 06:11 on Sep 16, 2010

FinkieMcGee
May 23, 2001

Look, I have to go identify our dead father's body. I'm sorry you're having a bad drug experience, but deal with it.

Hammerite posted:

You mean the user enters credentials and they're submitted this way, or...?

No like Database name (admittedly you need to be in the VPN to use this info), DB User, DB Password.

Hibame
Feb 20, 2008
Someone just committed code to the repository that errors at run time. They wanted some data that was stored in XML and instead of getting it from the location where it is already loaded into memory they wrote their own loading functions. Now this loader instead of using the application settings to get the path of the files has it hard coded. They needed data from two different types of XML. The first function is called ProcessXML, the second function is called ProceesXML. :argh:

When I ask them if they knew they committed bad code.
:btroll: "oh someone must have changed it."
Like hell, I see right there in the log you added your 500 lines where there was nothing and no one else has committed yet.

When asked if they are going to fix it.
:btroll: "I don't know, it works for me."

Edit: Best part, they are the lead on this project.

Hibame fucked around with this message at 22:10 on Sep 19, 2010

POKEMAN SAM
Jul 8, 2004

Hibame posted:

When asked if they are going to fix it.
:btroll: "I don't know, it works for me."

Whenever we submit something that breaks on someone else's machine we always pull this out :D

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.
That is glorious.

spinflip
Sep 11, 2001

O_o Helo U

Ugg boots posted:

Whenever we submit something that breaks on someone else's machine we always pull this out :D



Fly
Nov 3, 2002

moral compass

Ugg boots posted:


A keeper.

nbv4
Aug 21, 2002

by Duchess Gummybuns
code:
user_images = [{
        'id': int(images_id[o[1][0]]),
        'url': images[o[1][0]],
        'order': int(o[1][1])-1,
        'order_orig': o[1][0]
    } for o in enumerate(sorted(enumerate(images_order), key=lambda order: int(order[1]))) if int(o[1][1]) > 0
]

king_kilr
May 25, 2007
It's amazing... not once does he actually use o[0].

Chairman Steve
Mar 9, 2007
Whiter than sour cream
I've seen at least some terrible languages where 1 is the starting index of an array, not 0.

fritz
Jul 26, 2003

Chairman Steve posted:

I've seen at least some terrible languages where 1 is the starting index of an array, not 0.

i think at one point the numerical recipes in c books had a thing where all the array indices went through some kind of macro or something that offset everything by one because the original code was all in fortran

Vaginal Engineer
Jan 23, 2007

Chairman Steve posted:

I've seen at least some terrible languages where 1 is the starting index of an array, not 0.

I don't know how terrible they are, but this is the standard for a number of mathematical languages inlcuding Maple and Magma.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Some languages like Pascal let you define start and end indices for arrays as whatever numbers you want.

Adbot
ADBOT LOVES YOU

king_kilr
May 25, 2007

Vaginal Engineer posted:

I don't know how terrible they are, but this is the standard for a number of mathematical languages inlcuding Maple and Magma.

lua's another one. http://userweb.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

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