Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keeping java code clean

 
Hozi Surat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

One of the biggest issue I have when coding Android apps is keeping my java code clean. Does your book explain good techniques for keeping you code organized?

Thanks.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by that? Can you give an example of how your code differs when targeting Android compared to other platforms?
 
Hozi Surat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I haven't done much coding beyond Android development. I have learned a lot from many of the forums along with the book Big Nerd Ranch. All of the examples I have looked at have their code organized differently and I get confused as to whats the best or correct way. For example I see a lot developers write:

btnA = (Button)findViewById(R.id.btnA);

btnB = (Button)findViewById(R.id.btnB);

.
.
.
then add their onClickListener at the bottom of their code.




I also then see the opposite where they put the code in one code block.

I guess I am just confused what are the best practices when organizing your code.

I hope that clarifies my question. Thanks for your help.

 
Jeffrey Friesen
Author
Ranch Hand
Posts: 67
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hozi,

Creating and then attaching a listener based on an anonymous class to a view (e.g., a button) is inefficient because it requires a new object to be created each time an Activity callback method (e.g., onCreate(Bundle)) is called. I present a much cleaner and more efficient solution in Appendix C, which is freely available as a 96-page PDF file distributed in the book's code file -- see http://www.apress.com/9781430264545.

All the best.

Jeff
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you interest i am sending my CodeStyle xml file. You can add it in eclipse.
I create this code template based on VisualStudio.

To replacing it follow this path
Window > Preferences > Java > Code Style > Formatter

Coderanch does not allow upload XML file so i paste it here.
Just create a new xml file and put in it.

 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeffrey Friesen wrote:Hi Hozi,

Creating and then attaching a listener based on an anonymous class to a view (e.g., a button) is inefficient because it requires a new object to be created each time an Activity callback method (e.g., onCreate(Bundle)) is called. I present a much cleaner and more efficient solution in Appendix C, which is freely available as a 96-page PDF file distributed in the book's code file -- see http://www.apress.com/9781430264545.

All the best.

Jeff


Hi Jeff,

Is this what you are referring to? If so, would you mind explain this a little further as to how it work? I am currenlty working a very small applicaton project and it has 6 clickables on the MainActivity and it just seeems like I have code all over the place.

I guess what I don't understand, if this is what you were referring to, is where this is called or how it knows which button is clicked.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic