Why pass parameters?



  • Found code like this:

    public class wtfclass {

     public static wtfclass thisClass = null;
    

    public void wtfclass() {
    thisClass = this;
    }


    }

    Don't need to pass parameters, everything can just get access through the static. No, this is not a singleton.



  • Kinda looks like they were trying to make it one, though... and just didn't really know what they were doing.



  • I did not even know that you could create a public object of a class inside of itself even if you made it a static.  I would have thought it would throw some kind of compile error about circular or recursive referencing.



  • @CodeNinja said:

    Kinda looks like they were trying to make it one, though... and just didn't really know what they were doing.
    This.



  • It looks like it was designed to return the last instance of wtfclass that had it's member method wtfclass called.
    Here I made it self documenting...
    -.-

    public class Wtf {
     public static Wtf lastTackWtf = null;
     
     public void trackWtf() {
      lastTackWtf = this;
     }    
    }


  • I'd have guessed that somebody doesn't know how to use the "this" keyword... Except that he used it.

    @Anketam said:

    I did not even know that you could create a public object of a class inside of itself even if you made it a static.  I would have thought it would throw some kind of compile error about circular or recursive referencing.

     

    Why? That's not C++, what goes inside your class is just a pointer, it can point to anything.

     


  • Discourse touched me in a no-no place

    @Mcoder said:

    @Anketam said:
    I did not even know that you could create a public object of a class inside
    of itself even if you made it a static.  I would have thought it would
    throw some kind of compile error about circular or
    recursive referencing.

    Why? That's not C++,
    Hint: mention the language when you're posting short snippets without context.



    I thought exactly the same thing on a quick look at the code.



    It was only after a second scan of the code after your post that I realised it couldn't be C++. ('public' before 'class', 'null' instead of 'NULL' or '0', lack of colons etc. - things easily missed in such situations unlike the lack of a * if it were C++.)



  • @Mcoder said:

    I'd have guessed that somebody doesn't know how to use the "this" keyword... Except that he used it.

    @Anketam said:

    I did not even know that you could create a public object of a class inside of itself even if you made it a static.  I would have thought it would throw some kind of compile error about circular or recursive referencing.

     

    Why? That's not C++, what goes inside your class is just a pointer, it can point to anything.

     

     

    Even if it is C++, it would still work without problem - static class members are simply static-allocated variables, therefore they don't count towards the storage of the class. This is one way to implement the singleton pattern, actually. And it's all peachy until one singleton touches another singleton in its constructor and it all works, because the compiler put the initialisation of the first after the second, but then you rearrange something in the build system, or just add some other unrelated static variable in a different compilation unit and then their order of initialisation gets swapped and your program segfaults before it even reaches main(). Fun times.

     



  • @PJH said:

    It was only after a second scan of the code after your post that I realised it couldn't be C++. ('public' before 'class', 'null' instead of 'NULL' or '0', lack of colons etc. - things easily missed in such situations unlike the lack of a * if it were C++.)

    I bet even Lisp looks like C++ to you. Everything about that snippet except the lowercase class name screams Java.


  • Discourse touched me in a no-no place

    @Mo6eB said:

    Even if it is C++, it would still work without problem - static class members are simply static-allocated variables, therefore they don't count towards the storage of the class.
    Your compiler is broken if it's accepting that sort of thing:

    [pjh@sofa tmp]$ cat -n x.cpp
         1  class foo{
         2  public:
         3          static class foo;
         4  };
         5
         6  int main(void){
         7          return 0;
         8  }
    [pjh@sofa tmp]$ gcc x.cpp -o x -Wall
    x.cpp:3:15: error: ‘foo::foo’ has the same name as the class in which it is declared
    x.cpp:3:15: error: a storage class can only be specified for objects and functions
    


  • @PJH said:

    @Mo6eB said:
    Even if it is C++, it would still work without problem - static class members are simply static-allocated variables, therefore they don't count towards the storage of the class.
    Your compiler is broken if it's accepting that sort of thing:

    [pjh@sofa tmp]$ cat -n x.cpp
         1  class foo{
         2  public:
         3          static class foo;
         4  };
         5
         6  int main(void){
         7          return 0;
         8  }
    [pjh@sofa tmp]$ gcc x.cpp -o x -Wall
    x.cpp:3:15: error: ‘foo::foo’ has the same name as the class in which it is declared
    x.cpp:3:15: error: a storage class can only be specified for objects and functions
    

    Uh, yeah, that's obviously bad C++. But:

    class foo{
    public:
              static foo bar;
    };
    
    int main(void){
              return 0;
    }
    
    $ gcc x.cpp -o x -Wall
    $ 
    

    ...works just fine.


  • Discourse touched me in a no-no place

    @boomzilla said:

    static foo bar;
    Doh!



  •  A foo walks into a bar.

     The barman asks "what kind of example are YOU trying to set?"



  • @Cassidy said:

     A foo walks into a bar.

     The barman asks "what kind of example are YOU trying to set?"

    A Turkish horse walks into a bar. The barman asks "Why the long fez"?

    (and then they started arguing about lowercase letters)



  • @Speakerphone Dude said:

    @Cassidy said:
    A foo walks into a bar.

    The barman asks "what kind of example are YOU trying to set?"

    A Turkish horse walks into a bar. The barman asks "Why the long fez"?

    (and then they started arguing about lowercase letters)

    A brit, a scot and an irishman walk into a bar.

    The barman says "Oh no, not you guys again!"



  • @Anketam said:

    I did not even know that you could create a public object of a class inside of itself even if you made it a static.  I would have thought it would throw some kind of compile error about circular or recursive referencing.
    That's one way to create singletons in Java.

    public class Woof
    {
    private final static Woof INSTANCE = new Woof();

    private Woof()
    {
        // perform initialisation
    }
    
    public static Woof getInstance()
    {
        return INSTANCE;
    }
    

    }

    Please note the private constructor.



  • @JoeCool said:

    Found code like this:

    public class wtfclass {
    public static wtfclass thisClass = null;

    public void wtfclass() {
    thisClass = this;
    }
    }


    Don't need to pass parameters, everything can just get access through the static. No, this is not a singleton.

    I don't know what this has to do with parameters; actually, to me it looks more like a way to avoid return values...



  • @Anonymouse said:

    @Speakerphone Dude said:

    @Cassidy said:
    A foo walks into a bar.

    The barman asks "what kind of example are YOU trying to set?"

    A Turkish horse walks into a bar. The barman asks "Why the long fez"?

    (and then they started arguing about lowercase letters)

    A brit, a scot and an irishman walk into a bar.

    The barman says "Oh no, not you guys again!"

     

    Three blind mice walk into a bar, but to derive humor from them would be exploitative.

     

     



  • @Anonymouse said:

    @JoeCool said:

    Found code like this:

    public class wtfclass {
    public static wtfclass thisClass = null;

    public void wtfclass() {
    thisClass = this;
    }
    }


    Don't need to pass parameters, everything can just get access through the static. No, this is not a singleton.

    I don't know what this has to do with parameters; actually, to me it looks more like a way to avoid return values...

    He is using it to glabally get access to the object instead of passing the object to whatever methods need access. What the hell does it have to do with return values? He doesn't just use the object in one place.



  • @JoeCool said:

    What the hell does it have to do with return values? He doesn't just use the object in one place.
    Oh, now I see it, it was not about the "wtfclass" methods avoiding parameters, it was about other classes avoiding having a "wtfclass" parameter in their methods.

    I was originally reading into this that the original coder was somehow scared of using a return statement... you know, kind of like

    public class WtfClass {
      public static int result;
    
      public void WtfCalc(int a, int b) {
        result = a + b;
      }
    }

    ... doesn't make much sense, I admit, but WTFs rarely do.



  • @PJH said:

    @Mo6eB said:
    Even if it is C++, it would still work without problem - static class members are simply static-allocated variables, therefore they don't count towards the storage of the class.
    Your compiler is broken if it's accepting that sort of thing:
    [pjh@sofa tmp]$ cat -n x.cpp
    1 class foo{
    2 public:
    3 static class foo;
    4 };
    5
    6 int main(void){
    7 return 0;
    8 }
    [pjh@sofa tmp]$ gcc x.cpp -o x -Wall
    x.cpp:3:15: error: ‘foo::foo’ has the same name as the class in which it is declared
    x.cpp:3:15: error: a storage class can only be specified for objects and functions

    The compiler looks at the declaration on line 3 as a nested class. It then complains that:

    - It can't have the same name as the other class because for any class T, T::T is a constructor, not a type.

    - You can't declare a nested class static, you can only use that term for data and function members of the class.

    The compiler is right of course that these are both errors, but sometimes they could have a nicer way of explaining what you did wrong.

    e.g. "string" is not a member of namespace "std". Of course it is... it should tell you "you forgot to include the header for std::string". Of course having to include headers explicitly for the standard library in the source file is a WTF, it should be a general build setting, but there you go.

     

     

     


  • Discourse touched me in a no-no place

    @Cbuttius said:

    @PJH said:

    <code>

    As pointed out by boomzilla, my example code was wrong.



  • @Cbuttius said:

    e.g. "string" is not a member of namespace "std". Of course it is... it should tell you "you forgot to include the header for std::string". Of course having to include headers explicitly for the standard library in the source file is a WTF, it should be a general build setting, but there you go.

    I get why you say this, and I agree that the messages could be improved, but you are wrong in your proposed solutions. I'm not sure what you mean by a "general build setting" in this context.



  • @PJH said:

    @Mo6eB said:
    Even if it is C++, it would still work without problem - static class members are simply static-allocated variables, therefore they don't count towards the storage of the class.
    Your compiler is broken if it's accepting that sort of thing:

    [pjh@sofa tmp]$ cat -n x.cpp
         1  class foo{
         2  public:
         3          static class foo;
         4  };
         5
         6  int main(void){
         7          return 0;
         8  }
    

    Yeah, only Basic compilers are clever enough to support line numbers. Pro tip of the week: good Basic programmers use increments of 10 in the line numbers just in case there is a need to insert a GOTO somewhere in the future.



  • @Speakerphone Dude said:

    @PJH said:
    $ cat -n

    Yeah, only Basic compilers are clever enough to support line numbers. Pro tip of the week: good Basic programmers use increments of 10 in the line numbers just in case there is a need to insert a GOTO somewhere in the future.

    wat


  • @Ben L. said:

    @Speakerphone Dude said:
    @PJH said:
    t
    wat

    ^^^
    this is called "mashup insults"



  • @Speakerphone Dude said:

    @Ben L. said:
    @Speakerphone Dude said:
    @PJH said:
    t
    at

    ^^^
    is is cad "maup lts"



  • @Ben L. said:

    @Speakerphone Dude said:
    @Ben L. said:
    @Speakerphone Dude said:
    @PJH said:
    R
    ET

    ARD

    Misquoting out of context: I think I'm ready for my internship at Fox news



  • @Speakerphone Dude said:

    Misquoting out of context: I think I'm ready for my internship at Fox news

    LEAN FORWARD COMRADE!


  • Discourse touched me in a no-no place

    @Speakerphone Dude said:

    @PJH said:
    [...]
    cat -n x.cpp
    [...]

    Yeah, only Basic compilers are clever enough to support line numbers.

    You're an idiot.


  • @PJH said:

    @Speakerphone Dude said:
    @PJH said:
    [...]
    can pp
    [...]

    Yeah, only sic com are lever no sup linbers.

    Your an idiot
    M id t?

  • Discourse touched me in a no-no place

    @Ben L. said:

    @PJH said:
    @Speakerphone Dude said:
    @PJH said:
    [...]
    can pp
    [...]

    Yeah, only sic com are lever no sup linbers.

    Your an idiot
    Strange you should do that - I did actually contemplate writing that in a fit of irony, but assumed it would be taken the wrong way.


  • My guess, having seen slightly less WTF versions, would be that it was someone who couldn't work out how to access the enclosing instance from an inner class.



  • @pjt33 said:

    My
     

    I only just noticed.. your avatar has a...  thing... on its neck.



  • @PJH said:

    @Speakerphone Dude said:
    @PJH said:
    [...]
    cat -n x.cpp
    [...]

    Yeah, only Basic compilers are clever enough to support line numbers.

    Your an idhiot.

    Says the guy who can't spell



  • @Speakerphone Dude said:

    @PJH said:
    @Speakerphone Dude said:
    @PJH said:
    Ben L. should install Gentoo Linux.
    Potatoes
    I sure do like potatoes.
    Yes, let us talk about potatoes.
    Potatoes are sometimes mashed.



  • @PJH said:

    Ben L. should install Gentoo Linux.

    Gentoo Linux??? Did Emmett Brown finally connected his time machine to the water supply and got us all back to 2004?


  • Discourse touched me in a no-no place

    @Speakerphone Dude said:

    @PJH said:
    @Speakerphone Dude said:
    @PJH said:
    [...]
    cat -n x.cpp
    [...]

    Yeah, only Basic compilers are clever enough to support line numbers.

    Your an idhiot.

    Says the guy who can't spell

    You're still an idiot.


  • @PJH said:

    @Speakerphone Dude said:
    @PJH said:
    @Speakerphone Dude said:
    @PJH said:
    [...]
    cat -n x.cpp
    [...]

    Yeah, only Basic compilers are clever enough to support line numbers.

    Your an idhiot.

    Says the guy who can't spell

    It appears that I am the idiot after all so I apologize. If I send you a mix tape and naked pictures of my sister can we be BFF?

    I already have pictures of your sister, they are posted in the preview area on uglynakedpeople.com



  • @Speakerphone Dude said:

    @PJH said:
    @Speakerphone Dude said:
    @PJH said:
    @Speakerphone Dude said:
    @PJH said:
    Misquoted post

    Misquoted post

    Misquoted post

    Misquoted post

    Misquoted post

    Misquoted post

    Misquoted post



  •  Ben L are you drunk.



  • @dhromed said:

     Ben L you are an aspie.

    FTFY


  • Discourse touched me in a no-no place

    @Speakerphone Dude said:

    they are posted in the preview area on uglynakedpeople.com
    Still being an idiot I see.


    [pjh@sofa ~]$ nslookup uglynakedpeople.com
    ;; Got SERVFAIL reply from 194.168.4.100, trying next server
    ;; Got SERVFAIL reply from 194.168.4.100, trying next server
    Server:         194.168.8.100
    Address:        194.168.8.100#53
    
    ** server can't find uglynakedpeople.com: SERVFAIL
    
    [pjh@sofa ~]$
    


    You just can't help it, can you?


  • Dammit.... my nameserver stopped responding again?



  • @PJH said:

    @Speakerphone Dude said:
    they are posted in the preview area on uglynakedpeople.com
    Still being an idiot I see.


    [pjh@sofa ~]$ nslookup uglynakedpeople.com
    ;; Got SERVFAIL reply from 194.168.4.100, trying next server
    ;; Got SERVFAIL reply from 194.168.4.100, trying next server
    Server:         194.168.8.100
    Address:        194.168.8.100#53
    
    ** server can't find uglynakedpeople.com: SERVFAIL
    
    [pjh@sofa ~]$
    


    You just can't help me, can you?

    I find it very disturbing that you want me to help you find naked pictures of your sister. Does she know about it? Is she by any chance looking over your shoulder as you type this as part of a sick incestuous game? Or is it a case of multiple personalities and you are in fact both PJH and PJH's sister?



  • @Speakerphone Dude said:

    @PJH said:
    @Speakerphone Dude said:
    they are posted in the preview area on uglynakedpeople.com
    Still being an idiot I see.


    [pjh@sofa ~]$ nslookup uglynakedpeople.com
    ;; Got SERVFAIL reply from 194.168.4.100, trying next server
    ;; Got SERVFAIL reply from 194.168.4.100, trying next server
    Server:         194.168.8.100
    Address:        194.168.8.100#53
    
    ** server can't find uglynakedpeople.com: SERVFAIL
    
    [pjh@sofa ~]$
    


    You just can't help me, can you?

    I find it very disturbing that you want me to help you find naked pictures of your sister. Does she know about it? Is she by any chance looking over your shoulder as you type this as part of a sick incestuous game? Or is it a case of multiple personalities and you are in fact both PJH and PJH's sister?

    What if this PJH is a female clone of the other PJH that works for the CIA and wants to find out if there are enough grounds to nuke insert country name here and --oh wait the CIA isn't that smar


  • @Cassidy said:

    Dammit.... my nameserver stopped responding again?

    [cassidy@uglynakedpeople.com ~]$ nslookup uglynakedpeople.com
    ;; Got SERVFAIL reply from 127.0.0.1, trying next server
    ;; Got SERVFAIL reply from 127.0.0.1, trying next server
    Server:         127.0.0.1
    Address:        127.0.0.1#53
    
    ** server can't find uglynakedpeople.com: SERVFAIL
    
    [cassidy@uglynakedpeople.com ~]$
    

    Maybe you should not use your web server to browse internet.



  • @Ben L. said:

    What if this PJH is a female clone of the other PJH that works for the CIA

    PJH-lias is hot!



  • @Speakerphone Dude said:

    Maybe you should not use your web server to browse internet.
     

    Why? Everyone else apparently uses my server when they browse internet...



  • @Severity One said:

    @Anketam said:

    I did not even know that you could create a public object of a class inside of itself even if you made it a static.  I would have thought it would throw some kind of compile error about circular or recursive referencing.
    That's one way to create singletons in Java.

    public class Woof
    {
    private final static Woof INSTANCE = new Woof();

    private Woof()
    {
        // perform initialisation
    }
    
    public static Woof getInstance()
    {
        return INSTANCE;
    }
    

    }

    Please note the private constructor.

     

    I learned something closer to this:

    public class Woof
    {
    private static Woof instance;

    private Woof()
    {
        // perform initialisation
    }
    
    public static Woof getInstance()
    {
        if (instance == null)<br>        {<br>            instance = new Woof();<br>&nbsp;       }<br><br>&nbsp;       return instance;<br>    }
    

    }

    This way is more explicit in when the instance will be created, so we can be sure that any components that Woof relies on are loaded before we call getInstance(). (Yes, I know Java won't initialize it until first reference, but that's confusing.) It's just a personal preference thing though.


Log in to reply
 

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