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 7431065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:14:15+00:00 2026-05-29T09:14:15+00:00

I am porting a legacy Python system to Java. I’m a python developer primarily

  • 0

I am porting a legacy Python system to Java. I’m a python developer primarily so this question concerns how I might take an unsatisfactory pattern in the original Python system but do it better in my Java version.

A system I’m working on deals with prices of products. All products have prices > 0.0.

In the orginal system all prices are represented as Python float objects (roughly like a Java Double). Functions which operate on these prices typically begin with a bunch of asserts to check that the range of the prices are sensible. Since there are a lot of functions there are a lot of assertions which become very repetitive after a while.

These assertions are better than nothing but it would be far better to throw an error the instant that something tried to create a negative price, since that’s where the fault lies.

It occurred to me that I might do better in my re-implementation by inventing a new type of numberish thing which behaves exactly like a Double in all ways except that it cannot be constructed with a negative value. If I had such an object then I could rely on Java’s type system and not need to write so many asserts.

Other than that (to begin with) it will obey all the normal rules of arethmatic – it will be a subclass of Double.

Later on I might extend this class by adding features that embrace more features of real-world money, e.g. preventing fractions of pennies. That’s not actually needed in my initial version, but I’d like any pattern I adopt to accomodate this sort of development over the next few months.

Is this kind of pattern considered acceptable to a Java developer? Is there a better way to preserve the flexibility of having number-like objects but constraints on their possible values?

  • 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-05-29T09:14:15+00:00Added an answer on May 29, 2026 at 9:14 am

    Well, first of all, no real software that deals with money uses floating-point math, because of the imprecision. A “Money” class that holds integer numbers of pennies is one common way to deal with this.

    Secondly, yes, having a constructor that rejects negative numbers by throwing an exception would be a fine thing to do for such a class.

    Here’s a good Dr. Dobb’s Journal article on the topic, with code.

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

Sidebar

Related Questions

We are in the process of porting a legacy system to .NET, both to
This might not be a programming question but I am posting it after long
I'm porting an existing system to ASP.NET MVC2. In the current legacy app, the
Im porting a project from php to java. The project is a web-app based
I am planning on porting a PHP application over to Python. The application is
I am porting some legacy code from windows to Linux (Ubuntu Karmic to be
I am responsible for porting a class from legacy Win32 code to .Net and
(This was meant to be a general hypothetical question, me whining that .NET was
I have a chance to start porting a legacy application written in C++/Powerbuilder to
I'm porting some old code that uses the int enum pattern to Enum and

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.