• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make Code API Ready

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a problem of stripping NON ASCII data from an input string ,replace it with whitespace  and return the ASCII data. There are probably libraries which do it already but i'm getting back to honing my java skills so i wrote code for that. And then it got me thinking, if i were to expose this as an API what else should i do ? Put it another way do we have a blue print for writing code that makes it API ready.

What are some improvements that i should make when writing code for such problems which are common and what type of tests are run usually ?

 
Saloon Keeper
Posts: 7585
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A shorter way to accomplish the removal of non-ASCII characters would be to use regular expressions, somewhat like this:

return inputText.replaceAll("[^\\p{ASCII}]", " ")

As to making it into an API, are you talking more about a library, or a remote service (like a REST solution)?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of writing code for which there already is a library; you may be able to practise better on different code, for one thing, and the library will probably have been extensively verified and tested. Reinventing the wheel is often not a good idea.

Start by explaining what you mean by “stripping NON ASCII data from an input string”. You need a definite specification of what you are going to do before you try to do it.

Once you have decided that, you will probably find it easy to create code. Then you will have to decide on a name for the method and for the class it is in. I don't think the name of your method accurately describes what you are doing, because it doesn't mention replacement by spaces. Also decide what sort of class you are going to use: is it going to be a utility class (which I think is implied by your use of static) or an object?

I think your code will correctly replace all chars valued > 0x007f with 0x0020 (=single space). But I have all sorts of questions:-
  • 1: Why are you using ints rather than chars? Even when line 16 uses a char?
  • 2: Do you want code points or chars?
  • 3: Why are you calling the variable WHITE_SPACE when it is actually a single space?
  • 4: Why did you start with 0, which is the null character? Do you want to retain control characters in your text?
  • 5: Is it possible to get a code point < 0? Is one of your tests for the range of the character in line 15 redundant?
  • 6: Can you replace the if‑else starting line 15 with the ?: operator?
  • I think answering those questions will make you understand your code better and enable you to work out improvements. I would suggest you test that method with lots of Strings with or without characters > 0x007f. Pass an expected input and output and see whether you achieve the expected output.

    Once you have done that, there is one essential part of the method you have omitted. The biggest part of what you write shou‍ld be documentation comments explaining what the method does. Beware: once you have written a documentation comment for a public method and released it, you ought to maintain the contract implied by that documentation for ever.
     
    Anand Athinarayanan
    Ranch Hand
    Posts: 49
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Tim : Thank you. By API I mean a library. Regex always has been confusing for me but I understand for text manipulations Regex is powerful. I like it when there is less code.

    Campbell : Whoa, that's some good advice. Thank you. I'm checking the link you provided. I have to go back to my notebook now.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic