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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:32:05+00:00 2026-05-11T22:32:05+00:00

Constants are beautiful people – they can hold in a unique place a value

  • 0

Constants are beautiful people – they can hold in a unique place a value that is used everywhere in your code. Changing that value requires only one simple modification.

Life is cool.

Well, this is the promise. Reality is sometime different :

  • You change the LogCompleteFileName constant value from L:\LOGS\MyApp.log to \\Traces\App208.txt and you get two files : \\traces\App208.txt for the traces and \\traces\App208.txt.log for the logs…
  • You change TransactionTimeout from 2 to 4 minutes and you still get a timeout after 2 minutes (after spending the day, you find out that you also have to change the timeout of the DBMS and the timeout of the transaction manager…).
  • You replace SleepTimeInMinutes from 1 to 10 and you see no change (after an hour or so, you find out that the constant’s name was misleading : the granularity is not the minute but the millisecond…).
  • Even more subtle: you change CompanyName from, say Yahoo to Microsoft but automated mail alerts are still sent to alert@yahoo.com…

Creating a constant is a contract. You are telling your readers that whenever they change the value, it will still works the way they think it should be.

Nothing less.

Of course, you need to test that you are not misleading your readers. You have to make sure that the implied contract is right.

How do you achieve that with TDD? I’m just stuck with that. The only way I can test a change for a constant (!) value is to make that constant an application setting… Should I have to conclude that the const keyword should be avoided when I think that the value can and will change?

How are you testing your (so called) constants using TDD?

Many thanks in advance 🙂

  • 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-11T22:32:05+00:00Added an answer on May 11, 2026 at 10:32 pm

    The only way I can test a change for a constant (!) value is to make that constant an application setting

    All of the uses you listed in the question sound like application settings, not constants, to me. A constant is a value that is, well, constant, such as:

    const decimal LITERS_PER_HOGSHEAD = 238.480942392;
    

    Edited to add: Hopefully this is more helpful than my flippant answer. I usually create an AppSettings class. Some of the properties in this class are pulled from the config file, some are settings that I don’t expect to change, and some could be constants.

    public class AppSettings
    {
        public const decimal GILLS_PER_HOMER = 1859.771248601;
    
        public string HelpdeskPhone
        {
            get { // pulled from config and cached at startup }
        }
    
        public int MaxNumberOfItemsInAComboBox
        {
            get { return 3; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an algorithm in the standard library that can add a value to
Can constants in CodeIgniter be used for things like recurring text (say metatags and
I have several constants that I use, and my plan was to put them
I understand using constants for your names in a NSDictionary to prevent typos (myName
When I define constants in C that cannot be accurately represented in the decimal
I have various constants that my program uses... string 's, int 's, double 's,
What does Ruby constants really mean? The following code doesn't show any 'constant' attribute.
Most of the constants I see in others code are powers of 2, i.e.
I have a few constants that I'll need to define for my application, for
I created a beautiful faux legend for a box that surrounds some text: jsfiddle

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.