Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Object Construction - Invalid Values  RSS feed

 
Ranch Hand
Posts: 340
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose we have a Rectangle Class which has a constructor accepting length and breadth as parameters. But, if the user passes negative/zero values, then, what should be the behaviour? Should we validate it in the constructor itself?
 
Saloon Keeper
Posts: 4765
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Validate in constructor and throw an exception.
 
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have mutable reference types as parameters, take a defensive copy first and validate the arguments later. Then, as Carey said, throw an IllegalArgumentException. The same applies to any setXXX() methods.
 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of a rectangle, breadth and width are more or less synonymous (i.e. they both mean the same).

[Edit: this post doesn't make sense because I split off an off-topic post.  Sorry about that!  You can see the post here.
 
Carey Brown
Saloon Keeper
Posts: 4765
52
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In terms of graphics, they are more typically referred to as width and height.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!