The Intern



  • Our team hired an intern - mostly to save costs - with the intent of training him into a useful member of the team. This guy had just graduated, but claimed to have done several significant projects while in school. Ok, we didn't expect much, but what we got boggled the mind...

    As part of a routine code review, we found something he tried to integrate into the codebase:

    class Variables {
        public static int        classA_var1; // variable names annonymized
        public static String     classA_var2;
        public static double     classA_var3;
        ...
        public static Date       classB_var1;
        public static BigInteger classB_var2;
        ...
        // ~8,000 more lines of this
      }
    

    class A {
    public void someMethod() {
    Variables.classA_var1 = Variables.classB.var2;
    }
    }

    class B {
    public void someMethod() {
    Variables.classB_var1 = new Date();
    }
    }

    //...

    Pray tell, why did you make every single variable public static in what is effectively a class of globals?

    Because I needed to be able to change a private variable in one class from the other class, and Java only lets you pass variables by value.

    I weep for this industry.



  • The problem is:

    Those who can, do; those who can't, teach

     

     

    At my workplace, I am the go to guy when it comes to Java/Object-Oriented, but you couldn't tell that from my university records: I almost failed intro to OOP class.

    Example of things I did on my project and exam:

    public SomeClass clone() {
      ...
      copy.stringField = this.stringField;

     

    What the professor wanted:

      copy.stringField = new String(this.stringField);

     

    I don't know how it is in the US, but here in Portugal we have a shortage of competent professors and TA's aware that teaching computer programming is more than explaining the rules of the language.



  • Wow... and sometimes I worry about losing my job to younger programmers... ha!



  • I don't get it...The professor is correct for many languages. You don't specify whether this was Java (like you are the "go-to" guy for) or some other language like C++...Where this would provide an appropriate deep-copy.


  • Considered Harmful

    @Auction_God said:

    I don't get it...The professor is correct for many languages. You don't specify whether this was Java (like you are the "go-to" guy for) or some other language like C++...Where this would provide an appropriate deep-copy.

    The difference is that in university the professor catches the subtle bug and takes points off, where in an enterprise environment no one notices and raises are given.



  • @Auction_God said:

    I don't get it...The professor is correct for many languages. You don't specify whether this was Java (like you are the "go-to" guy for) or some other language like C++...Where this would provide an appropriate deep-copy.
     

     

    Sorry, my bad. The course was Java only. When defending the project, I explained that Java String objects are immutable, but the TA insisted that I need to deep clone the String object otherwise "some other class will change its value" and the professor sided with the TA.

    On the paper-written exam I was deducted points for writting the opening curly brace on the same line as the method declaration/if statement.

    Feel free to take your conclusions...



  •  What's a TA?


  • Discourse touched me in a no-no place

    @dhromed said:

     What's a TA?

    From the context I'm guessing 'teaching assistant.' I get the general impression it tends to be an older/graduated student seconded to the class, that is supposed to know better.



  • @PJH said:

    @dhromed said:

     What's a TA?

    From the context I'm guessing 'teaching assistant.' I get the general impression it tends to be an older/graduated student seconded to the class, that is supposed to know better.

    Yeah, TA is teaching assistant.  Most of the time they are a grad student, though sometimes for entry level classes they are undergraduates in their last year.  Sometimes they are just stuck correcting work or running help sessions before tests, but some schools use the grad student TAs to actually do the lectures for undergrad classes.



  • @snoofle said:

    This guy had just graduated

    In my experience, guys who have just graduated are most often considerably better than people with anywhere between 3 and 18 months of experience. They haven't been ruined by a culture of mediocrity. My most successful project to date consisted of me (Tech Lead), a PM, and three fresh graduates who've never written a line of code outside of school. Obviously, you expect them to be a bit less productive in the first couple of months, but if they're provided ample time to study and enough guidance (I spent roughly 50% of my time in that project going between them and helping them wherever they were stuck), and of course proper code reviews, they can do a really good job.



  • @edgsousa said:

    Feel free to take your conclusions...

    1. Your professor was a pedant with a 'my way or the highway' attitude,
    2. the TA had been indoctrinated by him,
    3. both were covering each other's ass and
    4. you should've put in a formal request for a second opinion.

    (Also, if the university does not give you the quite fundamental right to a second opinion, then WTF were you doing there?)



  • @configurator said:

    @snoofle said:
    This guy had just graduated
    In my experience, guys who have just graduated are most often considerably better than people with anywhere between 3 and 18 months of experience. They haven't been ruined by a culture of mediocrity. My most successful project to date consisted of me (Tech Lead), a PM, and three fresh graduates who've never written a line of code outside of school. Obviously, you expect them to be a bit less productive in the first couple of months, but if they're provided ample time to study and enough guidance (I spent roughly 50% of my time in that project going between them and helping them wherever they were stuck), and of course proper code reviews, they can do a really good job.

    And you created a (small) cult of programmers who follow your methodology?



  • In a Gaming/Simulation course (c programming), whenever my Instructor gave us code to work from, he'd make sure it's the worst code possible, with ineffiecencies, every variable global, failing to compile, and logic errors.

    His reason behind this is that he wanted us to know what it would be like in a worst case scenario in a Business Environment, and that understanding/refactoring the existing code is more important then just building new code blindly.

    At first I thought he was just lazy, but in the end I sort of agreed with the technique.

    Problem was, the people who slept through the first year and only started paying attention in the second year started learning...



  • I'm demonstrating my technical ignorance, but as someone who has just graduated and have just noticed myself
    writing nearly exactly that above code style, what should I be doing instead? I have a list of variables that I need to access from other classes.

    So I've just been setting the variables as 'public static final VAR'
    and accessing them with CLASSNAME.VAR.
    What is a better way to do this? Or, alternatively, should I just stop trying to use globals and stick with locals and calling methods.



  • @Auction_God said:

    I don't get it...The professor is correct for many languages.

    Any language where it is correct should be immediately forgotten. Mutable reference strings are a huge can of worms. @Auction_God said:

    …some other language like C++…Where this would provide an appropriate deep-copy.

    In C++, std::string is a value type. No new is, nor should be, involved. @Auction_God said:

    You don't specify whether this was Java…

    And C++ does not use .clone() methods. It uses copy-constructor and operator=. And C# uses .Clone(). Any other language is unlikely. So it must have been Java.


  • Discourse touched me in a no-no place

    @Bulb said:

    And C++ does not use .clone() methods.
    Mummy, someone's wrong on the internet again!!!



  • @nic said:

    I'm demonstrating my technical ignorance, but as someone who has just graduated and have just noticed myself writing nearly exactly that above code style, what should I be doing instead? I have a list of variables that I need to access from other classes.

    So I've just been setting the variables as 'public static final VAR'
    and accessing them with CLASSNAME.VAR.
    What is a better way to do this? Or, alternatively, should I just stop trying to use globals and stick with locals and calling methods.

    It's a bit hard to discuss this in abstract, but when you say you've got "variables that need to be shared between A and B", what you most likely have are either properties of A, properties of B or even properties of some other object C that gets passed around.

    There is no shame in creating a Context / Request / MyUseCase / StuffINeedForDoingThisThing class and storing your "variables" there, but you should instance it and pass it among the objects cooperating on your task. That is, as long as you don't get nuts and turn it into an EverythingContainer. You should remember the Single Responsibility Principle and create as many classes as necessary to structure your information cohesively.

    In the case of the OP, the guy should either create public setters in classes A and B; or pass a Variables instance to A#someMethod and B#someMethod. Judging by the names of the properties, the former.

    EDIT BEFORE EVEN POSTING: your case is different. Your variables... aren't. They're constants. They're final. And they are declared inside the classes they belong. You're doing it right.

     



  • @nic said:

    I'm demonstrating my technical ignorance, but as someone who has just graduated and have just noticed myself writing nearly exactly that above code style, what should I be doing instead? I have a list of variables that I need to access from other classes. So I've just been setting the variables as 'public static final VAR' and accessing them with CLASSNAME.VAR. What is a better way to do this? Or, alternatively, should I just stop trying to use globals and stick with locals and calling methods.
    Depends.

     If the variable is associated with an object/class then it should be inside that class and thus not static (unless it is a constant).  If you seem to have a lot of variables that are not associated with any defined class, then you need to ask yourself if you are missing classes.  As for public, protected, and private it comes down to what should be allowed access to the variable.  You should take advantage of get and set functions (how they are done varies by language, and some languages have special support to make them slicker).  In my case I noticed that in my classes that variables used for storing data tend to be public while variables used for functionality tend to be private.

    Note that exceptions to this do apply.



  • @snoofle said:

    I weep for this industry.
     

    Since you're apparently stuck using Java, I'll weep right along with you.

     


Log in to reply