12

I am intern learning tons about the industry outside of academia.

One thing I thought about today was trimming input.

On one side of the coin I don't want the user/implementer to constantly receive unexpected results because their input had too many spaces, thus I need to constantly trim user input after every function call.

But at the same time If I am creating a API library for internal use here at the office trailing/leading whitespace may be crucial to the results.

Then there are instances were I am not sure if whitespace will be important or not.

The big issue for me is that I find myself CONSTANTLY calling .trim() everywhere in my code.

Does anyone have any tips/ rules of thumb or just thoughts on how to handle certain situations?

TacticalMin
  • 265
  • 2
  • 6

3 Answers3

21

Never trim whitespace arbitrarily in an API.

The only reason to ever trim whitespace arbitrarily is as a UI feature. People frequently leave spaces at the end of entered fields but can't see that they've done so. It's pretty confusing to return a message, so -- except in the rare case where a user might enter "A " to mean something other than "A" -- you can usually ditch any useless spaces.

But computers don't add spaces by mistake, they add spaces because a programmer told them to, and if they break your API implementation you should throw an error back to the developer. If they don't then you should just use them.

There is, of course, a possibility that this data is coming from a human via an application, but that's the application-developer's issue, not yours. Again, if they're going to cause you a problem, reject them; if not, accept them. Don't trim them and assume your consumer knows that will happen.

pdr
  • 53,387
  • 14
  • 137
  • 224
  • Thanks for taking time to answer. You summed up what everyone else seems to be saying. – TacticalMin Sep 27 '13 at 15:36
  • Sounds more than reasonable, though this doesn't seem to be customer-friendly at all... Imagine I have 50+ partners who use my API. If at least 10% of them have issues with their applications that don't trim user input at UI level, then they will be unhappy to communicate with my API and I potentially can lose them, thus lose lots of money, even though I am not guilty! Hm... – RAM237 Nov 23 '20 at 22:47
4

One useful principle here is YAGNI: "You Ain't Gonna Need It." What it means is that, when you have an idea for a feature that you're not sure you'll actually need, don't implement it until you are sure you need it. Then, when the need for it becomes apparent, the places in your codebase where it is actually necessary should also be apparent.

Mason Wheeler
  • 82,151
  • 24
  • 234
  • 309
1

It depends what the input is going to be used for. If you are trimming white-space from data that's been entered as a search term then I can understand why you are doing it. It's a good habit to learn not to trust data coming into a system, I'm thinking primarily of SQL Injection but there are other aspects as well.

You might not need to always trim input but you definitely should always check input.

Daniel Hollinrake
  • 740
  • 1
  • 5
  • 13