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

  • Home
  • SEARCH
  • 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 9072217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:06:54+00:00 2026-06-16T18:06:54+00:00

You have an immutable object, and you set its internal variables in the constructor

  • 0

You have an immutable object, and you set its internal variables in the constructor which accepts couple of parameters.

Question:
Do you see any problems to VALIDATE constructor parameters in the constructor method of an immutable object and throw ArgumentExceptions if not valid?

(to me it makes sense but I wanted to ask in case there are some better ways or something not OK with this – for example if it is a better design to move validation from constructor to a factory)

Or if I generalize it by rephrasing the question:

Is it OK to put business rules-wise logic in the constructor methods? Or should constructors always do nothing more than setting object’s internals?

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-16T18:06:55+00:00Added an answer on June 16, 2026 at 6:06 pm

    In a way, it makes sense to validate in the constructor itself because you know that all usages of it will pass through that single point, and any other developer that will use your code will be protected from making mistakes because of your “low-level” validations.

    If you move the validation higher up the call chain, you leave the class code cleaner but you expose the code to the possibility of “you’re using it wrong” bugs.

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

Sidebar

Related Questions

I have an (immutable) object, Group, which I am trying to use in a
I know that immutable objects always have the same state, the state in which
Say that I have an immutable Point class with x and y parameters, and
I have a class which is intended for immutable use, hence I would like
I have a general question with regards to immutable collections. I will use Java
I am designing some immutable classes but I have to have some variables like
I have the following attempt at an immutable object: class MyObject { private static
I have a immutable List. In the below g.Select(...) I do a .ToList() which
I have designed a class containing some information about a given object which will
I have a program with a large immutable object that returns a modified version

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.