Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Thom Yorke raps
Nov 2, 2004


I just started a co-op (my first job programming), and one thing I see in a lot of the code is the following:
private final int ONE = 1;
private final double FIVE = 5.0;


I always thought that was bad coding; why not just use 5.0 in the code. The only thing I can think of is if you want to be able to modify all references of "FIVE" in one location. However, it probably shouldn't be named "FIVE" if you want to do that, because if you change it to FIVE = 6.0, then the name of the variable isn't really useful anymore.

Is this bad coding practice, or am I just stupid? They also do multiple return statements in methods often, which I thought was another no-no.

Adbot
ADBOT LOVES YOU

Thom Yorke raps
Nov 2, 2004


Hidden Under a Hat posted:

Sorry I hate to quote my own question, but I really would appreciate any insight to this issue, even if it's to say that it doesn't matter if I have one class that is 10,000 lines long. Does that matter or should I really be finding a way to delegate some of the stuff in my main class to another class?

Refactor it, break it into smaller pieces of functionality that make sense. Make sure you have tests so it doesn't break.

Thom Yorke raps
Nov 2, 2004


Ulio posted:

Thanks Jabor, I am working on it right now.

I wrote an exercise application a few days ago. Which asks the user three ints then shows the user the smallest int, the largest, the product, the average and the sum.

I did make it work but would like to know if there is a shorter way to find out which is the smallest and largest this how I did it.

code:
              if(num1 > num2)
		    if(num1 > num3)
		    if(num2 > num3)
		    {
		    	System.out.printf("Largest is %d\n", num1);
		    	System.out.printf("Smallest is %d\n", num3);
		    }
			if(num1 > num2)
			if(num1 > num3)
			if(num3 > num2)
		    {
			    	System.out.printf("Largest is %d\n", num1);
			    	System.out.printf("Smallest is %d\n", num2);
		    }
			  
I did this for each possibility so it was lots of copying and pasting. I just want to know if there is another way(pretty sure there is), but not something too advanced. Thanks.

How do you find the smallest number in a group of numbers? You go through and look at each number, and remember it if it is smaller than the previous smallest number you have found. So something like:
code:
public void printIntInfo(int... numbers) {
  if(numbers.length < 0) {
    System.out.println("Invalid number of arguments, must be at least one number");
    return;
  }
  int smallest = Integer.MAX_VALUE;
  for(int num : numbers ) {
    if( num < smallest ) 
      smallest = num;
    }
  }
}
Same thing for largest. While you are going through all the numbers, you may as well add them up and calculate the rest of the poo poo you need.

The "int... numbers" part of the argument says this method accepts any number of ints as an argument, including zero. It will also accept an array of ints as a valid argument. If there are no ints, we do some error handling.

Thom Yorke raps
Nov 2, 2004


ComptimusPrime posted:

I believe he really meant == 0 or < 1.

yeah. That's why we unit test/code review!

Thom Yorke raps
Nov 2, 2004


gotly posted:

Just to make sure I have it 100% - in this case, MyClass will actually change BUTTS and leave CHEWBACCA alone?

It assigns the value of CHEWBACCA to BUTTS.

Thom Yorke raps
Nov 2, 2004


tripwire posted:

Except for the glaring lack of compile-time immutable collections, and the violating liskov substitution principle thing.

The fact that I have no idea whether a List can be modified is absurd; unsupported operations on interfaces are WTFTerrible. Oh, and one of the Collections has an improperly implemented .equals (pretty sure it is ArrayDeque).

Thom Yorke raps
Nov 2, 2004


personally I prefer the:
String line;
while((line = scanner.nextLine) != null) {
}

syntax.

Thom Yorke raps
Nov 2, 2004


How worried do I have to be about permgen space? I'm working on a large (700,000 lines of code probably?) application. I'm trying to make use of functional programming, so I'm using a number of Function classes to imitate true closures.

Thom Yorke raps
Nov 2, 2004


Do you have a build script that generates the jar? You should. If so why do you care?

Thom Yorke raps
Nov 2, 2004


Harold Ramis Drugs posted:

Would that be using the "(Job)" cast in front of the call? If I do that, the program will compile but break when I try to run it with a Null Pointer Exception. Here's what I did:

code:
					waitJob = (Job)prime.work;
					waitJob.run(100);

It would be waitJob = (Job)prime.data; right? Alternatively you can do ((Job)prime.data).run(100); if you don't want to store it in a variable.

Thom Yorke raps
Nov 2, 2004


Hidden Under a Hat posted:

Thank you! That cleared it right up.

If I'm not actually using the GUI builder "drag-and-drop" aspect of Netbeans anymore, is there a better utility in which to write code?

IntelliJ community edition

Thom Yorke raps
Nov 2, 2004


Safe and Secure! posted:

Let's say that I claimed in an interview that I was a Java wizard, and this set my interviewer off on a mission to ask me increasingly difficult questions about Java to if I was lying. What kinds of questions/topics would I be asked about?

Out of all the languages I've used, I've got by far the most experience and am most comfortable in Java, so I figured that it would be best to focus on improving the area I'm good at if I want to get the best job I can when I graduate this Spring. What knowledge separates good, highly-experienced Java-using software engineers from the rest?
In addition to the things previous posters have mentioned, you should check out ThreadLocal. It is pretty neat.

If I really wanted to stump someone, I would ask them to implement a singleton that is constructed when it is first requested, that has public static members, and does not synchronize on getInstace(). (hint: it's impossible)
But that's only if I was being an rear end in a top hat. In general I don't care if someone knows the minutiae of Java, cause it doesn't mean they can write code worth a crap.

Thermopyle posted:

That all makes me feel pretty good. I don't feel like a Java expert at all (I only taught myself last year for Android development), but I feel like I have an ok understanding of at least half of the things mentioned in the previous few posts. Yay me.

I don't work in anything programming related, but out of curiosity, how much could someone make as a Java dev who understands as much as me?

Understanding poo poo like that is worth roughly $0. Understanding all that stuff and knowing how to properly structure programs, lead product development, and in general get poo poo done would net you a lot of money as a senior lead.

Thom Yorke raps
Nov 2, 2004


Jabor posted:

Nope, it's definitely possible.

Java code:
class Singleton {
	//Other static methods and stuff

	static Singleton getInstance() {
		return LazyHolder.instance;
	}

	private static class LazyHolder {
		static Singleton instance = new Singleton();
	}
}
The inner class lets you access other static members of the Singleton itself, while still using the class loader to ensure that initialization is only performed once, when getInstance() is first called.

:blush: awesome! learned something new

Thom Yorke raps
Nov 2, 2004


I hate Maven.
So, I have a project with a dependency tree that looks like this:
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ cloudmine-javasdk ---
[INFO] com.cloudmine.api:cloudmine-javasdk:jar:0.3-SNAPSHOT
[INFO] +- junit:junit:jar:4.10:compile
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.1:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.4:compile
[INFO] +- commons-io:commons-io:jar:2.3:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.0.0-RC2:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.0.0-RC2:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.0.0-RC2:compile
[INFO] +- joda-time:joda-time:jar:2.1:compile
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.2.1:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.2.1:compile
[INFO] | \- commons-codec:commons-codec:jar:1.6:compile
[INFO] \- commons-logging:commons-logging:jar:1.1.1:compile

and I have another project that depends on that jar
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ coderunner-test ---
[INFO] com.bigcompany.snippets:coderunner-test:jar:0.0.1-SNAPSHOT
[INFO] +- com.cloudmine.api:cloudmine-javasdk:jar:0.3-SNAPSHOT:compile
[INFO] +- com.cloudmine:coderunner:jar:0.0.1-SNAPSHOT:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] \- org.slf4j:slf4j-simple:jar:1.6.4:compile

Notice that according to this javasdk has no transitive dependencies. I wouldn't be surprised if I'm just doing something stupid cause I never use Maven, but does anyone have an idea why it isn't resolving these transitive dependencies? Right now, unless I manually copy over the dependencies, the code fails at run time with a NoClassDefFoundError.
If it matters, cloudmine-javasdk was manually added using mvn install:install-file.

Thom Yorke raps
Nov 2, 2004


Kruegel posted:

I'm pretty sure the pom files tell maven what the dependencies are and since you probably just provided the jar file (and not the pom) for javasdk it has no clue what its dependencies are.

Sedro posted:

Did you run mvn install:install-file off the jar or the pom?
I ran it off the jar, that makes perfect sense, thank you guys!

Adbot
ADBOT LOVES YOU

Thom Yorke raps
Nov 2, 2004


Kilson posted:

code:
DateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-5"));
System.out.println(df.format(new Date()));
With the above, I get '11/20/12 2:14 AM'
When I use "GMT+5", '11/19/12 4:14 PM'
Actual GMT time is 11/19/12 9:14 PM

Why does this seem to do the GMT offset backwards?

So apparently this is some sort of weird POSIX thing:


Does anyone have any recommendations on how to let an end-user specify what timezone they are in, without presenting them the entire list of ~600 choices? The JVM seems to support some simple abbreviations like CST/EST/etc., but I don't think those handle DST. JodaTime doesn't support those abbreviations at all.
Java dates are pretty terrible, I would use JodaTime instead for pretty much anything involving dates and Java

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply