• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Keeping java code clean

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Author
Posts: 86
5
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 202
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic