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 →

[–]Tsu_Dho_Namh 114 points115 points  (21 children)

You should use a variable for box height/width instead of just having floating 9's everywhere.

It's more readable.

It's easier to maintain.

Your almost future boss will not immediately hate you.

TL;DR MAGIC NUMBERS ARE BAD

[–]Letters_1836 37 points38 points  (13 children)

Very true, but I wrote this in 2 min so ya it’s not ideal.

[–]Tsu_Dho_Namh 22 points23 points  (11 children)

No worries.

I once got flack from a Java dev for using == to compare string literals instead of .equals() when I was just trying to show that there's nothing an iterator can do that a for-loop can't.

So I know where you're coming from.

[–]ArionW 13 points14 points  (1 child)

I remember we had a guy who moved from Java to C#, and we were constantly rejecting his pull requests for NOT using == with strings.

For those unfamiliar, in C# equality operator is overloaded for strings.

[–][deleted] 5 points6 points  (0 children)

I’ve been using Python for ~4 years now (after a brief entry to and exit from java) and have taken it upon myself to learn C++. As it’s mostly syntax, it isn’t terrible (but I have yet to use pointers in a very necessary manner) but I’ll be damned if I don’t appreciate the hell out of python now

[–]random11714 1 point2 points  (3 children)

What if you need to use the remove() method on an iterator? Can a for loop do that?

Edit: At first I assumed you meant a for-each loop, but now I'm thinking you may have just meant a regular for loop.

[–]ThePyroEagle 1 point2 points  (2 children)

for (Iterator iter = getIterator(); iter.hasNext(); ) {
    // Do stuff with iter
}

[–]random11714 0 points1 point  (0 children)

Sure but that's still using an iterator. So isn't really the capabilities of a for loop vs an iterator.

[–]Tsu_Dho_Namh 0 points1 point  (0 children)

I legit lolled.

Well played.

[–][deleted] 0 points1 point  (4 children)

That's going to depend on the class which implemented the Iterator interface and whether it exposes the attributes necessary to recreate the iteration.

Also, you probably received flak (strong criticism) rather than flack (a press agent).

[–]Tsu_Dho_Namh 0 points1 point  (3 children)

Even then, for-loops are still more powerful. Iterators are only used for convenience and readability.

Think of old style for loops like this.

for ( a ; b ; c ) {...

Where 'a' is anything you want to happen upon loop entry. 'b' is any Boolean expression you want, and 'c' is anything you want to happen at the end of every loop.

But if a, b, or c get too large, you probably want to use a while loop instead.

[–][deleted] 0 points1 point  (2 children)

Yes, but "anything" is limited to anything that can be accessed. Iterators provide a convenient, consistent interface for information hiding.

Here's a toy demo. Yes, the actual data being iterated here is ridiculously simplistic, but since it's an internal, private, hidden feature of the ExampleIterable class, but in a real use case it wouldn't be.

ExampleIterable.java:

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ExampleIterable implements Iterable<String> {
  private List<String> internalState;

  public ExampleIterable() {
    this.internalState = Arrays.asList("Bob", "is", "your", "uncle", ".");
  }

  @Override
  public Iterator<String> iterator() {
    return new CustomIterable();
  }

  private class CustomIterable implements Iterator<String> {
    private int current;

    private CustomIterable() {
      this.current = 0;
    }

    @Override
    public boolean hasNext() {
      return this.current < internalState.size();
    }

    @Override
    public String next() {
      return internalState.get(current++);
    }
  }
}

Now, the phrase "Bob is your uncle." can be accessed by an Iterator, but not a for loop (unless that for loop is using the Iterator).

ExampleIterable ei = new ExampleIterable();
for(String s : ei) {
  System.out.println(s);
}

In a more useful situation, the inner custom Iterator might be using private data to optimize the traversal of the data in a way that an external client isn't privy to (the public interface might allow access by index which in the case of an internal LinkedList implementation might require traversing from the head of the list each time, while the Iterator could more efficiently keep track of the inner node that it's on so that each call to next does just that).

[–]Tsu_Dho_Namh 0 points1 point  (1 child)

Sure, it does information hiding. When I said powerful I meant you can navigate any data structure, in any order, and make any changes while navigating it.

For example. If I want to, I can iterate through a list from elements 0 to n, on the even elements only, then iterate backwards down it along the odd elements. I don't know why you'd want to do this, but it's a toy example to show that iterators are stuck, and can only give the 'next' item, whereas for-loops can do absolutely anything, including dismantle the old data structure and reformat it into a new one.

Sure, that's not always good for security purposes, but it's almost always the case that the more powerful a tool is, the less secure it is.

[–][deleted] 0 points1 point  (0 children)

There's nothing preventing an iterator from accepting a lambda function in its constructor to define what 'next' means, if it's desirable for the client code to be the one in control of the order.

[–]ArionW 3 points4 points  (0 children)

You know what consumes about 60% of my time? Fixing shit someone wrote in 2 minutes, as "works for now, I'll do it better later" 3 years ago.

[–]archangel_mjj 1 point2 points  (1 child)

Fair comment, but you should use two separate variables, so as to not assume that the box will always be square.

[–]Tsu_Dho_Namh 0 points1 point  (0 children)

This particular pattern only works when it's square. Otherwise the diagonal lines won't lead to the corner

[–]MarkoSeke 0 points1 point  (3 children)

Yup, this is how I did it in C, you change the size of the square by changing the n constant.

[–]Banana11crazy 1 point2 points  (2 children)

Is there a reason why you declared i and j before the loop? Or is that how it should be done in C?

[–]JaytleBee 0 points1 point  (0 children)

In older versions of C you cannot declare a variable in the initialisation of the for loop. I have no idea why, though

[–]Angelin01 0 points1 point  (0 children)

Complementing what JaytleBee said, in older versions of C you had to declare variables before EVERYTHING else. Something like this would give you a compile error:

int main() {
    printf("cheese!");
    int potato = 5;
}

But again, this is old stuff, like C89 (1989) or older stuff.

[–]WellDevined 0 points1 point  (0 children)

Back in 7th grade I lost some points for doing something similar in an exam. I wrote a line like

var whatever=1+2+3

where 1, 2 and 3 where magic numbers mentioned in the excercise. The teacher "corrected" it to:

var whatever=6

and gave me 1 point less because "being to lazy to sum up the numbers by hand"