Java/Android which coding method is preferred? -
as self taught coder have question coding best practice. have written same program in 2 different ways, , give me guidance on way preferred? might cpu overhead or ram usage or coding best practice perspective. appreciate there may many answers or theories question, , know isn't stack overflow here for, me stack overflow question coding style causing me problems , don't know method should either sticking or adopting.
both examples have simple xml layout file 2 buttons , 2 updatable textviews (not included here).
example 1. (the way tend code @ moment)
public class mainactivity extends activity { button button1add, button2add; textview text1display, text2display; int count1, count2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initilizebuttons(); setuplisteners(); } public void initilizebuttons() { button1add = (button) findviewbyid(r.id.button1); button2add = (button) findviewbyid(r.id.button2); text1display = (textview) findviewbyid(r.id.textview1); text2display = (textview) findviewbyid(r.id.textview2); count1 = integer.parseint(text1display.gettext().tostring()); count2 = integer.parseint(text2display.gettext().tostring()); } public void setuplisteners() { button1add.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { count1++; text1display.settext(string.valueof(count1)); } }); button2add.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { count2++; text2display.settext(string.valueof(count2)); } }); } }
example 2. (the way think should coding?)
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initilizebuttons(); } public void initilizebuttons() { button button1add = (button) findviewbyid(r.id.button1); button button2add = (button) findviewbyid(r.id.button2); button1add.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { textview text1display = (textview) findviewbyid(r.id.textview1); int headcount = integer.parseint(text1display.gettext().tostring()); headcount++; text1display.settext(string.valueof(headcount)); } }); button2add.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { textview text2display = (textview) findviewbyid(r.id.textview2); int bodycount = integer.parseint(text2display.gettext().tostring()); bodycount++; text2display.settext(string.valueof(bodycount)); } }); } }
in example 1 assume quicker (i.e. less cpu) i've declared variables , add them throughout programme. i'm worried here ram usage.
in example 2 think cause more cpu usage has redeclare variables each time used, maybe uses less ram?
these examples, , i'm sure not cause in way of cpu or ram overheads. apply information answer supply general coding practice.
i think example 1 should preferred way.
the ram usage not increase much, widgets existing if use findviewbyid or not. reference objects shown.
personally use androidannotations allows drop initilizebuttons()
can add annotations fields , library injects them you. @ thier code comparison on thier website... awesome. , supports many other nice features. , best part is, uses code generation , not use runtime reflection(what costs cpu , performance) other similar librarys...
Comments
Post a Comment