Is there an anti-pattern name for constructor abuse?



  •  I keep seeing this a lot. A lot of code goes into a constructor that does everything. The object itself is just a throw away because the coder doesn't know what the static keyword is. I see code that is simply: new MyClass();, it might be stored to a variable and never used. E.g.

     public class MyClass {

       public static void main(String[] args) {

          new MyClass();

       }

       public void MyClass() {

          //Do stuff for main

       }

    }

     

    really, this is worse than just have a lot of static variables.



  • Doesn't that also reserve memory for every member the main class has twice?



  • @Lingerance said:

    Doesn't that also reserve memory for every member the main class has twice?
    Although I don't know for sure, I think it is only static members which are allocated if a main function is static.



  • @subanark said:

     I keep seeing this a lot. A lot of code goes into a constructor that does everything. The object itself is just a throw away because the coder doesn't know what the static keyword is. I see code that is simply: new MyClass();, it might be stored to a variable and never used. E.g.

     public class MyClass {

       public static void main(String[ args) {

          new MyClass();

       }

       public void MyClass() {

          //Do stuff for main

       }

    }

     

    really, this is worse than just have a lot of static variables.

     

     

    It looks to me like that main() method is generated to aid in debugging.  This is a common thing for building GUI dialogs so that you can launch just a single dialog without going through the application.  In that case, the stuff in the constructor is important for when the class is used within the system (which is the eventual goal).



  • I don't know, but any time I read the word "anti-pattern," I think about a guy I used to work with who used that term in a positive way, with a meaning somewhat analagous to "anti-hero." He would walk into my office extolling the virtues of some heavy-handed architecture. Then, after I tried (effectively, I thought) to poke holes in it, he would say something like "that's exactly the kind of narrow-minded pessimism my pattern is fighting against. It's an anti-pattern!!"



  • What the OP is talking about is using the single class for an entire application, and simply instanciating the class for main().


Log in to reply
 

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