Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to trim whitespace (leading and trailing) when selecting/inserting/updating table field data?

Presuming that the spaces are not important in a field's data, is it good practice to trim off the spaces when inserting, updating or selecting data from the table ?

I imagine different databases implement handling of spaces differently, so to avoid that headache, I'm thinking I should disallow leading and trailing spaces in any field data.

What do you think?

like image 852
Liao Avatar asked Dec 14 '22 03:12

Liao


1 Answers

I think it is a good practice. There are few things more soul crushing than spending an hour, a day, or any amount of time, chasing down a bug that ultimately was caused by a user typing an extra space. That extra space can cause reports to go subtly wrong, or can cause an exception somewhere in your program, and unless you have put brackets around every print statement in your logs and error messages, you might not realize that it is there. Even if you religiously trim spaces before using data you've pulled from the db, do future users of your data a favor and trim before putting it in.

like image 89
Peter Recore Avatar answered Dec 29 '22 00:12

Peter Recore