Slide 1

Slide 1 text

Android Coding Guidelines revisited München,  23.09.2014   Enrique López Mañas Manuel Lilienberg  

Slide 2

Slide 2 text

Indentation   •  Blocks  that  are  nested  more  should  be  indented  more.  Blocks  that   are  nested  the  same  should  be  indented  the  same.   •  In  case  of  doubt,  automa?c  indenta?on  (op?on  +  cmd  +  L)                                                                        

Slide 3

Slide 3 text

Indentation            

Slide 4

Slide 4 text

Naming  best  practices           •  Aim  to  write  self  documen?ng  code   •  Do  not  use  magic  numbers.  CREATE  CONSTANTS   •  Do  not  use  uncommon  abbrevia?ons  (Btn,  Lbl,  Tv)   •  Include  units  in  a  name  (distanceInMeters)   •  Most  meanings  have  mul?ple  words.  PICK  ONE   •  Try  to  avoid  comments  by  crea?ng  meaningful  names  for  methods  

Slide 5

Slide 5 text

Starting  instance  variables   •  “m”  for  non  public,  non  sta?c.  “s”  for  sta?c   •  (only  in  view  classes)  

Slide 6

Slide 6 text

Some  other  elements   •  TextView,  EditText...  begins  with  the  name  of  the  func?onality  and   then  the  type  (reason:  alphabe?cal  sor?ng).   •  TextView  showOfferTextView;   •  Strings:  same  patern  but  separa?ng  them  with  _.  Reason:  is  used   throughout  the  Android  plaZorm   •  show_offer_text_view                                                                        

Slide 7

Slide 7 text

Capitalization  rules   •  Classes  start  with  uppercase  le]er   •  public  class  Foo   •  Constant  in  uppercase  le]er,  descrip?ve  name   •  private  sta?c  final  int  THIS_IS_A_CONSTANT  =  1;   •  Everything  else  with  lowercase  le]er   •  private  int  fooFunc?on()  {..};   •  private  boolean  hasBeenFound;                                                                            

Slide 8

Slide 8 text

Breaking  things   •  Wri?ng  short  methods   •  No  official  limit.  As  small  as  possible   •  If  you  have  the  same  code  twice  -­‐>  refactor.  No  excep?on.   •  Short  methods  avoid  wri?ng  large  docs   •  Keep  lines  short   •  Google  recommends  lines  of  max  100  characters.  Not  sure  why  this  is  a  magic  number,  but  is   anyway  a  good  prac?ce  to  keep  lines  short.   •  If  you  strictly  need  to  keep  them  large  (i.e.,  mul?ple  if  else  condi?ons)  then  switch  to  a  new   line  

Slide 9

Slide 9 text

JavaDoc   •  Keep  it  simple!   •  Use  it  as  you  do  it.  Not  when  you  are  finished.   •  Don’t  document  constructor,  ge]er,  se]er   •  If  a  method  is  complex  enough,  say  what  it  does!   •  Useful  annota?ons   •  @Deprecated   •  @author   •  @see   •  @param   •  @return   •  @Override  

Slide 10

Slide 10 text

Multiple  returns   •  Mul?ple  returns  can  increase  readability.      private  void  onEvent(Loca?onUpdatedEvent  event)  {                  if  (event  !=  null  &&  isInCloseDistance()  &&  !mRequestRunning)  {                          doYourStuff();                  }  else  {                          return  null;                  }          }            private  void  onEvent(Loca?onUpdatedEvent  event)  {                  if  (event  ==  null)  {                          return;                  }                    if  (!isInCloseDistance())  {                          return;                  }                    if  (mRequestRunning)  {                          return;                  }                    doYourStuff();          }  

Slide 11

Slide 11 text

Good  practices   •  Limit  scope  of  variables  

Slide 12

Slide 12 text

Good  practices   •  Always  use  bracelets  for  statements  

Slide 13

Slide 13 text

Good  practices   •  •  Brackets:  Use  K&R  style  with  4  indenta?on  spaces  (automa?c  in   Eclipse  and  Android  Studio)  

Slide 14

Slide 14 text

Good  practices   •  When  spliqng  a  line,  use  8  spaces  

Slide 15

Slide 15 text

Good  practices   •  Importers:  don’t  use  *,  list  each  class  member.   •  You  can  automa?cally  perform  the  imports.