10

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?

  • 1
    When you say "API" here, are you talking about writing a library or are you talking about some sort of internal web-service? It's an important detail. – Sean McSomething Sep 26 '13 at 20:39
  • It would actually be a Library I am building (thanks) – Man Sep 26 '13 at 20:44
  • This sounds self-answering... if you are not sure that the white space is not needed, you probably shouldnt be trimming the text. – GrandmasterB Sep 26 '13 at 20:54
13

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.

  • Thanks for taking time to answer. You summed up what everyone else seems to be saying. – Man Sep 27 '13 at 15:36
3

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.