Strict types

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

    Strict types

    Hi All,

    For a while now I've been wishing PHP had (at least the option to
    enable) strict types. It would help a massive amount in BIG
    applications, and maybe start to taper the millions of lines of crap
    code that's out there.

    PHP is a great language, but the masses of incompetant coders out there
    do absolutely nothing to help the language grow. I know we all have to
    start somewhere, so please don't think I'm being eliteist, because I'm
    not, but something has to be done to try to fix the mess out there.

    Surely the contractors on here have witnessed this, or is it just me?

    What is everyone else's opinion?

    Cheers,

    T'rog

  • Andy Jeffries

    #2
    Re: Strict types

    On Thu, 30 Mar 2006 00:53:47 -0800, Treefrog wrote:[color=blue]
    > For a while now I've been wishing PHP had (at least the option to enable)
    > strict types. It would help a massive amount in BIG applications, and
    > maybe start to taper the millions of lines of crap code that's out there.[/color]

    To be honest the only real problem is with objects. For large
    applications a consistent naming scheme helps with simple variables and in
    a lot of cases it's not really important what the variable is.

    With regards to objects, PHP5 now has type hints which ensures a parameter
    to a function or method is of a given type (class):

    function bar(Foo $foo)
    {
    ...
    }

    Cheers,



    Andy

    --
    Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
    http://www.gphpedit.org | PHP editor for Gnome 2
    http://www.andyjeffries.co.uk | Personal site and photos

    Comment

    • David Haynes

      #3
      Re: Strict types

      Treefrog wrote:[color=blue]
      > Hi All,
      >
      > For a while now I've been wishing PHP had (at least the option to
      > enable) strict types. It would help a massive amount in BIG
      > applications, and maybe start to taper the millions of lines of crap
      > code that's out there.
      >
      > PHP is a great language, but the masses of incompetant coders out there
      > do absolutely nothing to help the language grow. I know we all have to
      > start somewhere, so please don't think I'm being eliteist, because I'm
      > not, but something has to be done to try to fix the mess out there.
      >
      > Surely the contractors on here have witnessed this, or is it just me?
      >
      > What is everyone else's opinion?
      >
      > Cheers,
      >
      > T'rog
      >[/color]
      Well, you could do it in PHP5 by wrapping all the basic types in classes
      like java does for a lot of things. Then each type class would have its
      own toXXX converters to handle the casts.

      This is an example of 'fixed with another level of abstraction' and I
      wonder at the utility of it all. PHP - like shell - is often attractive
      to coders *because* it is loosely typed.

      I'm not sure how many coders would find PHP as attractive if they had to
      follow stricter object type coding. For example:

      $a = 'This is a string';
      $b = ' This is another string ';
      $c = 3;
      $d = $a.$b.$c;

      would become something like:
      $a = new String('This is a string');
      $b = new String(' This is another string ');
      $c = new Int(3);

      $d = $a->concat($b->concat($c->toString())) ;

      Type-safe - sure! More meaningful? I would argue that all the method
      calls get in the way of the meaning of the code.

      Wasn't there a phplint project to help do some high level analysis of code?

      -david-

      Comment

      • Oli Filth

        #4
        Re: Strict types

        David Haynes said the following on 30/03/2006 11:43:[color=blue]
        > I'm not sure how many coders would find PHP as attractive if they[/color]
        had to[color=blue]
        > follow stricter object type coding. For example:
        >
        > $a = 'This is a string';
        > $b = ' This is another string ';
        > $c = 3;
        > $d = $a.$b.$c;
        >
        > would become something like:
        > $a = new String('This is a string');
        > $b = new String(' This is another string ');
        > $c = new Int(3);
        >
        > $d = $a->concat($b->concat($c->toString())) ;[/color]


        Well, Java has strict typing, and you don't have to do this. Same in
        C++ (for the most part).



        --
        Oli

        Comment

        • Treefrog

          #5
          Re: Strict types

          David Haynes wrote:[color=blue]
          > Well, you could do it in PHP5 by wrapping all the basic types in classes
          > like java does for a lot of things. Then each type class would have its
          > own toXXX converters to handle the casts.[/color]

          Cute. I might spend some time playing with that idea. but I'd prefer if
          PHP had built in support...

          [color=blue]
          > This is an example of 'fixed with another level of abstraction' and I
          > wonder at the utility of it all. PHP - like shell - is often attractive
          > to coders *because* it is loosely typed.
          >
          > I'm not sure how many coders would find PHP as attractive if they had to
          > follow stricter object type coding.[/color]

          Hmm, I agree I think, hence wanting the option to use strict types. The
          problem is, from my experience, people initially use PHP because it's
          easy and they can get their idea realised in a short time with a
          minimal leanring curve/expense. Unfortunately, soem of these ideas
          actually work and start making money, sometimes BIG money, and they are
          left with poorley coded legacy crap that everybody firefights to keep
          working... and any improvments are complete kluges.

          I'm not sure what can be done to change that, but I want to stay with
          PHP, it can be used for huge scalable systems, but only if coded
          properly. I suppose I'm clutching at straws to make it harder to write
          bad code.

          Comment

          • David Haynes

            #6
            Re: Strict types

            Oli Filth wrote:[color=blue]
            > David Haynes said the following on 30/03/2006 11:43:[color=green]
            > > I'm not sure how many coders would find PHP as attractive if they had to
            >> follow stricter object type coding. For example:
            >>
            >> $a = 'This is a string';
            >> $b = ' This is another string ';
            >> $c = 3;
            >> $d = $a.$b.$c;
            >>
            >> would become something like:
            >> $a = new String('This is a string');
            >> $b = new String(' This is another string ');
            >> $c = new Int(3);
            >>
            >> $d = $a->concat($b->concat($c->toString())) ;[/color]
            >
            >
            > Well, Java has strict typing, and you don't have to do this. Same in
            > C++ (for the most part).
            >
            >
            >[/color]
            Yes, they do, but they are designed into the language from the
            beginning. I was operating from the assumption of using what was
            available today in PHP5.

            -david-

            Comment

            • David Haynes

              #7
              Re: Strict types

              Treefrog wrote:[color=blue]
              > David Haynes wrote:[color=green]
              >> Well, you could do it in PHP5 by wrapping all the basic types in classes
              >> like java does for a lot of things. Then each type class would have its
              >> own toXXX converters to handle the casts.[/color]
              >
              > Cute. I might spend some time playing with that idea. but I'd prefer if
              > PHP had built in support...
              >
              >[color=green]
              >> This is an example of 'fixed with another level of abstraction' and I
              >> wonder at the utility of it all. PHP - like shell - is often attractive
              >> to coders *because* it is loosely typed.
              >>
              >> I'm not sure how many coders would find PHP as attractive if they had to
              >> follow stricter object type coding.[/color]
              >
              > Hmm, I agree I think, hence wanting the option to use strict types. The
              > problem is, from my experience, people initially use PHP because it's
              > easy and they can get their idea realised in a short time with a
              > minimal leanring curve/expense. Unfortunately, soem of these ideas
              > actually work and start making money, sometimes BIG money, and they are
              > left with poorley coded legacy crap that everybody firefights to keep
              > working... and any improvments are complete kluges.
              >
              > I'm not sure what can be done to change that, but I want to stay with
              > PHP, it can be used for huge scalable systems, but only if coded
              > properly. I suppose I'm clutching at straws to make it harder to write
              > bad code.
              >[/color]

              I think it ends up being more organic. You start to develop a set of
              libraries and practices that protect you from a lot of coding goofs and
              continue to build/refine.

              One of the more humbling things I think any good coder can do is look at
              their code from a year or two before. I know I usually end up wondering
              what I was thinking in some places ;-)

              With the stronger object support in PHP5, a lot of things can be coded
              to be protected well and, as with all programming languages, it is
              possible to write good and bad code regardless of the protection the
              language offers.

              One other way people have handled the 'type' issue is to use a strong
              naming convention - e.g. Hungarian notation. Personally, I can't stand
              using it but others find it helpful.

              -david-

              Comment

              • Jerry Stuckle

                #8
                Re: Strict types

                Treefrog wrote:[color=blue]
                > Hi All,
                >
                > For a while now I've been wishing PHP had (at least the option to
                > enable) strict types. It would help a massive amount in BIG
                > applications, and maybe start to taper the millions of lines of crap
                > code that's out there.
                >
                > PHP is a great language, but the masses of incompetant coders out there
                > do absolutely nothing to help the language grow. I know we all have to
                > start somewhere, so please don't think I'm being eliteist, because I'm
                > not, but something has to be done to try to fix the mess out there.
                >
                > Surely the contractors on here have witnessed this, or is it just me?
                >
                > What is everyone else's opinion?
                >
                > Cheers,
                >
                > T'rog
                >[/color]

                Hi, T'rog,

                Well, one of the big attractions to PHP is the lack of strict typing. You don't
                have to go through all the overhead of declaring variable types (or even
                variables unless they are class members). Getting rid of the "administra tive
                overhead" allows you to concentrate on the problem.

                You can write good code in any language (except maybe COBOL :-) ). You can
                write bad code in any language - including Java and C++. However, IMHO, strict
                typing does in general get you to write "better" code.

                I think a good compromise between old and new would be a "strict" flag. It
                could be set in the php.ini file or .htaccess (on Apache, if allowed). Perhaps
                it could even be a parse-time command (ini_set isn't executed until too late),
                similar to C/C++'s pragma statement. The last would allow someone to convert a
                website one file at a time to strict. Then when everything's changed, change
                the php.ini file, or, if on a shared server, at a statement to .htaccess.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • Steve Chapel

                  #9
                  Re: Strict types

                  Treefrog wrote:[color=blue]
                  > Hi All,
                  >
                  > For a while now I've been wishing PHP had (at least the option to
                  > enable) strict types. It would help a massive amount in BIG
                  > applications, and maybe start to taper the millions of lines of crap
                  > code that's out there.[/color]

                  Generally speaking, PHP is a scripting language. Scripting languages are
                  great for one-off projects and small programs, but become unwieldy in
                  big applications, larger than a few thousand lines. For larger projects,
                  I would suggest Java, which has strict typing and encourages
                  object-oriented programming and modularization.

                  Comment

                  • Treefrog

                    #10
                    Re: Strict types

                    Steve Chapel wrote:[color=blue]
                    > Treefrog wrote:[color=green]
                    > > Hi All,
                    > >
                    > > For a while now I've been wishing PHP had (at least the option to
                    > > enable) strict types. It would help a massive amount in BIG
                    > > applications, and maybe start to taper the millions of lines of crap
                    > > code that's out there.[/color]
                    >
                    > Generally speaking, PHP is a scripting language. Scripting languages are
                    > great for one-off projects and small programs, but become unwieldy in
                    > big applications, larger than a few thousand lines. For larger projects,
                    > I would suggest Java, which has strict typing and encourages
                    > object-oriented programming and modularization.[/color]

                    No no, I can already code c# (so probably Java too), that has strict
                    types and great oo support, but I've grown up using PHP, all my
                    professional career has been mainly PHP based and I want to stay with
                    the language. I've seen it evolve to inlcude basic OO support, then
                    more advanced OO....

                    Once upon a time, PHP was a scriting language, but it's more than that
                    now. For example, I'm currently working with a 500,000 line 100% PHP
                    application. Because 95% of it is built on poor coding, it's nearly
                    impossible, certainly futile to try to impliment standards into it. I
                    was just trying to think of/ask for ways in which situations like this
                    can be stopped in the future. Which, I think needs to happen for PHP's
                    future. I've worked in quite a few places, and with quite a few other
                    developers, and they seem to agree; PHP code is nearly always messy
                    crap.

                    But it doesn't have to be.

                    Comment

                    • Steve Chapel

                      #11
                      Re: Strict types

                      Treefrog wrote:[color=blue]
                      > Once upon a time, PHP was a scriting language, but it's more than that
                      > now. For example, I'm currently working with a 500,000 line 100% PHP
                      > application. Because 95% of it is built on poor coding, it's nearly
                      > impossible, certainly futile to try to impliment standards into it. I
                      > was just trying to think of/ask for ways in which situations like this
                      > can be stopped in the future. Which, I think needs to happen for PHP's
                      > future. I've worked in quite a few places, and with quite a few other
                      > developers, and they seem to agree; PHP code is nearly always messy
                      > crap.
                      >
                      > But it doesn't have to be.[/color]

                      I'm not so sure about that. Large Perl programs (and even many small
                      programs) are nearly always messy crap. I believe it's a natural
                      property of languages that have weak or loose typing, do not require you
                      to declare variables, and do not have a separate compilation step. These
                      kinds of languages are often called scripting languages
                      <http://en.wikipedia.or g/wiki/Scripting_langu ages>.

                      Perhaps there is a way to make scripting languages more suited for large
                      applications. Discussion of how to do that may be beyond the scope of a
                      post in a PHP newsgroup, however.

                      Comment

                      • Chung Leong

                        #12
                        Re: Strict types


                        Treefrog wrote:[color=blue]
                        > Hi All,
                        >
                        > For a while now I've been wishing PHP had (at least the option to
                        > enable) strict types. It would help a massive amount in BIG
                        > applications, and maybe start to taper the millions of lines of crap
                        > code that's out there.[/color]

                        Eliminating badcode by altering the language--that sounds rather
                        Orwellian to me.

                        Comment

                        • David Haynes

                          #13
                          Re: Strict types

                          Chung Leong wrote:[color=blue]
                          > Treefrog wrote:[color=green]
                          >> Hi All,
                          >>
                          >> For a while now I've been wishing PHP had (at least the option to
                          >> enable) strict types. It would help a massive amount in BIG
                          >> applications, and maybe start to taper the millions of lines of crap
                          >> code that's out there.[/color]
                          >
                          > Eliminating badcode by altering the language--that sounds rather
                          > Orwellian to me.
                          >[/color]
                          You mean double plus ungood?


                          Comment

                          • Treefrog

                            #14
                            Re: Strict types

                            Chung Leong wrote:[color=blue]
                            >
                            > Eliminating badcode by altering the language--that sounds rather
                            > Orwellian to me.[/color]

                            You're absolutely right. My most humble apologese. Let the bad code
                            prevail!

                            Comment

                            • Chung Leong

                              #15
                              Re: Strict types


                              Treefrog wrote:[color=blue]
                              > Chung Leong wrote:[color=green]
                              > >
                              > > Eliminating badcode by altering the language--that sounds rather
                              > > Orwellian to me.[/color]
                              >
                              > You're absolutely right. My most humble apologese. Let the bad code
                              > prevail![/color]

                              Just because people can make illogical statements in English does not
                              imply that stupidity will prevail.

                              Comment

                              Working...