且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在选择/插入/更新表字段数据时修剪空格(前导和尾随)是一种好习惯吗?

更新时间:2023-11-18 09:58:28

我认为这是一个很好的做法.没有什么比花费一小时、一天或任何时间来追查最终由用户输入额外空格引起的错误更令人心碎的事情了.额外的空间可能会导致报告出现微妙的错误,或者可能导致程序中的某个地方出现异常,除非您在日志和错误消息中的每个打印语句周围都加了括号,否则您可能没有意识到它的存在.即使您在使用从数据库中提取的数据之前虔诚地修剪空格,也请帮助您的数据的未来用户并在将其放入之前修剪.

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.