What NOT to do when Java Coding

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5

70 Replies - 63543 Views - Last Post: 30 May 2013 - 04:57 PM

#61 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 188
  • Joined: 22-October 12

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 06:06 AM

Plus, for certain situations ArrayLists are the ONLY solution.

e.g. prime number generator
Was This Post Helpful? 0
  • +
  • -

#62 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 06:46 AM

You've lost me. What part of a prime number generator are ArrayLists essential for?
Was This Post Helpful? 0
  • +
  • -

#63 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 07:38 AM

View Postextremeblueness, on 30 May 2013 - 08:06 AM, said:

Plus, for certain situations ArrayLists are the ONLY solution.


Well, for some applications, ArrayLists are going to be the most convenient tool, but nothing's ever the only solution.
Was This Post Helpful? 1
  • +
  • -

#64 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12135
  • View blog
  • Posts: 45,119
  • Joined: 27-December 08

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 09:22 AM

View Postextremeblueness, on 30 May 2013 - 09:06 AM, said:

Plus, for certain situations ArrayLists are the ONLY solution.

e.g. prime number generator

I generally use a bit sieve implementation, which is array based.
Was This Post Helpful? 0
  • +
  • -

#65 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 09:31 AM

View Postmacosxnerd101, on 30 May 2013 - 11:22 AM, said:

View Postextremeblueness, on 30 May 2013 - 09:06 AM, said:

Plus, for certain situations ArrayLists are the ONLY solution.

e.g. prime number generator

I generally use a bit sieve implementation, which is array based.



Exactly. We might also just note that people have been generating primes with computers for a lot longer than we've had Arraylists.
Was This Post Helpful? 0
  • +
  • -

#66 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 09:43 AM

View Postextremeblueness, on 29 May 2013 - 08:22 PM, said:

6. Use while loops for EVERYTHING.


Please explain this.

When looping I would prefer, in order, foreach, for, while. ( goto is not an option )

Reasoning. A foreach will explicitly assign the variable that is the identity of the loop. A for maintains all the incrementation criteria in one place. A while is the most general purpose and therefore gives least implicit idea of intent.
Was This Post Helpful? 1
  • +
  • -

#67 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1230
  • View blog
  • Posts: 4,016
  • Joined: 12-May 09

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 04:09 PM

View Postbaavgai, on 30 May 2013 - 12:43 PM, said:

Reasoning. A foreach will explicitly assign the variable that is the identity of the loop. A for maintains all the incrementation criteria in one place. A while is the most general purpose and therefore gives least implicit idea of intent.

Agreed. For each loops eliminate entire classes of errors and make the code easier to reason about.
Was This Post Helpful? 0
  • +
  • -

#68 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 188
  • Joined: 22-October 12

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 04:15 PM

View Postbaavgai, on 30 May 2013 - 04:43 PM, said:

View Postextremeblueness, on 29 May 2013 - 08:22 PM, said:

6. Use while loops for EVERYTHING.


Please explain this.

When looping I would prefer, in order, foreach, for, while. ( goto is not an option )

Reasoning. A foreach will explicitly assign the variable that is the identity of the loop. A for maintains all the incrementation criteria in one place. A while is the most general purpose and therefore gives least implicit idea of intent.


Explanation:

While loops are so inefficient that for each and for loops are much, much, much better.
Was This Post Helpful? 0
  • +
  • -

#69 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1230
  • View blog
  • Posts: 4,016
  • Joined: 12-May 09

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 04:19 PM

I know people are giving you the benefit of the doubt, but are you just making things up?
Was This Post Helpful? 1
  • +
  • -

#70 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 04:31 PM

View Postextremeblueness, on 30 May 2013 - 07:15 PM, said:

While loops are so inefficient that for each and for loops are much, much, much better.


First, sorry, I see the while loop thing was part of your anti pattern list. It just stood out to me as odd and kind of threw me.

However, while loops aren't particularly inefficient. Other loops aren't much much better. Rather, other loops isolate the state change of the loop better. It's not about efficiency, just giving bugs one less places to hide. A goto can be exceptionally efficient, they're just known roach motels.

OOP in general is about the trade off between maintainability and efficiency. Spaghetti code can be extremely efficient, but good luck fixing it.
Was This Post Helpful? 2
  • +
  • -

#71 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10625
  • View blog
  • Posts: 18,185
  • Joined: 19-March 11

Re: What NOT to do when Java Coding

Posted 30 May 2013 - 04:57 PM

Example time in the old corral. Perhaps you know that you can see the underlying jvm code using the javap utility?

>> javap -v SomeClass

will echo a human-readable "assembly"-style listing of the generated jvm code. Very educational, this.

So let's see what we get...

Source code:
public class Loops
{

	public static void forLoop(int a){
		for (int i = 0; i < a; i ++) {
			System.out.println("Example of a for loop");
		}
	}
	public static void whileLoop(int a){
		int i = 0;
		while ( i < a) {
			System.out.println("Example of a while loop");
			i++;
	}
	}

}




generated jvm code (stackmap table and linenumber tables omitted, run javap yourself if you want to see them):
public static void forLoop(int);
  Code:
   Stack=2, Locals=2, Args_size=1
   0:	iconst_0
   1:	istore_1
   2:	iload_1
   3:	iload_0
   4:	if_icmpge	21
   7:	getstatic	#2; //Field java/lang/System.out:Ljava/io/PrintStream;
   10:	ldc	#3; //String Example of a for loop
   12:	invokevirtual	#4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   15:	iinc	1, 1
   18:	goto	2
   21:	return


public static void whileLoop(int);
  Code:
   Stack=2, Locals=2, Args_size=1
   0:	iconst_0
   1:	istore_1
   2:	iload_1
   3:	iload_0
   4:	if_icmpge	21
   7:	getstatic	#2; //Field java/lang/System.out:Ljava/io/PrintStream;
   10:	ldc	#5; //String Example of a while loop
   12:	invokevirtual	#4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   15:	iinc	1, 1
   18:	goto	2
   21:	return




A similar example can be constructed for iteration over an array, using a for loop and a foreach loop. I'll leave that one to you.
Are there any other questions?
Was This Post Helpful? 3
  • +
  • -

  • (5 Pages)
  • +
  • « First
  • 3
  • 4
  • 5