• Post Reply Bookmark Topic Watch Topic
  • New Topic

coding style/rules question  RSS feed

 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ..
Im following a tutorial that most of example on it are complex to me.for instance,for showing shuffle method it declare 3 class and every class has some method with passing List<T>,Collections etc...
today I wrote this code without following tutorial rules :


All I have wanna to know is,does my code bad ?I think varible declaring in main scope is not good..is it right ?
decalring variable and method in main is better or declaring them in separate classes.
and as I said the purpose was using shuffle method from Collections class.
thanks
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you start by checking the indentation. Look at lines 5‑6. Make sure to indent things correctly because you can confuse yourself with wrongly‑indented code.
You shouldn't have so much code in the main method, no. In fact you shouldn't have any “real” code there. Look at our FAQ.
If you are using the main method, why do you have the field at all? And the field shou‍ld have private access.
Your spacing is also inconsistent; you sometimes have spaces around binary operators and {} and sometimes don't. Spaces around { and } please except in array initialisers. Have a look at our formatting suggestions.
Where does 51 come from? You shou‍ld avoid magic numbers like that. Also in a for loop, always use the standard format until you need to change it:-
for (int i = 0; i < myArray.length; i++) ...
Not <= because you can get yourself confused. You don't need the >= parts in lines 23 and 25.
When you get to line 21, what order is the array in?
 
Saloon Keeper
Posts: 3331
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your indentation is way off. It should look something like this
 
Carey Brown
Saloon Keeper
Posts: 3331
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that you are shuffling "list" but printing from "arr".
 
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many of the newer IDEs have support for http://editorconfig.org/ rule files for automatic code formating.
I know that IntelliJ, Eclipse (via plugin), MS Visual Studio Code and MS Visual Studio 2017 support these rules files. If you do use a IDE that supports formatting rules then usually you can have the IDE format your code after you type it in. Not that you shouldn't format your code as you type, but sometimes you don't get that option (e.g. copy and paste from a web site.)
On the other hand if you are using something like Notepad or some other non IDE then you may have to format you code manually.

There are reasons to use IDEs and there are reasons to not use IDEs. There are also reasons why people like one IDE over a different IDE and sometimes the company dictates which IDE you have to use at work or for work projects. However this is something that has been and will probably be discussed at length in other posts/sites.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:. . . if you are using something like Notepad . . .
Not NotePad, I hope, which is dreadful for writing code. Try NotePad2 or NotePad++ if you use Windows. Much better products.
discussed at length in other posts/sites.
Here, for example.
 
Greg Sully
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replys
oh my bad ^^ yeee I know about indentation and space as you mentioned.
my most problem was using varible on main method,Im sure that it is bad idea now
Where does 51 come from?

when I set to 52 I got and exception error so I had to insert 51 ,I noticed that i = 0
When you get to line 21, what order is the array in?

the order is random cause of shuffle.the point of this section(tutorial) was using shuffle method .so I used this way instead of declaring many classes and methods >.<

 
Greg Sully
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry I used code tag 
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Sully wrote:. . . when I set to 52 I got and exception . . .
That is caused by writing the for loop wrongly with <= rather than <
Avoid number literals. If you have an array which you are dealing from, use i < myArray.length rather than the number.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Sully wrote:. . .
When you get to line 21, what order is the array in?

the order is random cause of shuffle. . . .
Are you sure the array has been randomised? Try System.out.println(Arrays.toString(array));

Find yourself a copy of Java Puzzlers by Joshua Bloch and Neal Gafter. I Haven't got my copy to hand at present. There is a puzzle there about Collections#shuffle and they tell you two things: the potential complexity of a pack of cards (52! different combinations) overwhelms the capacity of randomisation in the Java® API, and Collections#shuffle is still the best way to shuffle a List. There is more discussion about shuffling in this old thread, and more details about the book.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!