Matok's PHP Blog

Why I horizontally don't align code and you shouldn't too

article image
I don't believe in 2018 are people who claimed that horizontally align code has better readability. Well, they exists among us. To be honest: if you do code in assembly or maybe C language align it.

Misunderstanding

I guess there is big diffrece between read good and looks good. This one looks good:
#define/**/Alan/**/(fflush(0),j=c=0;++c<b[6]+7;v=b[c]^(b[c+
char*H="close\r\nContent-Type:text/html\r\n\r\n<canvas id=\
'c'width='128'height='128'style='width:256px;height:256px'\
><script>x=y=-1;v=window.c;v.onmousemove=function(e){x=e.p\
ageX-v.offsetLeft;y=e.               pageY-v.offsetTop};w=\
new WebSocket('ws'+                     location.href.subs\
tr(4));w.binary"                          "Type='arraybuff\
er';w.onmess"                               "age=function(\
e){c=v.getC"                                 "ontext('2d')\
;b=c.crea"              "teI"                 "mageData(12\
8,128);u=        new Uint8Array(d=e.           data);b.dat\
a.set(u)      ;c.putImageData(b,0,0)            ;u[0]=x/2;\
u[1]=y/2    ;w.send(d.slice(0,x<0?0:             2));x=y=-\
1}</scr"   "ipt>",b[9999];float*e,d[             65536],u,v
,lu,lv,    z;void*f;typedef unsigned              long long
l;l*p,t[  99]={0x67452301,0xEFCDAB89,            0x98BADCFE
,308438*  881,3285377520       },i,j,k           ,n,m[204];
#include        /*io*/          <stdio.h>        /*turing*/
void s(){  ;     for(  k          =0;k++<7 *  2  *9;)for(i=
0;++i<127; )      e=d +  i* 4   +512*k,1[e]      +=(i-u)*(i
-u)+(k-v)* (k- v  )<20?*e+=UV_DROP:0;}l*q=  m,   g=1LL<<32;
l(h)(l v,l (a)){ return(v<<a)|(v%g>>(32-a    )  );}int main
(int c/*ha r*/,  char**y){for(e=d;fgets(b      ,m[97]=480,\
stdin)&&*b >31;  )for( i=j=0;n=b[i],i<18   ?  (l)"sec-webs\
ocket-key: "[i]       ==(n|32):n-32? m[j/4]  |=(j< 24?n:(l)
"258EAFA5-" "E914-47DA-95CA-C5AB0DC85B"     "11\x80"[j-24])
<<(3-j%4)*8 ,++j<61:1;)i++;for(j=3;--j;    ){for(i=4;++i<74
;)i>9?p=q+i- 2             ,p[8]=h(p[5     ]^*p^p[-6]^p[-8]
#define/*hah  aha          <--mouth*/      laplacian(u)l##u
,1):(t[i]=t[9 -i]       );for(p=t+7          ,i=-1;++i<82;k
=*p++,p[2]=h(p[1],5)   +p[-3]+n+     (i/+     20%2?*p^k^p[-
2]:(*p&k)|((i< 20?~*p:*p|k)&p       [-2        ]))+*q++,t[+
81-i]+=(i>76)*     (* p=          h(*           p,30*(i<80)
)))for(;n-k;n=(((               1LL               +(9<<i/20
)/8)<<60)/k+k)/2)             k=n   ;}  for     (i =0;i<57;
i++)i<30?q[i%5*             16/   3+   i/5]   |=t[i%5]%g<<4
>>(30+i%5%3*2-i/5       *6)&   63,0    :(n   =*q++,b[i-30]=
n<62?n-37+"fl!"     [n      /26]:n    *4-    205);for(prin\
tf("HTTP/1.1 "/*    |     */"%d "          "OK\r\nConnecti\
on:%s%.27s=\r\n\r\n"       ,101   +99*!  *m,!*m?H:"upgrade\
\r\nUpgrade:webso"        "c"   "ket\r\nSec-WebSocket-Acce\
pt:",b);e-d<65536 ;      e+=  4)e[1]=(*e=UV_BACKGROUND);for
(;*m;c=0){for(f= c<      2  ?0:fopen(y[1],"r");f&&fscanf(f,
"%f,%f,",&u,&v)>0;)s      ();for(;j<SPEED*4;j+=2)for(i=0;i<
63504;e+=5-j%4*2,z=      TIMESTEP,*e=u+z*(elta_u),e[1]=v+z*
(elta_v),i+=4)for(c=  0;c+2;u=v,v=e[4],lu=lv,lv=e[-508]+*e+
e[8]+e[516]-v*4)e=d+i+512+i/504*8+j%4-c--;for(;n=c%4,65544>
c;c++)putchar(c>7?n>2||(u=d[c-n-7]*4*(n<2?!n?RGB))>255?255:
#define/*x*/Turing(x)4]=getchar()&127))u=v;if(b[6]>1)s();}}
u<0?0:u:c<0?124-c*3:c==5);/* -- The word "genius" is */ for


                 Alan Turing  (1912--1954)

What is horizontal alignment in code?

Look at these three examples:

private function getTranslationParams()
{
    $params                            = [];
    $params['%sitename%']              = $this->channel->getSitename();
    $params['%hostname%']              = $this->channel->getHostname();
    $params['%hostname_and_protocol%'] = self::URL_SCHEME.'://'.$this->channel->getHostname();

    return $params;
}
foreach ($profileTypes as $profileType) {
    $entityName        = $profileHelper->getEntityName($profileType);
    $profileRepository = $this->getDoctrine()->getRepository($entityName);
    $items             = $profileRepository->findBy([
            'isPublished' => true,
            'channel'     => $channel,
        ], [
            'id' => 'desc',
        ],
        3
    );

    $viewArr['items'][$profileType] = $items;
}
$lastValue                = end($range);
$result[$lastValue.'-gt'] = $lastValue;

Why is this alignment bad?

Well, are examples above something you like to read? Because this is real code from real application. In second example is assignment to variable $items absolutely messed up with this weird alignment. Natural is to read from left to right, when you encounter something spaces, spaces ... more spaces something you must almost turn your head like owl if you want to read whole line. This kind of alignment breaks nature of reading from left to right and rather force you to read by columns.

Java style guide by Google is here. To summarize their opinion: it's hard to maintain and doing this fucked up version history information.

There is whole chapter in Clean Code about source code formatting and this also covers horizontal alignment.

I have found, however, that this kind of alignment is not useful. The alignment seems to emphasize the wrong things and leads my eye away from true intent.
Rober C. Martin / Clean Code

Conclusion

. I can only agree with Google style guide and Uncle Bob. Finding something in version control system was like punishment because often you have several versions that only change spaces, so any developer spent much more working with this kind of stuff when it was needed. I never do any kind of measurement, but I'm sure that my pace was stunted by 5-10%.


Footnotes:


If you like this article then mark it as helpful to let others know it's worth to read. Otherwise leave me a feedback/comment and we can talk about it.

I'm foreigner. Where I live my friends call me Maťok.


Comments - Coming Soon