Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

02-08-2012 , 07:38 PM
I think it's pretty debatable whether it's a bad practice or not.

Quote:
Originally Posted by Xhad
This didn't occur to me, but it probably is given that the length could hypothetically change during the loop (assigning to indices outside the existing bounds extends the array)
My (admittedly foggy) recollection is that .length is updated just once when the size of the array is increased.

Modifying an array you are looping over is definitely a bad practice though
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 07:43 PM
The biggest offense to my sense of style is that the numbers can't sum themselves.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 07:53 PM
Quote:
Originally Posted by Xhad
This didn't occur to me, but it probably is given that the length could hypothetically change during the loop (assigning to indices outside the existing bounds extends the array)
Changing the size of something (especially increasing it) like a collection when using its Length or Count in a for-loop is a recipe for disaster imo. That loop could end up running until you are out of memory. Decreasing the size I think you run the risk of causing an index out of range exception.

I know its "legal" in C# to for example add elements to a List inside a for-loop but I think its probably a very bad idea
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 08:09 PM
Quote:
Originally Posted by kyleb
Never calculate something n times when 1 times is enough. I can't believe I see that example in for loops in tutorials.
I know in C# the arr.Length calc is done only once. Its actually the case that using: (int i = 0; i < arr.Length; i++) is faster than the alternative in the case of C#
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 08:39 PM
I feel like I must be wrong, otherwise someone would have noticed before now, but I think this is wrong:

Code:
for ( i = 0; i < raceTimes.length; i++ ) {
    var totalTime = (totalTime || 0) + raceTimes[i];
}
totalTime there is scoped within the for loop, and thus doesn't exist outside, isn't it?

Not that I can ever remember stupid scoping rules in javascript
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 08:44 PM
Zurvan: I don't know the answer to that myself but here is something else funny about this whole thing. The codeyear site has this little webform where you can type in your code and run it. Interestingly enough it actually gives a warning about that version of the loop for the exact reason you said, but if you run it it works anyway.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 09:08 PM
Quote:
Originally Posted by Zurvan
I feel like I must be wrong, otherwise someone would have noticed before now, but I think this is wrong:

Code:
for ( i = 0; i < raceTimes.length; i++ ) {
    var totalTime = (totalTime || 0) + raceTimes[i];
}
totalTime there is scoped within the for loop, and thus doesn't exist outside, isn't it?

Not that I can ever remember stupid scoping rules in javascript
I actually noticed that too, but I think js has some weird ass scoping rules;

I just ran this:

Code:
function run_tests(){
    
    for (var i=0; i<35; i++){
	var test = i;
    }
    console.log(test);
}
and the result is 34
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 09:14 PM
I thought about that scope being wrong too lol. I guess javascript doesn't local scope in loops, only in functions / objects?

but I think it's bad to repeatedly use var insidde the loop in the second version, I'm sure I remember a tutorial telling me (re)instancing a variable is expensive compared to using one already defined. maybe that's changed these days though.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 09:17 PM
Now that I think of it, Python also does something similar:

Code:
for i in range(10):
    q = 10
    
print q
will print 10
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 09:26 PM
What I don't understand is why reinstancing the variable isn't resetting it.

I javascript, but **** like this drives me nuts. It's why I'm such a Nazi at work about writing clean JS
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 09:38 PM
Quote:
Originally Posted by Neko
Now that I think of it, Python also does something similar:

Code:
for i in range(10):
    q = 10
    
print q
will print 10
Thinking about this it makes more sense to me than the alternative given that you can assign to variables without declaring them. Otherwise, how would something like this work?

Code:
q = someinitialvalue
for x in somelist:
   q = somefunctionof(q, x)

print q
Interestingly enough the following throws an error in Python 3. I remember reading this isn't the case in Python 2 though!

Code:
[i for i in range(10)]
print(i)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 09:48 PM
Some languages to block scoping (Lexical scoping) while other just have function scoping.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 10:08 PM
Quote:
Originally Posted by Xhad
Interestingly enough the following throws an error in Python 3. I remember reading this isn't the case in Python 2 though!

Code:
[i for i in range(10)]
print(i)
Code:
[tyler@toaster:~]$ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(i)
9
i'm not sure how i feel about this. probably bad.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 10:15 PM
Quote:
Originally Posted by Zurvan
I feel like I must be wrong, otherwise someone would have noticed before now, but I think this is wrong:

Code:
for ( i = 0; i < raceTimes.length; i++ ) {
    var totalTime = (totalTime || 0) + raceTimes[i];
}
totalTime there is scoped within the for loop, and thus doesn't exist outside, isn't it?

Not that I can ever remember stupid scoping rules in javascript
JavaScript doesn't scope within loops, only objects.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 10:15 PM
From what I understand there is some over-my-head reason why they couldn't fit comprehensions into Python 2 without either introducing that quirk or breaking backward compatibility. So Python 2 chose the first option and Python 3 chose the second.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 10:18 PM
relatedly, i don't think i posted this here but i should have because i ****ing love it: https://www.destroyallsoftware.com/talks/wat

(i pronounce "wat" more like "waat" than "waet" but watever.)

my coworker who sent that to me wondered what a wat for python might look like. today he posted this:

Quote:
When you compare objects of different types in Python, and they don't implement the special methods that indicate how they should be compared, Python will still compare them. But it compares them based on the alphabetical orders of their types. So every int is less than every string, because "int" < "str".

I think they did this to get a consistent sort order when a list contains more than one kind of object. Still it's one thing to say "all numbers are less than all strings" and another to say "all numbers are less than all strings BECAUSE the string 'int' is less than the string 'str' and this also means that all floats are less than all ints, all lists are less than all tuples, all dicts are less than all sets."

In Python 3, they took this out and comparing objects of different types raises an exception by default. You can still define the special methods on a class to allow them to be compared, but you have to do it explicitly. (When sorting, you can also provide a key function that generates keys of a single type from whatever objects are in the list.)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 10:47 PM
lol javascript
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-08-2012 , 11:29 PM
Don't think this is quite on the level of weird as that JavaScript stuff, but ime this throws nearly everyone at first. Compare this:

Code:
def ok(n, x=0):
    x += n
    print(x)

ok(1)
ok(2)
To this:

Code:
def wat(n, x = []):
    x.append(n)
    print(x) #or print x for you Python 2 people

wat(1)
wat(2)
Spoiler:
The first prints '1', '2', but the second prints '[1]', '[1, 2]'!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 12:34 AM
Quote:
Originally Posted by Xhad
Spoiler:
the second prints '[1]', '[1, 2]'!
Spoiler:
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 01:30 AM
For amusement value: https://www.destroyallsoftware.com/talks/wat

Talking about various scripting language oddities, javascript high among them.

(Hangs head in shame for grunching the last couple of posts...)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 01:32 AM
OK, idly looked at SO and this one threw me for a loop (no pun intended):

Code:
>>> a, b, c = (lambda: x for x in range(3))
>>> a()
2
>>> b()
2
>>> c()
2
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 01:56 AM
God JS is so great but there is so much stupid **** about it as evidenced in that video.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 04:00 AM
Quote:
Originally Posted by sorrow
For amusement value: https://www.destroyallsoftware.com/talks/wat

Talking about various scripting language oddities, javascript high among them.

(Hangs head in shame for grunching the last couple of posts...)
I feel like it would be hilarious to make someone who doesn't program sit through that and watch their bewilderment at why these nerds are laughing at this **** (obv I found it hilarious too)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 10:07 AM
Quote:
Originally Posted by Xhad
...
Code:
def wat(n, x = []):
    x.append(n)
    print(x) #or print x for you Python 2 people

wat(1)
wat(2)
Spoiler:
The first prints '1', '2', but the second prints '[1]', '[1, 2]'!
This was one of the first things I learned in Python when I started really using it. When the parser creates the method, it initializes the default value and holds on to it. When the default happens to be a reference type, it holds on to the same reference. Still, very Wat!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-09-2012 , 01:08 PM
Quote:
Originally Posted by sorrow
For amusement value: https://www.destroyallsoftware.com/talks/wat

Talking about various scripting language oddities, javascript high among them.

(Hangs head in shame for grunching the last couple of posts...)
Brilliant!!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m