A getter with data validation that isn't either



  • um ...

    public class Accessor {

    String id;

    .
    .
    .
    

    protected void getId(Properties properties) {
    id = properties.getProperty("ID", "0");
    try {
    Integer idInt = new Integer(id);
    } catch (NumberFormatException e) {
    id = "";
    }
    if (id.trim().equals("0")) {
    id = "";
    } else {
    id = id.trim();
    }
    }
    }

    • A method named as a getter that doesn't return a value
    • A numeric integer-based ID stored as a String
    • Attempt to/create a new Integer object just to see if the provided String-based ID represents an integer and immediately discarding it
    • Repeatedly calling trim() when you could have done that before the value-checking
    • The possibility of an empty ID value (FILE_NOT_FOUND?)


  • People who drown puppies for a living have a better job than you.

    I spent the morning debugging stupid PHP written by people who shouldn't be allowed to use Twitter, let alone an IDE, and I still have a better job than you.



  • @morbiuswilters said:

    People who drown puppies for a living have a better job than you.

    I spent the morning debugging stupid PHP written by people who shouldn't be allowed to use Twitter, let alone an IDE, and I still have a better job than you.

    I appreciate those who appreciate my pain. <3



  • @zelmak said:

  • A numeric integer-based ID stored as a String
  • I bet Larry Wall would say this is no WTF.



  • Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.



  • @lolwtf said:

    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.

    Ah, a perfect example of The Gold-plating Principle: use strings instead of numbers so the Solution keeps working when you hit quadrillions. Never mind the actual cycles wasted in casting and data validation; what matters is the unlikely event that is now guaranteed not to happen.



  • @zelmak said:

    • A method named as a getter that doesn't return a value
    • A numeric integer-based ID stored as a String
    • Attempt to/create a new Integer object just to see if the provided String-based ID represents an integer and immediately discarding it
    • Repeatedly calling trim() when you could have done that before the value-checking
    • The possibility of an empty ID value (FILE_NOT_FOUND?)
    You forgot: not checking against a null value, thereby inviting our old friend NullPointerException.

     



  • @lolwtf said:

    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.
    Floating point inaccuracy? Wow! Wouldn't that be a bit far-fetched? But Integer overflows, if you want, ok.



  • @TheRider said:

    @lolwtf said:

    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.
    Floating point inaccuracy? Wow! Wouldn't that be a bit far-fetched? But Integer overflows, if you want, ok.

    Easy for you to say. How can I represent user number 1.878798734E-38 without floating point IDs?



  • @morbiuswilters said:

    @TheRider said:

    @lolwtf said:

    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.
    Floating point inaccuracy? Wow! Wouldn't that be a bit far-fetched? But Integer overflows, if you want, ok.

    Easy for you to say. How can I represent user number 1.878798734E-38 without floating point IDs?

     

    Arbitrary-precision arithmetic?

     



  • @zelmak said:

    um ...

    public class Accessor {

    String id;

    .
    .
    .
    

    protected void getId(Properties properties) {
    id = properties.getProperty("ID", "0");
    try {
    Integer idInt = new Integer(id);
    } catch (NumberFormatException e) {
    id = "";
    }
    if (id.trim().equals("0")) {
    id = "";
    } else {
    id = id.trim();
    }
    }
    }

    • A method named as a getter that doesn't return a value
    • A numeric integer-based ID stored as a String
    • Attempt to/create a new Integer object just to see if the provided String-based ID represents an integer and immediately discarding it
    • Repeatedly calling trim() when you could have done that before the value-checking
    • The possibility of an empty ID value (FILE_NOT_FOUND?)

    This is obviously an object-centric paradigm where methods are conceptually invoked BY an object, not ON them. getID() doesn't mean that you, the consumer are getting the ID, it means that the object itself is getting the ID from your property store.



  • @morbiuswilters said:

    Easy for you to say. How can I represent user number 1.878798734E-38 without floating point IDs?

    I'm fairly certain the solution should contain XML.



  • @pkmnfrk said:
    This is obviously an object-centric paradigm where methods are conceptually invoked BY an object, not ON them. getID() doesn't mean that you, the consumer are getting the ID, it means that the object itself is getting the ID from your property store.
    TRWTF is that the caller needs to provide the properties to the method as a parameter. This means that the caller already knows all those properties and could do the ID extraction itself. Instead, the getId-method should be called without a parameter, knowing implicitly where the properties in question are stored, such as in some global variable that is lazily initialized as needed. This way, there is no need for the caller to bother about them properties, and the getId() method can "magically" produce that number. Don't we all love magic?

     



  • @Speakerphone Dude said:

    @lolwtf said:
    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.

    Ah, a perfect example of The Gold-plating Principle: use strings instead of numbers so the Solution keeps working when you hit quadrillions. Never mind the actual cycles wasted in casting and data validation; what matters is the unlikely event that is now guaranteed not to happen.

    You didn't think he was serious, did you?



  • @Severity One said:

    @zelmak said:

    • A method named as a getter that doesn't return a value
    • A numeric integer-based ID stored as a String
    • Attempt to/create a new Integer object just to see if the provided String-based ID represents an integer and immediately discarding it
    • Repeatedly calling trim() when you could have done that before the value-checking
    • The possibility of an empty ID value (FILE_NOT_FOUND?)

    You forgot: not checking against a null value, thereby inviting our old friend NullPointerException.

    Actually, since the default value of id is "0", I don't see where a NullPointerException can creep in ... oops ... unless properties is null ...



  • @C-Octothorpe said:

    @Speakerphone Dude said:
    @lolwtf said:
    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.

    Ah, a perfect example of The Gold-plating Principle: use strings instead of numbers so the Solution keeps working when you hit quadrillions. Never mind the actual cycles wasted in casting and data validation; what matters is the unlikely event that is now guaranteed not to happen.

    You didn't think he was serious, did you?

    Based on the non-photo and the fact that name.Contains("lol") I was thinking it was not a He. This being said, that remains a perfect example.



  • @Speakerphone Dude said:

    @lolwtf said:
    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.

    Ah, a perfect example of The Gold-plating Principle: use strings instead of numbers so the Solution keeps working when you hit quadrillions. Never mind the actual cycles wasted in casting and data validation; what matters is the unlikely event that is now guaranteed not to happen.

    Please show the perofmance specification that will be violated (or dangerously close) due to "actual cycles wasted in casting and data validation". If you dont have a spec, then you dont have a requirement, and if it isnt required then it done matter.



  • @TheCPUWizard said:

    @Speakerphone Dude said:

    @lolwtf said:
    Just you wait until your IDs get up into the quadrillions and you're getting incorrect IDs due to floating point inaccuracy... you'll be glad you used a string instead, that can contain all the digits.

    Ah, a perfect example of The Gold-plating Principle: use strings instead of numbers so the Solution keeps working when you hit quadrillions. Never mind the actual cycles wasted in casting and data validation; what matters is the unlikely event that is now guaranteed not to happen.

    Please show the perofmance specification that will be violated (or dangerously close) due to "actual cycles wasted in casting and data validation". If you dont have a spec, then you dont have a requirement, and if it isnt required then it done matter.

    In this situation, there are obvious wasted cycles in the conversion/validation steps required to reliably store numbers in strings because the design is driven by an unlikely scenario (hitting the quadrillions) compared to using a numeric data type.

    In most organization there would be no need to provide detailed specifications for this type of situation because guiding principles and design/implementation patterns should be in place to provide guidance. If the organization adheres to a specific framework of process improvement (Six Sigma, Lean, CMMI, etc.) this type of decision would also be influenced by principles such as avoiding over-production and over-processing (i.e.: waste).

    Are you really disputing this or are you just looking to start a lengthy discussion?


Log in to reply
 

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