Do you use global variables?

it's a bit like goto. everyone bangs on about "don't use got". almost every set of company coding standards i've ever seen forbid them. and fair enough, you never need to use goto. there's always another way. but then, every time you do switch, break, continue, if, else, for, while... you're really just doing a goto. they all compile to some sort of JMP instruction. you can screw up with any of them.

Depends on how you use goto, it can be used in some rare situations like this:

Code:
for (int i=0; i<10; i++) {
  for (int x=0; x<10; x++) {
    if (x = 5) 
      goto end;
  }
} end:

In my opinion is more efficient than:

Code:
for (int i=0; i<10; i++) {
  bool break_outer_loop;

  if (break_outer_loop) 
    break;

  else {
    for (int x=0; x<10; x++) {
      if (x = 5) 
        break_outer_loop = true;
    }
  }
}

I still wouldn’t use goto though because its frowned upon.
 
Sorry DJ_Jestar, I don't follow? Can you elaborate.

Cheers

It's a "shortcut" syntax, rather than an explicit global. Java's JVM will look at a variable and say "So it's not a local/temp variable, it must be a property" and if it still doesn't find it, will throw an exception (or wobbly as dynamic typed developers like to call it :p).

Depends on how you use goto, it can be used in some rare situations like this:

Code:
for (int i=0; i<10; i++) {
  for (int x=0; x<10; x++) {
    if (x = 5) 
      goto end;
  }
} end:

In my opinion is more efficient than:

*snip*

I still wouldn’t use goto though because its frowned upon.
Break bool's are usually a code smell. There are few instances (if any) that I'd use one.
Code:
for (int i = 0; i < 10; i++) {
  for (int x = 0; x < 10; x++) {
    if (x == 5) break;
  }
  if (x == 5) break;
}
would be my preference; ignoring of course that there is no reason to iterate i in the first place :p
 
It's a "shortcut" syntax, rather than an explicit global. Java's JVM will look at a variable and say "So it's not a local/temp variable, it must be a property" and if it still doesn't find it, will throw an exception (or wobbly as dynamic typed developers like to call it :p).

Sorry, still don't get the context (slow day, insufficient coffee) but no worries ;) Think there might be some confusion with the term "property" - I think you're using the term to refer to a member variable above, while C# (and apologies if you already know this) has a Property syntax for exposing properties from an object like this:

Code:
private String _foo;
public String Foo {
  get {  return _foo; }
  set {  _foo = value; }
}

Out of interest, wouldn't the scenario you describe above be caught by the compiler rather than the JVM?
 
JVM.. compiler.. meh. :p

The compiler will, in pseudo, perform this when it reads a variable name:

Code:
if (tempVars.containsKey(theVariableName)) {
    return tempVars.get(theVariableName);
} elseif (instVars.containsKey(theVariableName)) {
    return instVars.get(theVariableName);
} else {
    throw new Wobbly();
}
 
In this example, the property (red) called 'name' has a scope of the whole class. However when the constructor's parameter (megenta) called 'name' is defined, the property (red) is still in scope so the parameter will have priority over the property when used in the constructor.

To allow access to the property (red) in the constructor, the Java keyword 'this' can be used to refer to 'this instance' which allows access to the properties. This means that you can use two variables with the same name from different scopes in the same scope (yellow). This is shortcut in Java which is used when you want to use the same variable name to maintain clarity.

Code:
public class Test
{
	private String [COLOR="Red"]name[/COLOR];

	public Test(String [COLOR="Magenta"]name[/COLOR])
	{
		[COLOR="Yellow"]this.name = name;[/COLOR]
	}
}

Without the keyword, this would be the typical way of doing it:

Code:
public class Test
{
	private String name;

	public Test(String name1)
	{
		name = name1;
	}
}
 
Yep, I'm totally fine with all that, and the mechanics of addressing variables within a particular scope (I tend to follow the convention whereby member variables are decorated with an underscore, rather than having to quality them with "this") - I just wasn't clear on how it applied to the discussion in general ;)

Code:
throw new Wobbly();
Like it :D
 
I usually apply underscores to private variables. I always use this/self/me (depending on language) when accessing them anyway.
 
Yeah, all my member variables are either private or protected with underscores applied. I never use public member variables, instead I use a the C# Property syntax to wrap a private/protected member variable.
 
Code:
for (int i = 0; i < 10; i++) {
  for (int x = 0; x < 10; x++) {
    if (x == 5) break;
  }
  if (x == 5) break;
}
would be my preference; ignoring of course that there is no reason to iterate i in the first place :p

I was meaning in a situation where you had a loop within a loop and you needed to exit the outer loop from within the inner loop based on a condition that could only be checked within the inner loop. My example didn’t show this very well I must admit.

Its not a very common situation anyway.
 
I was meaning in a situation where you had a loop within a loop and you needed to exit the outer loop from within the inner loop based on a condition that could only be checked within the inner loop. My example didn’t show this very well I must admit.

Its not a very common situation anyway.
Quite. :) I'd also be inclined to separate it to it's own method, then simply "return" from the inner loop.
 
Back
Top Bottom