• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

preventing brittle code

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone share their thoughts on why use of the following statement leads to brittle code?
When is this appropriate? (Reflection perhaps?)

this.getClass().getName()

(I had used it in one of the assignments)

Thank you.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's brittle because if the name of a class changes, the code breaks -- although it probably still compiles. You find out that the program is broken only when you run it (if you're lucky; you might not find out until a CUSTOMER runs it, which is far worse.)

Generally, brittle code is code that depends on details that shouldnt really matter, especially in ways so that changing one thing breaks a number of really unrelated things.
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you want to change the string: do you change your class name?

Suppose you want to change the class name: will you remember to change your string? Or will your program still work with the new string?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

EFH wrote:Well, it's brittle because if the name of a class changes, the code breaks



"Breaks" in what sense?" It changes, sure. But I had assumed that the intent of the code was to provide the correct class name. Even if the class name changes. Because, after all, that's what the code says to do. So how is that broken? Should we assume the coder was trying to do someting else entirely? Based on what?

paul wheaton wrote:Suppose you want to change the string: do you change your class name?


I would think that depends on the intent of your change. Do you want to change the string, or do you want to change the class name? Is the string intended to reflect the class name, or not? This goes back to my previous comments. To me it seems perfectly reasonable to want to, say, log the actual name of the class that was executing this method. Which this does.

PW wrote:Suppose you want to change the class name: will you remember to change your string? Or will your program still work with the new string?


From what we've seen so far, the original intent of the program was probably to show the actual class name. The string will change automatically in reponse to the change of the class name. And this seems like a good thing. What's the problem?
 
paul wheaton
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From what we've seen so far, the original intent of the program was probably to show the actual class name.



And that's where I have advantage over you. BWA HA HA HA HA HA (cough cough) .......

So this is related to an assignment, which we are all trying to not directly discuss.

But I happen to have been the nitpicker for this assignment where it was used.

Suppose you have a shopping cart full of groceries and the user asks for a list of the stuff in the basket. You have a class called Cheese which contains info on variety of cheese, how stinky the cheese is and other interesting cheesy attributes. When asked to list the stuff in the cart, do you report the class name, or would that make the application brittle?




 
Jeremy Medford
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am beginning to understand why the use of this makes for brittle code.

I didn't see this mentioned, but can somebody speak to the question of when its appropriate to use this method?

Thanks.
 
paul wheaton
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have to say it is very, very rare.

I know that some people use it for some logging stuff, but the uses I've seen seem stupid to me.

I'm sitting here trying to think of a case ... something to do with debugging or logging something for a developer ... but I cannot help but think all of the exception stuff takes care of any need for that sort of thing.

If nothing else, use of this sort would be rather advanced. I probably wouldn't use it unless there was no other way.

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use it for logging warnings ... in case the customer asks why the "City Name" field is blank in the report ... or whatever ... without throwing an exception and killing the processing of all the other records that follow the one with the blank field (left blank by the customer, not something controlled by my application). This seems to save time particularly in the case where I have subclasses and I want to know which subclass called the shared (superclass) code. An alternative would be to hard code the class name in the logging statement, but then you would need to remember to change the statement (or the String which is assigned to the String variable which holds the class name for multiple logging statements in the same class) if you changed the name of the class.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic