I bring you all another gem from my computer science course, this time from my OOP class.

The first assignment we made for this class was a simple CLI shop, where you would have basically three main classes:

- A Product class that you extend to create different types of products.
- A Cart class that manages a list of products (basically an ArrayList<Product>) and has some useful methods
- A CLI class to display a simple interface to the user and call methods on a Cart.

Basic OOP stuff, so far so good.

Then for our second assignment the teacher asked us to make Cart a generic class, where you would say Cart<Bagel> and you would only be able to put bagels in it. This makes absolutely no fucking sense, this is not a good use case for generic types since

1) you would never limit your customer's cart to one type of product at compile time.
2) in Cart, you have to cast the generic type to Product to extract any information from the product, like when getting product prices to calculate the total price, so might as well use a fucking ArrayList<Product>

I'm just saying what he's asking us to do has (to our fictional shop's business logic) absolutely no advantage over subtyping.

Also, why the fuck teach generic constraints when you can just tell your students "just cast T to Product", right?

Like fucking hell, couldn't you spend like 10min to come up with a decent assignment that actually teaches generic types the right way? ffs

And just so no one can say "but wut simple assignment would you give to teach students generic types?", here's a simple and much, much better alternative: implementing your own ArrayList. Done. Can't get much better than that, it's a legit use case and teaches you the basics.

Sorry man, you're a great person, you really are, but you suck as a teacher.

  • 5
    And if you say that coherent business logic is not needed here because he's just aiming to teach the basics I'll fucking strangle you to death.

    Coherent business logic IS important so that the student understands the use cases for whatever you're teaching and also shows the advantages of X over Y for use cases like Z.

    My classmates all think this is bullshit too, even the absolute newbs, because they see that using generic types here has no advantage. I just hope they don't dismiss generics as "subtyping but worse"...
  • 2
    The generic cart should take
    products, coupons, and combos (groups of products with metadata)and work correctly with all of them. That’s a better generic, and a very common real world case.

    I also really like your example of reimplementing ArrayList. Everyone should do that for their languages’ sugary features so they understand how they work and why they are implemented the way they are.
  • 0
    @Root I'm not sure I understand what you mean. Why make them generic parameters? That way you wouldn't be able to create new coupons and combos at runtime, you'd have to recompile the program.
Add Comment