Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8026601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:32:48+00:00 2026-06-04T23:32:48+00:00

Is it a good practice to make all interface (trait) methods for which there

  • 0

Is it a good practice to make all interface (trait) methods for which there may exist a future implementation with invalid arguments returning an option?

Let me give an example. If I would implement a library for probability distributions with a trait

trait Similarity {
   def getDensity(): Double
}

Since most distributions are not defined over the whole real space, there are always some illegal parameters e.g. a non-positive variance for a Gaussian distribution. If I understood it correctly, I should return an Option[Double] rather than a Double and throwing an IllegalArgumentException.

I think the same is true for most functions/calculations. What is “best practice” in this case? I am afraid this would make a library overly clumsy.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:32:49+00:00Added an answer on June 4, 2026 at 11:32 pm

    I wouldn’t throw an IllegalArgumentException as it is not the arguments that are the problem, but the state of the object. If it were to be an exception, IllegalStateException would match.

    However, the real answer depends on what you expect the caller to do in the case of a problem.

    If they would themselves throw an exception, that’s what you should do, saving them the bother.

    If they would do something different based on the answer being impossible, an Option[Double] is a good indicator.

    A possibility worth knowing of, but less likely to be useful, is Double.NaN, effectively a Null Object but for Doubles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it good practice to make sure that all abstract classes have names prefixed
Is using MS SQL Identity good practice in enterprise applications? Isn't it make difficulties
Is it good practice to set all text fields to nvarchar(MAX)? if im not
Is there any good practice for this? I wish I could solve the problem
I am wondering if unit testing private methods is a good practice? Normally only
I am wondering is it good practice to try to make a view that
When i write down a function is a good practice to validate all the
Is it good practice to have one shared SqlConnection object in a .NET application
What is a good practice on work with (nHibernate) ITransactions between several repositories? First
Is it a good practice to have timestamps for the last row update in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.