• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Strategies for console application input sanitisation

 
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm writing my first console project that I would like to present as a small real project. By that I mean, it reads data from a file, a user can add or edit content and save the changes back to a file.

What is becoming concerning is that the code is littered with try catch blocks and if else blocks in an attempt to deal with poor user input.

If I ignore poor user input, the code reads much cleaner and is significantly shorter.

Naturally, if I do this, the program crashes each time the user:

1. types a letter instead of a number
2. types a number outside of the bounds of the menu options
3. presses enter instead of typing any input.

What strategies are there to deal with this, so that the intent of the program remains clear, while dealing with poor user input.
 
Jay Rex
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After posting that message, the following Wiki article was suggested, which is exactly what I was looking for:

https://coderanch.com/wiki/659777/User-Input
 
Sheriff
Posts: 16961
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad you found that article useful.

Just a couple of things I'd like to note in addition:

1. It's great to see that you are thinking about keeping your code clean and clear. That's more uncommon than you'd think it would be and for me, it's the mark of a good programmer. As Martin Fowler said: "Any fool can write code that computers can understand. Good programmers write code that humans can understand." (My emphasis on "good")

2. The techniques explained there revolve around a refactoring called Extract Method. That refactoring and the Rename For Clarity refactoring are refactorings you will find yourself performing over and over as you write code and strive to make it clearer. Extract Method can address at least a couple of issues: duplication (i.e., violations of the DRY Principle) and noise from violations of the Single Level of Abstraction Principle. Rename addresses the code smell of lack of clarity.

Taken together with the Compose Method Refactoring, you have what I consider to be the Big Three refactoring techniques that make up 80% of the effort needed to keep code relatively clean and maintainable.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic