Check, check, testing, one, two
-
C# .NET:
if (record.ModifiedList.Length > 0) { for (int i = 0; i < record.ModifiedList.Length; i++) { //do stuff } }
So what language does this make sense in?
-
If there's something in the list, do a for loop to iterate over the list...?
Surely it's protecting against record.ModifiedList being empty before attempting to do an operation on it which would otherwise be dropping an E-bomb?
Filed under: I got no idea what a ModifiedList is. I never saw it before in Unity C#
-
If do stuff uses a function that record(null) throws an exception?
In this scenario, you're guaranteed at least one entry in the list.
There are better ways to do it, but. um.
-
I got no idea what a ModifiedList is.
I assume justIt was confirmed to be an Array that therecord
struct/class exposes.Of course, if "ModifiedList" could be null, even the if check would blow up, so there's no reason for it as-is.
-
ModifiedList
is of typestring[]
Checking for
record
(class) being null orrecord.ModifiedList
being null is one thing, but the conditional here is just plain redundant.
-
No point in the check at all. Probably just some leftover cruft.
-
Why is it a for loop instead of a foreach? Do they use
i
to do something other than grab specific thing inModifiedList
?
-
//do stuff
looks like this:UpdateRecord(UserID, record.ModifiedList[i]);
-
The only reason I can think of is if they need index for something.
-
According to @chubertdev right before you they need it to grab something from the array they are iterating over, so yeah it should be a foreach.
-
Question is, is the second parameter of
UpdateRecord
aref
param?Otherwise I'd have to hope it's just someone that came from something that didn't support foreach and he's relatively new to programming or C#.
-
Maybe premature optimization? ... !
-
Nope, it's passed by value. The
UpdateRecord
method is avoid
.I'm fine with the for loop, although foreach would be better. But they'd work the same, just about. It's the conditional that really gets me.
-
The only reason to do the for over the foreach for sure would be if the function had that as a
ref
parameter, because then the foreach wouldn't put the changed value back in. But since it's not, yeah, either or would work.And I'm sure we already went over the if check being pointless as-is. Either it's redundant or it'll blow up the same as the for would.
-
Question is, is the second parameter of UpdateRecord a ref param?
In C# that would need the
ref
before the parameter when calling it.
-
In C# that would need the ref before the parameter when calling it.
It was probably worth asking, since, as you can see, my obfuscation isn't the greatest.
-
Ah, yes, forgot that. I don't work as much in C# right now, so I forget that aspect. I'm neck-deep into VB.NET at work.
-
At least it's not PHP, right?
-
VB .NET is incredibly sane compared to VB6, VBA, etc.
-
I've never actually done PHP, but by everything I hear, I would prefer to avoid it.
I definitely don't try to avoid VB.NET, since it's more a first-class language since .NET 2.0. Still a bit verbose and messy in some spots, but worlds better than Classic VB.
-
The problem, of course, is some of our clients are still on classic ASP and VBA applications, which we have to help fix/maintain. Guess who gets stuck with that duty?
-
PHP is quite a bit similar to classic VB. Good for beginners, can cobble together a terrifying array of business critical stuff in, and can be used for serious applications if done properly, but oh so rarely done properly.
-
Isn't it SOP to give that to the FNG ASAP?
-
Up until last year, that was me (it's a really small business). Now, there is a "newer" guy, but he doesn't know Classic VB and they actually want something that'll work.
Filed under: [We also bill by the hour.](#tag2)
-
Sounds like the company that I worked for when I started my career.
-
Checking for record (class) being null or record.ModifiedList being null is one thing, but the conditional here is just plain redundant.
Right but it makes sense, both to me and to the C# compiler. So I take issue with your question in the OP.
-
Right but it makes sense, both to me and to the C# compiler. So I take issue with your question in the OP.
.........................wut?
you're alive???
-
.........................wut?
you're alive???
Why would you care? I don't even know who the fuck you are.
-
Why would you care? I don't even know who the fuck you are.
So take your issue and move on.
-
Is there an
else
? This is sane if there's anelse
.
-
Nope, no
else
. The next statement is the same pattern for a different List.
-
You don't know who we are, but we know who you are and we have the greatest respect for your awesomeness.
-
Get a room you two ...
-
I don't think so.
-
Anyways, the question is based on the COBOL response in this thread:
http://what.thedailywtf.com/t/how-not-to-use-a-switch-statement/1590/2/So my question was aimed at figuring out if there was a reason why someone would do this based on the behavior of a different language.
-
I'm just thinking straight defensive coding out of a lack of familiarity with the nuances of the types at hand. I'd probably do it if I didn't know what exactly record was.
-
I can't think of any language or any type for
record
that would make this useful.
-
I shudder to admit it, but PHP conceivably could do so.
Albeit in a sloppy case where you're dealing with record as an array and you haven't actually ensured it exists before trying to iterate over it. Remember in PHP, there's a whole thing of RAII going on, so if you only defined the array previously with $record[] = $item, it's possible $record never gets defined.
Terrible example from a fairly bad language.
-
What would the reference to
record.ModifiedList.length
do? Would it not blow up?
-
I was assuming you'd do !empty() instead of a literal check on length since arrays in PHP aren't implemented as instances of classes (meaning that there is a bunch of functions to operate on arrays, ranging from count, through various pointer manipulations, through to a whole bunch of functions that have array_ as a prefix)
-
Yeah, that's the thing. Trying to think of sane code in another language that would end up creating this code in C# has to change the nature of the code. You wouldn't have the same pseudocode from both languages.
-
In which case I'd have to assume attempted and/or premature optimisation in testing the array is non-empty before trying to iterate over it.
-
I'm just assuming this:
Otherwise I'd have to hope it's just someone that came from something that didn't support foreach and he's relatively new to programming or C#.
-
I vote all of the above.
-
No, they would NOT work the same (for vs. foreach). A "novice" (10 years experience) just went through a codebase and changed many "For" to "ForEach"... Broke things horribly. Apox on you if you don't know why.
-
The scope of that statement is this type of code.
See:
http://what.thedailywtf.com/t/check-check-testing-one-two/1591/14
-
But yeah, there was no need to change it. I removed the conditional, and left the loop as is..
-
Is it you?
-
New hypothesis:
int i = 0
That statement has side effects, so they needed to avoid using it if they didn't absolutely need to.
-
@blakeyrat said:
I don't even know who the fuck you are.
You don't know who we are, but we know who you are and we have the greatest respect for your awesomeness.that word... I don't think it means what you think it means.