• 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

Data validation and saving practice

 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say I want to save User object. Before I save it, I have to validate it through various business validations. How do I go about it?

In my current code, I see it coded 2 ways as below.



I am curious about how do you folks code this. ty
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using Strings to pass messages between methods? Strings are for people to read; computers have difficulty with them.
Are those validation methods in the same class? I think they ought to be, so you can maintain single responsibility. If you have mutable reference types (Strings are of course immutable) take defensive copies and for reasons explained in Bloch's Effective Java validate after copying. Make sure the criteria for validation are spelled out in the documentation comments. Maybe you shou‍ld use a regex to validate String input. And as soon as you find something invalid, don't stand there staring the varmint in the eyes. Shoot from the hip and make lots of lead fly. Don't waste your breath arguing with such an ornery critter; just plug him.

Or, in non‑cowboy language, start throwing some Exceptions and get it over and done with. Who needs to do lots of processing when one already knows the result is going to be invalid?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saurabh Pillai wrote:


This signature doesn't make sense to me. Think why.
 
Saurabh Pillai
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Or, in non‑cowboy language, start throwing some Exceptions and get it over and done with. Who needs to do lots of processing when one already knows the result is going to be invalid?


I am validating all the fields so that I can inform user about all the problems with any of the fields in single error message, hoping user would correct it in one go. Apart from that, do you think having validation and save into separate methods is good practice?
 
Saurabh Pillai
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Saurabh Pillai wrote:


This signature doesn't make sense to me. Think why.



Just want to say that this is not copied from production. About the problem with signature, I forgot to add throws clause. Is that it?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saurabh Pillai wrote:. . . About the problem with signature, I forgot to add throws clause. Is that it?

No.

* * * * *

Do the validation from the constructor (or factory method if used). I think it is probably a good idea to have a private validation method called from the constructor, otherwise you will have too long a constructor.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saurabh Pillai wrote:

Liutauras Vilda wrote:

Saurabh Pillai wrote:


This signature doesn't make sense to me. Think why.



Just want to say that this is not copied from production. About the problem with signature, I forgot to add throws clause. Is that it?


No.

Not knowing what's around that code, but, read it out loud what you have, well, will help you with that:

"Here I'm giving you a user, please create for me user" - pretty dumb, isn't? Same as I'd say to you: "Take the bread, bake bread".

From the parameter I see you are passing a User already, why are you creating a User? It is created already from what I understand. Now I'm playing silly, you might want to save it to database or something? Then change the method name if that's the case.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saurabh Pillai wrote:Just want to say that this is not copied from production.


I see. Before it gets there, make sure you have indentation and formatting correct.

You have comment:
That comment is incorrect. There is a key and a value, not the two keys.

You have some magic number there, which is 25. You either supposed to have a descriptive variable what it represents, or explanation why is 25 there. For example: restricted length on db table's field.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaah! I was wondering what the 25 meant.

If you create an object and then validate its fields later, you might have an object not in a consistent state, i.e. one which has not established its invariants. Once the object has been created, it is possible for other code to use it and any errors will have effects on other data. You cannot necessarily tell where the errors have occurred, which is why I said validation ought to be done early. If you alidation fails during object construction, and you throw an exception in the constructor, you can destroy the object immediately. If validation fails later, there is no 100% reliable way to destroy the object.

Also remember not to let a this reference escape the constructor.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of app is this?
Since this seems to be about user input validation, is this a web app?

Either way, there are validation frameworks that will handle this sort of thing.
Spring has one, for example.
 
Saurabh Pillai
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Now I'm playing silly, you might want to save it to database or something? Then change the method name if that's the case.


I named the method according to CRUD. This is part of REST webservice. User object contains what actual user entered during signup process. I need to validate that input and then save the user into database. I kept the return type as void because, why do I need to return the user object?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liutauras is right: that is a badly‑named method.
As I said before, if you have unvalidated data, what good is it having an object in the first place?
 
Saurabh Pillai
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:You have comment:
That comment is incorrect. There is a key and a value, not the two keys.

You have some magic number there, which is 25. You either supposed to have a descriptive variable what it represents, or explanation why is 25 there. For example: restricted length on db table's field.



I add 2 keys because, isValid will have either {"true", "false"} and if validation failed then message will have appropriate error message.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using a REST framework?
If so does that framework have validation you can hook into?

In something like Spring that User object should have been validated before even entering this method.

As for the naming of the method it possibly ought to be addUser to avoid the confusion over the signature.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic