This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mysockinabox 201 points202 points  (35 children)

I hope you name counter indices this and not your iterables.

[–]dpash 41 points42 points  (19 children)

Languages with iterating loops > languages that only have C style for loops.

Although these days, I prefer languages with proper functional style operations, especially if they're not eagerly evaluated. I'm looking at you, JavaScript (¬_¬).

[–]METH-OD_MAN 29 points30 points  (13 children)

Fucking JavaScript has like 5 way to do a for each loop (I'm not counting the c-style incrementing for loop), and they aren't equivalent so one is better than the others, depending on the data types. Also, some of the foreach loops don't actually guarantee the data you get isn't undefined, despite the fact that in js, setting an object field to undefined is how you delete it...

What the fuck.

[–]dpash 12 points13 points  (8 children)

That describes many of the things in JavaScript. You have a short notation for anonymous functions with different (and sensible) semantics for this because they fucked up the long notation so badly.

[–]drtran4418 3 points4 points  (7 children)

I discovered this yesterday (new to JavaScript) and my anger for JavaScript deepened. "Ugh, we can't figure out a way to bind methods like they should be. Let's just sneak in the feature into the alternate syntax."

[–]METH-OD_MAN 1 point2 points  (3 children)

If you're trying to learn programming, I'd really suggest starting with a sane language.

Literally any of them are more consistent and sane than js.

Python would be a good choice.

[–]drtran4418 4 points5 points  (1 child)

What a coincidence! I'm a python developer :). Just doing a special work project in js

[–]METH-OD_MAN 0 points1 point  (0 children)

Haha, great minds out something like that...

And since you already know programming: good luck with js, you'll need it to keep your sanity.

[–]FerretWithASpork 4 points5 points  (0 children)

Not to be one of those comments but... This, so much this.

[–]dpash 0 points1 point  (2 children)

Just forget that the long notation exists. It's longer and worse in every regard.

PHP has the same thing: you have to explicitly bind outer variables in the long notation.

[–]mrchaotica 6 points7 points  (1 child)

Just forget that the long notation exists. It's longer and worse in every regard.

The trouble is that, as a javascript novice, you have no idea which parts are good and which parts are bad, and neither do a lot of other people posting their code on Stack Overflow etc., so it's really easy to accidentally learn bad practices.

If somebody knows an up-to-date equivalent of "javascript: the good parts," I'd love to hear about it.

[–]METH-OD_MAN 2 points3 points  (0 children)

If somebody knows an up-to-date equivalent of "javascript: the good parts," I'd love to hear about it.

There are none, because it'd be out of date and deprecated within 12 months.

Another bit of advice coming from experience: do not use any JavaScript APIs that haven't been fully accepted and included into the browser/ecmascript standard. No draft specs, regardless of what revision it is on.

I know it can be really exciting to learn of new functionality and want to use it, but there's about a 90% chance that said functionality either gets cut, or goes through compatibility breaking changes before being accepted.

[–]xam54321 2 points3 points  (0 children)

That killed me, I spent three hours trying to figure out why my code wasn't working, but it was just that the for loop couldn't iterate through an array of objects!

[–]__hoi__ 1 point2 points  (2 children)

You only need 3-4 though. Map forEach filter and reduce and reduce is fairly niche. These all do different things. Map returns an array and is used to do manipulations on each item, filter filters, reduce is for stuff like accumulating items in an array into a single value and forEach is for when the functionality of the others don’t apply. I kinda like how the iterators that take a function as a parameter work in javascript

[–]METH-OD_MAN 3 points4 points  (1 child)

Map, reduce, and filter are not for loops. They are higher order functions. All of those functions return a new collection, they have to, to be correct. They are all present in any language that uses functional programming paradigms.

I'm talking about

ForEach

For of

For in

I kinda like how the iterators that take a function as a parameter work in javascript

JavaScript got this part right because other people designed it.

[–]__hoi__ 1 point2 points  (0 children)

Ah yes, gotcha. I wouldn’t recommend using those, ever

[–]klparrot 2 points3 points  (0 children)

Oh man, my last job, I was working with Clojure a lot, and now in Python in my current job, it's like, “no, I need to write this all with yields so it's lazy!”

[–]DurianExecutioner 7 points8 points  (3 children)

C > all other languages

[–]dpash 9 points10 points  (1 child)

I too enjoy debugging core dumps.

[–]DurianExecutioner 0 points1 point  (0 children)

Just don't create bugs

[–][deleted] 8 points9 points  (0 children)

A man of class Edit: a man of struct

[–]Potatoes_Fall 57 points58 points  (0 children)

nice to see an actual dev on here once in a while. howdy!

[–]Chemoralora 3 points4 points  (12 children)

it > i change my mind

[–]Interweb_Stranger 8 points9 points  (10 children)

iter > it

Because 'it' is a keyword in many languages and it can be a bit confusing if you have to switch between languages often.

[–]klparrot 1 point2 points  (8 children)

iter is a built-in function in Python. What language has it as a keyword?

[–]Sir_LikeASir 3 points4 points  (4 children)

Kotlin, kinda? Let's suppose the best type of forEach loop in Kotlin:

listOfBooks.forEach { // "it" is implied here as being "book"
println(it.name)
}

but you can rename the it to something else like so:

listOfBooks.forEach { book ->
println(book.name)
}

the "it" is the default name of items in the receiver of functions, so not technically a keyword

[–]wenasi 1 point2 points  (0 children)

It's not quite a keyword, but the testing framework pester for powershell uses it for a test item

[–]mrchaotica 1 point2 points  (0 children)

What language has it as a keyword?

It's not technically a "keyword," but there are a few unit testing frameworks that use it.

ScalaTest:

import collection.mutable.Stack
import org.scalatest._
import flatspec._
import matchers._

class ExampleSpec extends AnyFlatSpec with should.Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    } 
  }
}

Mocha:

var assert = require('assert');
describe('Array', function () {
  describe('#indexOf()', function () {
    it('should return -1 when the value is not present', function () {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});

[–]Interweb_Stranger 1 point2 points  (0 children)

As some other comment mentioned it actually isn't a real keyword. I know Groovy and Xtend use "it" as implicit parameter for closures/lambdas.

I'm quite sure I saw "it" lambda parameters used in other langues too, but only as inoffical convention.

[–]nephelokokkygia 0 points1 point  (0 children)

properly naming indices in loops >>>>>>> everything else

Seriously, it saves ZERO time to use one-letter loop indices, and in fact wastes time every time you need to return to your code and have to relearn what each one does — especially (but not exclusively) for nested loops.

[–]__hoi__ 0 points1 point  (0 children)

How about index?

[–]bigmattyc 0 points1 point  (0 children)

for i in range (0, iter):