Background: So while Javascript implicitly convert various data types and hence may seem almost type-less(is there such a thing?) it does have different types.

Most of my colleagues seem to think otherwise. The pre-sales and non development colleagues are unaware of this and other developers(90% are senior to me) seem to think it doesn't matter as the users don't really care.

Problem: Whilst implementing a new feature where we accept a setting file in form of JSON, the specification does not specify which types the values are. On the surface this isn't such a big deal as it's very obvious to deduce the types: thickness, transparency, etc are obviously numbers, others are obviously strings. But after implementing the feature, the colleague mentioned that I should take all values as string first, then attempt to parse it to double because many users don't know the difference.

He's correct in a sense that his method seems more "defensive" and the specification does not explicitly specify the types of the values. But I am somewhat hesitant because I think we are actively encouraging a bad practice.

So what should I do? Ask the designer to explicitly specify the types on the specs sheet? Change my implementation to take it as a string? Or leave it as it is and display error when incorrect type is given for the value?

UPDATE: What ended up happening is we decided to use a common logic which accepts the strings as well. I told my team manager that the types were missing from the specs and he said he will talk to the designer.

  • 1
    Why don't you just specify what the types should be? This will save you lots of trouble down the road.
    – JacquesB
    Dec 1 '17 at 7:23
  • @JacquesB I wish, but the specs are written by one of the senior developers and I can't modify it directly. I can however "veto" her specification sheet if it doesn't meet the checklist requirements, however this is considered...somewhat rude from a junior developer. Dec 1 '17 at 8:25
  • 1
    Then it seems you have a leadership problem rather than a technical question. No amount of defensive programming will save you from an imprecise specification.
    – JacquesB
    Dec 1 '17 at 8:49

"Be conservative in what you send, be liberal in what you accept": Robustness Principal a.k.a. Postel's law, a.k.a RFC 1122.

In other words, you should be lenient unless there is a very good reason (e.g. security) to be strict.

  • 7
    Postels law has its place, but it is definitely not a solution for an imprecise specification.
    – JacquesB
    Dec 1 '17 at 7:48

This sounds like a user experience issue. If the configuration is manually written by users who might not know the difference between a string and a float, then JSON files is probably not the appropriate format for input. Consider providing a GUI for the configuration.

In any case, you should create a precise specification for the JSON format.

You note that JavaScript implicitly converts types. This is true, but it does not solve the issue of wrong types. Say your code expect "thickness" to be a number, but the user provides the string "bold". Sure, JavaScript can implicitly convert this to a numeric value (NaN), but it will almost certainly not work the way the user expected. So it might be better to alert the user that the input is in a wrong format.

Implicit conversion will work in a few very specific cases, e.g. if a number mistakenly is quoted as a string it can be simplicity converted to a number. But the opposite, forgetting the quotes around a string, will almost certainly lead to a parse error.

  • OP specified that this was a string-number issue, so "bold" is not an issue. Agree that users, in general, should not be writing JSON.
    – user949300
    Dec 1 '17 at 16:10
  • Forgetting quotes around a string is not valid JSON, so that is already covered in his spec. How will creating a "precise specification" help that?
    – user949300
    Dec 1 '17 at 18:02
  • @user949300: It wont, that why I say it is a user interface issue.
    – JacquesB
    Dec 1 '17 at 18:14

A few years ago, I was working on a similar problem; but that was working with the direct input of data, so that if there were any confusion, an error msg could ask for a different input, offer guides, etc.

For this, since you are receiving the data in a file, as your colleague suggested, try to find ways to automatically adjust data to meet the needs of the program.

If there are situations that cannot be fixed automatically, the information would be able to provide, would help the colleague understand the need for more clear requirements.

While you point out that the user can supply data in a file, is it feasible to create a data input app they could use, that would be able to make sure all the info you receive was valid?

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.