am I too wired? On coding style

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zhang Weiwu

    am I too wired? On coding style

    Hello. I have a question that spining around my head for a long time. I
    prefer to make this kind of if statement:

    if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
    'Location: '.$GLOBALS['phpgw']->
    link('/', 'menuaction=for um.uiforum.inde x')
    );

    But almost all the 'good' php code I saw write it this way:

    if (!$GLOBALS['phpgw_info']['user']['admin'])
    {
    Header(
    'Location: '.$GLOBALS['phpgw']->
    link('/', 'menuaction=for um.uiforum.inde x')
    );
    }

    the argument is the latter is more readable and easy to understand. But,
    to me, a if statement immediately followed by another statement or
    function call is more readable and easy to understand. It saved me from
    thinking what's after this statement or function call. Truly, I think
    keeping a stack of 'if' or 'for' in the brain is difficult for me, I
    tends to remove the {} and add the statement after if-clause when if
    statement contain only one statement.

    I am just thinking if my way of keeping code logic in brain is too
    wired. If most people truly consider the {} more easier to
    read/understand, I'll force myself write understandable code; if there
    are only very small difference in understanding (considering reading
    10000 line of source, even small difficulty counts), I would like to
    keep my style.

  • NSpam

    #2
    Re: am I too wired? On coding style

    Zhang Weiwu wrote:[color=blue]
    > Hello. I have a question that spining around my head for a long time. I
    > prefer to make this kind of if statement:
    >
    > if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
    > 'Location: '.$GLOBALS['phpgw']->
    > link('/', 'menuaction=for um.uiforum.inde x')
    > );
    >
    > But almost all the 'good' php code I saw write it this way:
    >
    > if (!$GLOBALS['phpgw_info']['user']['admin'])
    > {
    > Header(
    > 'Location: '.$GLOBALS['phpgw']->
    > link('/', 'menuaction=for um.uiforum.inde x')
    > );
    > }
    >
    > the argument is the latter is more readable and easy to understand. But,
    > to me, a if statement immediately followed by another statement or
    > function call is more readable and easy to understand. It saved me from
    > thinking what's after this statement or function call. Truly, I think
    > keeping a stack of 'if' or 'for' in the brain is difficult for me, I
    > tends to remove the {} and add the statement after if-clause when if
    > statement contain only one statement.
    >
    > I am just thinking if my way of keeping code logic in brain is too
    > wired. If most people truly consider the {} more easier to
    > read/understand, I'll force myself write understandable code; if there
    > are only very small difference in understanding (considering reading
    > 10000 line of source, even small difficulty counts), I would like to
    > keep my style.
    >[/color]
    Its simply a matter of style. If its your own code use whatever format
    you are comfortable with. If you are producing commercial code then go
    with the style that the organisation uses.

    Comment

    • John Bokma

      #3
      Re: am I too wired? On coding style

      Zhang Weiwu wrote:
      [color=blue]
      > Hello. I have a question that spining around my head for a long time.
      > I prefer to make this kind of if statement:
      >
      > if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
      > 'Location: '.$GLOBALS['phpgw']->
      > link('/', 'menuaction=for um.uiforum.inde x')
      > );
      >
      > But almost all the 'good' php code I saw write it this way:
      >
      > if (!$GLOBALS['phpgw_info']['user']['admin'])
      > {
      > Header(
      > 'Location: '.$GLOBALS['phpgw']->
      > link('/', 'menuaction=for um.uiforum.inde x')
      > );
      > }
      >
      > the argument is the latter is more readable and easy to understand.[/color]

      Nah, the best argument is: if you use {} you can easily add debug
      statements.

      I would write it like:

      if ( !$GLOBALS[ 'phpgw_info' ][ 'user' ][ 'admin' ] ) {

      Header(

      'Location: ' .
      $GLOBALS[ 'phpgw' ]->link(

      '/',
      'menuaction=for um.uiforum.inde x'
      )
      );
      }
      [color=blue]
      > But,
      > to me, a if statement immediately followed by another statement or
      > function call is more readable and easy to understand.[/color]

      Then you'll have a hard time reading code. I guess that PHP can do the
      following, which I even consider more clear:

      $GLOBALS[ 'phpgw_info' ][ 'user' ][ 'admin' ] or
      Header(

      'Location: ' .
      $GLOBALS[ 'phpgw' ]->link(

      '/',
      'menuaction=for um.uiforum.inde x'
      )
      );

      so $GLOBALS bla bla OR else do Header ...

      Which I sometimes indeed write as:

      $GLOBALS[ 'phpgw_info' ][ 'user' ][ 'admin' ] or Header(

      'Location: ' .
      $GLOBALS[ 'phpgw' ]->link(

      '/',
      'menuaction=for um.uiforum.inde x'
      )
      );

      if I am 100% sure it doesn't need debugging statements. I read the part
      before the or as an assertion. It must be valid to continue, or else do
      the or part to "fix" the problem. In my Perl code I do things like:

      defined $variable or $variable = 'default value';

      Which I read: the variable must be defined, or else, give it a defined
      value.

      ( sometimes you see this written as: $variable ||= 'default value',
      which can be wrong in special cases).
      [color=blue]
      > It saved me from
      > thinking what's after this statement or function call. Truly, I think
      > keeping a stack of 'if' or 'for' in the brain is difficult for me, I
      > tends to remove the {} and add the statement after if-clause when if
      > statement contain only one statement.[/color]

      Yup, and then you have to put the {} back when you want to debug a piece
      of code.

      --
      John MexIT: http://johnbokma.com/mexit/
      personal page: http://johnbokma.com/
      Experienced programmer available: http://castleamber.com/
      Happy Customers: http://castleamber.com/testimonials.html

      Comment

      • Chung Leong

        #4
        Re: am I too wired? On coding style

        "Zhang Weiwu" <zhangweiwu@rea lss.com> wrote in message
        news:q51kf2x4o6 .ln2@arneg.hack erdom.org...[color=blue]
        > Hello. I have a question that spining around my head for a long time. I
        > prefer to make this kind of if statement:
        >
        > if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
        > 'Location: '.$GLOBALS['phpgw']->
        > link('/', 'menuaction=for um.uiforum.inde x')
        > );
        >
        > But almost all the 'good' php code I saw write it this way:
        >
        > if (!$GLOBALS['phpgw_info']['user']['admin'])
        > {
        > Header(
        > 'Location: '.$GLOBALS['phpgw']->
        > link('/', 'menuaction=for um.uiforum.inde x')
        > );
        > }
        >
        > the argument is the latter is more readable and easy to understand. But,
        > to me, a if statement immediately followed by another statement or
        > function call is more readable and easy to understand. It saved me from
        > thinking what's after this statement or function call. Truly, I think
        > keeping a stack of 'if' or 'for' in the brain is difficult for me, I
        > tends to remove the {} and add the statement after if-clause when if
        > statement contain only one statement.
        >
        > I am just thinking if my way of keeping code logic in brain is too
        > wired. If most people truly consider the {} more easier to
        > read/understand, I'll force myself write understandable code; if there
        > are only very small difference in understanding (considering reading
        > 10000 line of source, even small difficulty counts), I would like to
        > keep my style.
        >[/color]

        I like this better:

        $GLOBALS['phpgw_info']['user']['admin'] or HeAdEr(
        'Location: '.$GLOBALS['phpgw']->lInK('/',
        'menuaction=for um.uiforum.inde x'));



        Actually, I don't :-)


        Comment

        • NSpam

          #5
          Re: am I too wired? On coding style

          Chung Leong wrote:[color=blue]
          > "Zhang Weiwu" <zhangweiwu@rea lss.com> wrote in message
          > news:q51kf2x4o6 .ln2@arneg.hack erdom.org...
          >[color=green]
          >>Hello. I have a question that spining around my head for a long time. I
          >>prefer to make this kind of if statement:
          >>
          >>if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
          >>'Location: '.$GLOBALS['phpgw']->
          >>link('/', 'menuaction=for um.uiforum.inde x')
          >>);
          >>
          >>But almost all the 'good' php code I saw write it this way:
          >>
          >>if (!$GLOBALS['phpgw_info']['user']['admin'])
          >>{
          >>Header(
          >>'Location: '.$GLOBALS['phpgw']->
          >>link('/', 'menuaction=for um.uiforum.inde x')
          >>);
          >>}
          >>
          >>the argument is the latter is more readable and easy to understand. But,
          >>to me, a if statement immediately followed by another statement or
          >>function call is more readable and easy to understand. It saved me from
          >>thinking what's after this statement or function call. Truly, I think
          >>keeping a stack of 'if' or 'for' in the brain is difficult for me, I
          >>tends to remove the {} and add the statement after if-clause when if
          >>statement contain only one statement.
          >>
          >>I am just thinking if my way of keeping code logic in brain is too
          >>wired. If most people truly consider the {} more easier to
          >>read/understand, I'll force myself write understandable code; if there
          >>are only very small difference in understanding (considering reading
          >>10000 line of source, even small difficulty counts), I would like to
          >>keep my style.
          >>[/color]
          >
          >
          > I like this better:
          >
          > $GLOBALS['phpgw_info']['user']['admin'] or HeAdEr(
          > 'Location: '.$GLOBALS['phpgw']->lInK('/',
          > 'menuaction=for um.uiforum.inde x'));
          >
          >
          >
          > Actually, I don't :-)
          >
          >[/color]
          Readability is more important than brevity, Revisiting code that was
          written months ago is always a problem whether in ones own code space or
          a commercial environment. I suppose the phrase "pleas leave this code in
          the state you would like to find it" just about sums the argument up <g>

          Comment

          • nospam@geniegate.com

            #6
            Re: am I too wired? On coding style

            In: <q51kf2x4o6.ln2 @arneg.hackerdo m.org>, Zhang Weiwu <zhangweiwu@rea lss.com> wrote:[color=blue]
            >Hello. I have a question that spining around my head for a long time. I
            >prefer to make this kind of if statement:
            >
            >if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
            > 'Location: '.$GLOBALS['phpgw']->
            > link('/', 'menuaction=for um.uiforum.inde x')
            >);[/color]

            Personally, I hate when people do that. I like the { }, I just
            find it easier to see what statement goes with what. (Particularly
            if it's followed by a statement that spans multiple lines)

            [color=blue]
            >But almost all the 'good' php code I saw write it this way:[/color]
            [color=blue]
            >if (!$GLOBALS['phpgw_info']['user']['admin'])
            >{
            > Header(
            > 'Location: '.$GLOBALS['phpgw']->
            > link('/', 'menuaction=for um.uiforum.inde x')
            > );
            >}[/color]

            I don't like that either. (I must be a grump!) Here is what I like:

            if($condition) { //1-liner }

            if($condition) {
            // multi
            // line
            // code.
            }

            Some folks really hate my style too. Sometimes I'll break $condition
            across multiple lines, but only if it can't easily fit on 1 line.

            Heh.. I think light ought to be spelled 'lite' too. :-)
            [color=blue]
            >10000 line of source, even small difficulty counts), I would like to
            >keep my style.[/color]

            $works_for_you ? keepYourStyle() :change();

            Jamie
            --
            http://www.geniegate.com Custom web programming
            guhzo_42@lnubb. pbz (rot13) User Management Solutions

            Comment

            Working...