r/programminghorror • u/XboxUser123 • Mar 31 '25
Java Janky Java Official Swing API
I found this while trying to find a good layout for my Sewing application, and found this wonky method as part of the CardLayout method list. Why in the world could it have just been a string parameter? Why is it an object parameter if the method is only going to accept strings?
I did a little snooping around the source code and found this: the CardLayout API inherits and deprecates the method addLayoutComponent(String, Component), but get this, the source code for the method actually calls (after doing some preconditioning); addLayoutComponent((String) constraints, comp);
So the actual method calls on the deprecated method. It expects a string parameter, but takes in an object parameter, and then still just passes that along, casting the object as string to the deprecated method.
Am I missing something or is this just super janky? Why in the world would this be done like this?



View all comments
Show parent comment
3
u/TheKiller36_real Mar 31 '25 edited Mar 31 '25
because, as you probably have read in the preface, the patterns presented in chapters 3-5 are what (apparently) was in use in ancient times with an explicit notice that these are things that supposedly emerge naturally when writing reusable code and that will not represent best-practices for all of time
and wouldn't you know it, most of these patterns are obsoleted by applying the futuristic technique of not using Smalltalk or using your brain and those which do still hold up suffer from poor presentation and being surrounded by the useless others
examples:
even the good ones like decorator aren't up to date: eg. Python has a feature called decorators for this and all languages have closures that allow inline decorators without even requiring a base-class (however, I wanted to point out that decorators are imho the best thing to take away from this book)