Code style

Discussion in 'iOS Programming' started by pavelk, Nov 10, 2014.

  1. pavelk macrumors newbie

    Joined:
    Nov 8, 2014
    #1
    Which code style you are using for Objective-C programming?
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    This question is extremely vague. What do you mean by 'code style'?
     
  3. Sean7512 macrumors 6502a

    Joined:
    Jun 8, 2005
    #3
  4. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #4
    I dislike this style

    Code:
    if (user.isHappy) {
    //Do something
    }
    else {
    //Do something else
    }
    And prefer this way (with tab replaced into 3 space)

    Code:
    if (user.isHappy) 
    {
       //Do something
    }
    else 
    {
       //Do something else
    }
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  6. X5-599 macrumors regular

    Joined:
    Oct 28, 2012
    #6
    I'm pretty much conform with that one. But when I started Objective-C I saw most init methods returning "id". Not "instancetype". Is that something that developed as a standard later? Also, I've seen code where they return the Class. Like so:
    Code:
    -(MyCoolClass *)init
    {
      //....
      return self;
    }
    
     
  7. Sean7512 macrumors 6502a

    Joined:
    Jun 8, 2005
    #7
    Take a look on Apple's page on Adapting Modern Objective-C: https://developer.apple.com/library...ernObjective-C/AdoptingModernObjective-C.html

    instancetype is the more preferred way these days.
     
  8. pavelk thread starter macrumors newbie

    Joined:
    Nov 8, 2014
    #8
    Yes, maybe my question was a little bit vague, but thank you guys for answers and links. And maybe its a holywar question, but why you using tabs (or spaces), what advantages has each approach?
     
  9. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #9
    If you use spaces and open a file in a different editor the layout remain as it should be; in case tab is replaced with space.
    Sometimes when I open a XCode file In Textwrangler it just looks awful.
     
  10. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #10
    I prefer this: :eek:
    Code:
    char*_ = "'""/*";
    #include <stdio.h>
    #define m 21
    #define o(l, k) for(l=0; l<k; l++)
    #define n(k) o(T, k)
    
    
                  int E,L,O,R,G[42][m],h[2][42][m],g[3][8],c
                  [42][42][2],f[42]; char d[42]; void v( int
                  b,int a,int j){ printf("\33[%d;%df\33[4%d"
                  "m  ",a,b,j); } void u(){ int T,e; n(42)o(
                  e,m)if(h[0][T][e]-h[1][T][e]){ v(e+4+e,T+2
                  ,h[0][T][e]+1?h[0][T][e]:0); h[1][T][e]=h[
                  0][T][e]; } fflush(stdout); } void q(int l
                                ,int k,int p){
                                int T,e,a; L=0
                                ; O=1; while(O
                                ){ n(4&&L){ e=
                                k+c[l] [T][0];
                                h[0][L-1+c[l][
                                T][1]][p?20-e:
    e]=-1; } n(4){                                          e=k+c[l][T][0]; a=L+c[l][T][
    1]+1; if(a==42                                          || h[0][a][p?20-e:e]+1){ O=0
    ; } } n(4){ e=                                          k+c[l][T][0]; h[0][L + c[l][
    T][1]][p?20-e:                                          e]=g[1][f[p?19+l:l]]; } L++;
    u(); } n(42) {                                          o(e,m)if(h[0][T][e]<0)break;
    o(a, m&&e==m){                                          for(L=T; L; L--) { h[0][L][a
    ]=h[0][L-1] [a                                          ]; } h[0][0][a]=-1; } } u();
    }int main(){ int T,e,t,r,i,s              ,D,V,K; printf("\33[2J\33[?25l"); n(8)g[i=
    1][T]=7-T; R--; n(42) o(e,m)              G[T][e]--; while(fgets(d,42,stdin)) { r=++
    R; n(17){ e=d[T]-48; d[T]=0;              if ((e&7)==e) { g[0][e] ++; G[R][T+2]=e; }
    } } n(8)if(g[0][7-T]){ t=g[i              ][O]; g[i][O++]=g[i][T]; g[i][T]=t; } n(8)
    g[2][g[i][T]]=T; n(R+i)o(e,m              )if(G[T][e]+i) G[T][e]=g[2][G[T][e]]; n(19
    )o(t,2){ f[T+t+T]=(T["+%#,4"              "5>GP9$5-,#C?NX"]-35)>>t*3&7; o(e,4){ c[T]
    [e][t]=("5'<$=$8)Ih$=h9i8'9"              "t=)83)l4(99(g9>##>4(" [T+t+T]-35)>>e*2&3;
    } } n(15) { s=T>9?m:(T&3)-3?15:36;o(e,s)o(t,2)c[T+19][e][t]="6*6,8*6.608.6264826668\
    865::(+;0(6+6-6/8,61638065678469.;88))()3(6,8*6.608.6264826668865:+;4)-*6-6/616365,\
    -6715690.5;,89,81+,(023096/:40(8-7751)2)65;695(855(+*8)+;4**+4(((6.608.626482666886\
    5:+;4+4)0(8)6/61638065678469.;88)-4,4*8+4(((60(/6264826668865:+;4-616365676993-9:54\
    +-14).;./347.+18*):1;-*0-975/)936.+:4*,80987(887(0(*)4.*""/4,4*8+4(((6264826668865:\
    +;4/4-4+8-4)0(8)6365678469.;88)1/(6*6,6.60626466686:8)8-8*818.8582/9863(+;/""*6,6.6\
    0626466686:4(8)8-8*818.8582/9863(+;/,6.60626466686:8-818.8582/9864*4+4(0())+;/.6062\
    6466686:8/8380/7844,4-4*4+4(0())69+;/0626466686:818582/9864.4/4,4-4*4+4(0())+;" [e+E
    +e+t]-40; E+=s+s; } n(45){ if(T>i) { v(2,T,7); v(46,T,7); } v(2+T,44,7); } T=0; o(e,
    42)o(t,m)h[T][e][t]--; while(R+i) { s = D=0; if (r-R) { n(19) if (G[R+i][T]+i) V=T/2
    ; else if(G[R][T]+i) s++; if(s) { if(V>4){ V=9-V; D++; } V+=29; n(20) q(c[V][T][0],c
    [V][T][i],D); } } n(19) if((L=G[R][T])+i) { O=T-L; e=O>9; t=e?18-O :O; o(K,((t&3)-3?
    16:37)){ if(K){ L=c[t+19][K-i][0]; O=c[t+19][K-i][i] ; } q(L,O,K && e); } } if(s) q(
    c[V][20][0], c[V][20][i], D); R--; } printf("\33[47;1f\33[?25h\33[40m"); return 0; }
     
  11. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #11
    ok, someone please try to compile that piece of art :p
     
  12. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #12
    Question of the day: What the heck does it do?
     
  13. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #13
    Given what the code looked like, I assumed some kind of Tetris. I compiled it and that's what it seems to do, but it didn't work properly on my Linux box with my terminal emulator.
     
  14. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #15
    That's actually pretty awesome that it would be a Tetris... but I'd hate to be the new-hire that had to maintain the code :D

    Maybe Google will use that in their hiring exam.
     
  15. theSeb macrumors 604

    theSeb

    Joined:
    Aug 10, 2010
    Location:
    Poole, England
    #16
    I thought I was the only one left that prefers this. I cannot stand { following on the same line. It must be on a new line. I have tried to adapt and I just can't do it.
     
  16. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #17
    I think great programmers are borderline OCD (I mean this in the best of ways) :cool:
     
  17. Barna Biro macrumors 6502a

    Barna Biro

    Joined:
    Sep 25, 2011
    Location:
    Luzern, Switzerland
    #18
    I'd correct the above by saying "all people who are really passionate about something".
    Be it programming, construction, painting, singing, etc. It's all about the attitude...
     
  18. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #19
    I think you're right. I just read about this and how these kind of people aren't recognized as good managers/leaders because they aren't as outgoing, yet they are some of the best decision makers.

    One thing I notice is that great programmers can stay focused for long periods of time, whereas most regular people can't pay attention for more than a sound bite and search for the quickest answer.

    Actually pretty interesting video about this. https://www.youtube.com/watch?v=c0KYU2j0TM4
     

Share This Page