Does the message really message?



  • I just found the following method written by a coworker in a php application that is currently being pushed into production:

        public function confirmMessage($message)
        {
            if($message){
                 return $message ;
            }
        }
    

    I'm affraid to look for where it is used.



  •  It looks like unfinished skeleton code.



  • I don't know much about PHP. What happens if $message evaluates to false?

    If you wrote something similar in, say, Java, it would not compile because "not all code paths return a value".



  • if the function doesn't explicitely return something, it returns null.



  • @Rhaban said:

    if the function doesn't explicitely return something, it returns null.
     

    I'd wager it "returns" undefined, but I'm too lazy to search php.net



  •  Well, it does do something... It's the same as:

    if(!$message)

      $message = NULL;

     (That is, assuming it's used as $message = confirmMessage($message);, obviously variable names need changing otherwise)


    But I agree it's probably just skeleton code...



  • @Evo said:

    But I agree it's probably just skeleton code...
    ...currently being pushed into production.



  • @dhromed said:

    @Rhaban said:

    if the function doesn't explicitely return something, it returns null.
     

    I'd wager it "returns" undefined, but I'm too lazy to search php.net

    PHP doesn't really have an undefined type.  Functions without an explicit return implicitly return NULL.



  • I don't really know PHP, so let me ask: does PHP coerce an empty string to false?  If so, that means this function would take an empty string and return null, right?   That seems worse than doing nothing...



  • @Xyro said:

    I don't really know PHP, so let me ask: does PHP coerce an empty string to false?  If so, that means this function would take an empty string and return null, right?   That seems worse than doing nothing...

    Yes, in PHP: NULL == '' == false == 0

     

    @Xyro said:

    Filed
    under: What's the equivalent of a null pointer exception in PHP?
    <input name="ctl00$ctl00$bcr$bcr$ctl00$PostList$ctl10$ctl23$ctl01" id="ctl00_ctl00_bcr_bcr_ctl00_PostList_ctl10_ctl23_ctl01_State" value="value:Filed%20under%3A%20%3Ca%20href%3D%22%2Ftags%2FWhat_2700_s%2Bthe%2Bequivalent%2Bof%2Ba%2Bnull%2Bpointer%2Bexception%2Bin%2BPHP_3F00_%2Fdefault.aspx%22%20rel%3D%22tag%22%3EWhat's%20the%20equivalent%20of%20a%20null%20pointer%20exception%20in%20PHP%3F%3C%2Fa%3E" type="hidden">

    Fatal error: Call to a member function bar() on a non-object in Command line code on line 1

     

    That's just for calling functions on a non-object.   You can usually set properties on a non-object (it is implicitly converted to an object).

     



  • @morbiuswilters said:

    @Xyro said:
    Filed
    under: What's the equivalent of a null pointer exception in PHP?
    Fatal error: Call to a member function bar() on a non-object in Command line code on line 1
    Strings aren't handled in an object-oriented way in PHP, right?  It's all external functions like, for example, the way Perl does? Do strings have to be non-null to manipulate them?  That is, if you wanted to transform my_string to upper case, but the variable was null, would it survive?  I'm just curious of the explosive impact code like that could do.  I should probably learn PHP one day.@morbiuswilters said:
    You can usually set properties on a non-object (it is implicitly converted to an object).
    ... er, never mind.



  • @Xyro said:

    Strings aren't handled in an object-oriented way in PHP, right?  It's all external functions like, for example, the way Perl does? Do strings have to be non-null to manipulate them?  That is, if you wanted to transform my_string to upper case, but the variable was null, would it survive?  I'm just curious of the explosive impact code like that could do.  I should probably learn PHP one day.

    Strings are primitives in PHP, yes.  The string functions will coerce a null into an empty string, so doing strtoupper() on a null results in an empty string.



  • @morbiuswilters said:

    @Xyro said:

    Strings aren't handled in an object-oriented way in PHP, right?  It's all external functions like, for example, the way Perl does? Do strings have to be non-null to manipulate them?  That is, if you wanted to transform my_string to upper case, but the variable was null, would it survive?  I'm just curious of the explosive impact code like that could do.  I should probably learn PHP one day.

    Strings are primitives in PHP, yes.  The string functions will coerce a null into an empty string, so doing strtoupper() on a null results in an empty string.

    That's stupid.  Strings should be objects, but get special handling and behavior from the compiler that no object can get, even other string-like objects.  Consistency is hardly a best practice.



  • @bstorer said:

    That's stupid.  Strings should be objects, but get special handling and behavior from the compiler that no object can get, even other string-like objects.  Consistency is hardly a best practice.



    So very true. and what's with all these "operators" people use? I've seen so much ugly code like this:

     

    X=5*Y/(2-Z)

     

    I mean, seriously, wtf? X and Y and Z are obviously "primitive" types. primitive types are inherently evil. Therefore, we should have objects for everything! No such thing as a primitive. Therefore that expression should really be this in a real programming language:

     

    X=new Integer((Integer.FIVE.Multiply(Y)).Divide(Integer.TWO.Subtract(Z))

     

    See, it's just soo much easier to read. The way a language should be, readability be damned! The latest version of this make-believe language I'm sure will do away with the cumbersome assignment "operator"  in favour of something more civilized. no such thing as a "literal" value! all numbers must be created by multiplying and adding statics. you don't type "183" you type:

    Integer.ONE.Multiply(Integer.TEN.Multiply(Integer.TEN)).Add(Integer.EIGHT.Multiply(Integer.TEN).Add(Integer.THREE))

     

    Soo much more clear and easy to read.

     

    Primitives are such a primitive concept!



  • @bstorer said:

    That's stupid.  Strings should be objects, but get special handling and behavior from the compiler that no object can get, even other string-like objects.  Consistency is hardly a best practice.
    I disagree, I believe consistency is key to easy learning and usage. That's why in my language, strings are constructed from byte arrays. Behold:


    byte[13] message_bytes = {'H','e','l','l','o',',',' ','w','o','r','l','d','!'};

    string message_str = new string(message_bytes);

    message_str.upper_case(); // It's object-oriented! And mutable, too!


    This is using version 2.4 of the language, of course, as I'm using the new syntactic sugar to create the whole array at once instead of the 2.3 idiom of


    byte[13] message_bytes = alloc(13*sizeof(byte));

    message_bytes[0] = 'H';

    message_bytes[1] = 'e';

    message_bytes[2] = message_bytes[3] = 'l';

    message_bytes[4] = message_bytes[8] = 'o';

    // etc...



  • @Xyro said:

    @bstorer said:
    That's stupid.  Strings should be objects, but get special handling and behavior from the compiler that no object can get, even other string-like objects.  Consistency is hardly a best practice.
    I disagree, I believe consistency is key to easy learning and usage. That's why in my language, strings are constructed from byte arrays. Behold:


    byte[13] message_bytes = {'H','e','l','l','o',',',' ','w','o','r','l','d','!'};

    string message_str = new string(message_bytes);

    message_str.upper_case(); // It's object-oriented! And mutable, too!


    This is using version 2.4 of the language, of course, as I'm using the new syntactic sugar to create the whole array at once instead of the 2.3 idiom of


    byte[13] message_bytes = alloc(13*sizeof(byte));

    message_bytes[0] = 'H';

    message_bytes[1] = 'e';

    message_bytes[2] = message_bytes[3] = 'l';

    message_bytes[4] = message_bytes[8] = 'o';

    // etc...

    You forgot to null-terminate it.



  • @morbiuswilters said:

    You forgot to null-terminate it.
    Psh, what kind of archaic language do you think this is? I don't need to null terminate strings because the byte array is built with a defined (and queriable) length. The string object stores the length as one of its members so you don't have to use asinine linear algorithms for compares, searches, substrings, and so forth. That's the whole reason for using a proper string object rather than direct manipulation of the byte array (to say nothing of Unicode handling).



  • @morbiuswilters said:

    @Xyro said:

    @bstorer said:
    That's stupid.  Strings should be objects, but get special handling and behavior from the compiler that no object can get, even other string-like objects.  Consistency is hardly a best practice.
    I disagree, I believe consistency is key to easy learning and usage. That's why in my language, strings are constructed from byte arrays. Behold:
    byte[13] message_bytes = {'H','e','l','l','o',',',' ','w','o','r','l','d','!'};
    string message_str = new string(message_bytes);
    message_str.upper_case(); // It's object-oriented! And mutable, too!
    message_str.append(null.toString());

    You forgot to null-terminate it.

    Like this?



  • Just as a FYI to the person who was actually interested, but php of course also has ===, so if you want to be sure, you can be sure.



  • I'm glad I'm not a full-time programmer.



  • @BC_Programmer said:

    you don't type "183" you type:

    Integer.ONE.Multiply(Integer.TEN.Multiply(Integer.TEN)).Add(Integer.EIGHT.Multiply(Integer.TEN).Add(Integer.THREE))

    You have an extra multiply there.

    (Integer.ONE.Multiply(Integer.TEN).Add(Integer.EIGHT)).Multiply(Integer.TEN).Add(Integer.THREE)

    There, I optinomalized that for you!



  • @BC_Programmer said:

    X=new Integer((Integer.FIVE.Multiply(Y)).Divide(Integer.TWO.Subtract(Z))

    Instead of this abomination, an even better way would be a way to implement operators just like any other method. Languages allowing this that come to my mind are C++ and Smalltalk. Oh, how good were the days when I was programming in Smalltalk. No primitives. Everything was an object. Life was so much easier. No boxing or unboxing required. No abominations as above. I hated Java's guts when I had to move from Smalltalk to Java due to business reasons. It really felt like a downgrade.

    Yes, I do believe that primitives are bad and that the decision of the Java designers, not to implement operators-as-methods was bad, too.



  • @dhromed said:

     It looks like unfinished skeleton code.

    public function createSkeleton()
    {
        return new skeleton();
    }



  • @Charleh said:

    new skeleton();
     

    That's a partial class, then?



  •  Wow, I really have created a monster. It's infecting other threads!



  • @TheRider said:

    @BC_Programmer said:

    X=new Integer((Integer.FIVE.Multiply(Y)).Divide(Integer.TWO.Subtract(Z))

    Instead of this abomination, an even better way would be a way to implement operators just like any other method. Languages allowing this that come to my mind are C++ and Smalltalk. Oh, how good were the days when I was programming in Smalltalk. No primitives. Everything was an object. Life was so much easier. No boxing or unboxing required. No abominations as above. I hated Java's guts when I had to move from Smalltalk to Java due to business reasons. It really felt like a downgrade. Yes, I do believe that primitives are bad and that the decision of the Java designers, not to implement operators-as-methods was bad, too.

    I can't speak of Smalltalk as I have never programmed in it, but C++ (or C#) only has operator overloading. This means you can change what the existing operators do, but you still can't turn arbitrary methods into arbitrary operators. One language I know of that does that is Haskell. If you want to write a = b +^& c, you can have that.



  • @Abdiel said:

    I can't speak of Smalltalk as I have never programmed in it, but C++ (or C#) only has operator overloading. This means you can change what the existing operators do, but you still can't turn arbitrary methods into arbitrary operators. One language I know of that does that is Haskell. If you want to write a = b +^& c, you can have that.
    You can do that with Perl 6, too.  It's pretty awesome.  You can even define your own coercion rules. I think it goes something like

    multi sub infix:<+^&> (Num $a, Num $b) { $a * 10 + $b };
    my $answer = 4 +^& 2;
    say $
    answer; # Prints "42" to output



  • @Xyro said:

    @Abdiel said:

    I can't speak of Smalltalk as I have never programmed in it, but C++ (or C#) only has operator overloading. This means you can change what the existing operators do, but you still can't turn arbitrary methods into arbitrary operators. One language I know of that does that is Haskell. If you want to write a = b +^& c, you can have that.
    You can do that with Perl 6, too.  It's pretty awesome.  You can even define your own coercion rules. I think it goes something like

    multi sub infix:<+^&> (Num $a, Num $b) { $a * 10 + $b };
    my $answer = 4 +^& 2;
    say $
    answer; # Prints "42" to output

    Wow.  Larry Wall has lost his goddamn mind.



  • @morbiuswilters said:

    Wow.  Larry Wall has lost his goddamn mind.
    Since at least 1987.



  • @Zecc said:

    @Evo said:

    But I agree it's probably just skeleton code...
    ...currently being pushed into production.

    So instead of you know, maybe letting your colleague know about what was obviously an oversight so he can fix it, you just post it here and laugh at him?



  • @morbiuswilters said:

    @Xyro said:

    @Abdiel said:

    I can't speak of Smalltalk as I have never programmed in it, but C++ (or C#) only has operator overloading. This means you can change what the existing operators do, but you still can't turn arbitrary methods into arbitrary operators. One language I know of that does that is Haskell. If you want to write a = b +^& c, you can have that.
    You can do that with Perl 6, too.  It's pretty awesome.  You can even define your own coercion rules. I think it goes something like

    multi sub infix:<+^&> (Num $a, Num $b) { $a * 10 + $b };
    my $answer = 4 +^& 2;
    say $
    answer; # Prints "42" to output

    Wow.  Larry Wall has lost his goddamn mind.

    Good thing Perl 6 is imaginary, like a unicorn or Welbog.



  • 😞  I like Larry Wall and Perl and Perl 6



  • @Xyro said:

    😞 :</b>  I like Larry Wall and Perl and Perl 6
     

    Windows-centraled that for you.



  • @smxlong said:

    @Zecc said:

    @Evo said:

    But I agree it's probably just skeleton code...
    ...currently being pushed into production.

    So instead of you know, maybe letting your colleague know about what was obviously an oversight so he can fix it, you just post it here and laugh at him?

    He was on vacation last week, and after posting here I looked for calls to the function in the project and it seems unused.

    And if some code is obviously wrong but harmless, I prefer to laugh at it than play professor know-better-than-you.



  • @Rhaban said:

    He was on vacation last week, and after posting here I looked for calls to the function in the project and it seems unused.
    Good to know your priorities.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.