constants

Why does Perl complain when I use a hash reference with constant.pm?

I have perl, v5.6.1 built for MSWin32-x86-multi-thread Binary build 638 provided by ActiveState. I am working on a Perl script where I have declared constants that are used later for comparison purposes. For some reason, I am getting an error that states something along the line of Constant name has invalid characters at script's line ...

Enumeration and constants

Is there better way than declare enumeration as public enum DepthNumberSize { Bit1 = 1, Bit4 = 4, Bit8 = 8, Bit16 = 16, Bit32 = 32 } and every time when operations with related data chunk performed switch statements are used, like: switch(size) { case DepthNumberSize.Bit1: buffer[i++] = input[j] & 1; ...

Weird constants

I've seen these in real code: #define SCREEN_DIMENSIONS 2 #define THREE_THOUSAND_FIVE_HUNDRED_TWENTY_TWO 3522 What is the weirdest constant you've ever seen? P.S. And of course my favorite in JScript: bool b; switch (b.ToString().length) { case 4: // true ... break; case 5: // false ... break; ) ...

How can I import constants into multiple modules in Perl?

I'm writing an app in Perl with several modules. I want to write some global constants that will be visible from everywhere, like this: #Constants.pm $h0 = 0; $scale = 20; And then use them without qualifying with main:: or Constants:: in several modules. However, if I write use Constants; in more than one module, they only get import...

Why doesn't C# offer constness akin to C++?

References in C# are quite similar to those on C++, except that they are garbage collected. Why is it then so difficult for the C# compiler to support the following: Members functions marked const. References to data types (other than string) marked const, through which only const member functions can be called ? I believe it would...

Constants in Objective C

I'm developing a Cocoa app, and I'm using constant NSStrings as ways to store key names for my preferences. I understand this is a good idea because it allows easy changing of keys if necessary. Plus, it's the whole 'separate your data from your logic' notion. Anyway, is there a good way to make these constants defined once for the whole...

What's the point of using constants for property keys?

Lately, I've come across a lot of Java code that relies on "properties files" for configuration. But instead of plain old string literals, the code uses constants (static final Strings) to retrieve the property values . I find this extra level of indirection annoying because I need to perform TWO lookups in EITHER direction. If I star...

When is it a good use of time to refactor string literals?

I'm starting on a project where strings are written into the code most of the time. Many strings might only be used in a few places but some strings are common throughout many pages. Is it a good use of my time to refactor the literals into constants being that the app is pretty well established and runs well? What would be the long-t...

What is the suffix used for long long constants

Hello, If i want to use something like below in a C code: if(num < 0x100000000LL) I want the comparison to happen on a long long constant, but suffix LL doesn't work in MSVC6.0 , but it works in MS Visual Studio 2005. How can i get it working in MSVC 6.0? -Ajit ...

Is it possible to simulate constants in Javascript using closures?

Is it possible to simulate constants in Javascript using closures? If so, can you please furnish me with an example? ...

Are there any valid arguments for using unnamed constants?

A commonly used term is "Magic numbers". As discussed in a related question, this is considered a code smell. I assume the same would go for string constants, although the term "Magic strings" is not in common use. So to generalize a bit, it would seem that ALL constants should be named, thus making them "unmagical". Or am I missing som...

changing constants for unit tests

Hi there, I'm writing some unit tests in cocoa for a data driven application. I've got a constants header file which defines a whole heap of variables including paths to the databases etc. I was wondering if it's possible to get all the classes to use a different set of constants which would link to a testing version of the database e...

Should I make a constants class for my annotations?

Which is better? @SuppressWarnings("unchecked") @SuppressWarnings(AnnotationConstants.UNCHECKED) Where AnnotationConstants is a typical constants class... public final class AnnotationConstants { private AnnotationConstants() { } public static final String UNCHECKED = "unchecked"; ... } I know that there are a lot of...

C++ best way to define cross-file constants

Hi, I am working on a game and have an interesting question. I have some game-wide constant values that I want to implement in one file. Right now I have something like this: constants.cpp extern const int BEGINNING_HEALTH = 10; extern const int BEGINNING_MANA = 5; constants.hpp extern const int BEGINNING_HEALTH; extern const int B...

How does one avoid accidentally redeclaring global constants in C++?

I have a template matrix class class defined in a header called "Matrix.h". Certain matrices are used repeatedly in my program. I thought that I would define these in the "Matrix.h" header file, like so: const Matrix<GLfloat> B_SPLINE_TO_BEZIER_MATRIX(4, 4, values); When I do this g++ complains that I redefined the constant in questi...

Is it better to store class constants in data members or in methods?

I recently wrote a class that renders B-spline curves. These curves are defined by a number of control points. Originally, I had intended to use eight control points, so I added a constant to the class, like so: class Curve { public: static const int CONTROL_POINT_COUNT = 8; }; Now I want to extend this class to allow an arbi...

Direct array initialization with a constant value

Whenever you allocate a new array in C# with new T[length] the array entries are set to the default of T. That is null for the case that T is a reference type or the result of the default constructor of T, if T is a value type. In my case i want to initialize an Int32 array with the value -1: var myArray = new int[100]; for (int i=0...

VB.NET Assigning a binary constant

Is there a way to assign a binary value to a vb variable? All of the obvious choices don't work. I've tried prefixing with &B, appending with b but nothing seems to work. I'm not having much luck searching for it either. I don't NEED this for my application, but am rather, just curious so alternate solutions are not necessary. [Edit...

Why does the absence of the assignment operator permit me to modify a Ruby constant with no compiler warning?

In the following two examples I do the same thing, creating a constant String and using the concat method to modify it. Because it's a constant, I expect a compiler warning but only receive one in the second example when I use the assignment operator. Why is this? X = "hello" X.concat(" world") puts X # no warning X = "hello" X = X.con...

Your opinion on declaring constants inside methods...?

Hi there, A developer in a team I'm supervising prefers declaring variables as constants in his tests, e.g. const int someValue = 1; (rather than just int someValue = 1;). When I saw this I found it a bit odd and questioned what he'd done. His argument is that this is sensible for the purposes of this test - because the value he's assi...