• 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

Trim spaces?

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When reading or writing records back to the data file did you guys trim the strings to remove whitespaces?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When records are added to the record cache the field values are trimmed. If you write records back to the file, why would you trim them if you pad the field with spaces?
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Roel. The specific case I was pondering about was what if someone sent a record in to my business class where the field was a string that exceeded the schema.

I too am trimming strings when I load the cache. I am also trimming strings when converting my transfer object to a String[] array.

In my setup, someone could set a field that was too long, but because of my trimming in converting a transfer object to a String[] array everything will be ok.

So, to answer your question of why you would trim when writing back, simply because someone could have set a field to a string that was too long, and trimming it would reduce the length (if the value happened to have whitespaces at the end).

I understand you will have prevented this from happening in your GUI. But I look at this from the point of view of someone using my business class.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you should look back to the title of this post and confirmed by the question in your 1st post: "did you guys trim the strings to remove whitespaces?", so I respond to what you ask. I can't know you actually meant trimming of values which are too long. I'm not a clairvoyant

In my application a String[] with a value which is too long, won't pass the validation. I believe Roberto trimmed strings which are too long for the field.
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Keane wrote:The specific case I was pondering about was what if someone sent a record in to my business class where the field was a string that exceeded the schema.


I validated input length in the GUI, trimmed whitespace (if any) coming out of the database, and padded to max length (if necessary) on the way into the database. I also truncated if necessary on the way into the database, on the theory that my data class shouldn't depend on a validation in this GUI in a project that anticipates the adoption of other front ends. I didn't use a cache, so I can't speak to that.

I understand you will have prevented this from happening in your GUI. But I look at this from the point of view of someone using my business class.


My reasoning was the same. I pad or trim as necessary. My earthbound classes shouldn't know what's happening in the upper atmosphere.
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Maybe you should look back to the title of this post and confirmed by the question in your 1st post: "did you guys trim the strings to remove whitespaces?", so I respond to what you ask. I can't know you actually meant trimming of values which are too long. I'm not a clairvoyant



I think you may have misunderstood me. But apologies if any confusion. My second question was a follow up one on the first question - I wasn't implying you didn't understand my first question .

Roel De Nijs wrote:In my application a String[] with a value which is too long, won't pass the validation. I believe Roberto trimmed strings which are too long for the field.



I am in two minds which approach to go with here. I originally went with trimming. But thinking of it in database terms, if I ask an API to write "abc " to the database, then I expect to get "abc " back from the database if I read it back after writing.
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Byron wrote:I also truncated if necessary on the way into the database



I know this is probably a little on the theoretical side - but do you see the potential for confusion that I outlined in my previous post caused by trimming on the way in and out of the database?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Keane wrote:I am in two minds which approach to go with here. I originally went with trimming.


I think it's situation dependent. For example: people can send through our application mailings to a selection of members in our database. Each sent mailing is logged in a table and we keep some description about to which members it was sent (everybody between 15 and 35 years, living in Belgium,...). Such description can get quiet long and although we have a column of 10000 chars, it could exceed this value, so here we trim the value. Because you certainly don't want a DataTruncationException being thrown and you also don't want a user be prevented to send a mailing, because some column's size is not big enough.
But if data is written to the database which can be changed by the user, it must fit in the appropriate column, otherwise an exception will be thrown.
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Keane wrote:I know this is probably a little on the theoretical side - but do you see the potential for confusion that I outlined in my previous post caused by trimming on the way in and out of the database?

Sure. In general, I agree with Roel that it's situationally dependent.

Validations in the client and biz tiers should normally ensure that the need to truncate never arises. But at some point, writing to a 50-wide column is writing to a 50-wide column. The place to save the user from himself is in some higher layer. Below the realm of Recycle Bins, though, there's a place where rm -Rf is going to nuke your stuff, no questions asked.

I can see validating further even at the point of data access just to ensure that the bad case never occurs, but that's not the choice I made for this assignment.
 
reply
    Bookmark Topic Watch Topic
  • New Topic