Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant abuse?

I have run across a bunch of code in a few C# projects that have the following constants:

    const int ZERO_RECORDS = 0;
    const int FIRST_ROW = 0;
    const int DEFAULT_INDEX = 0;
    const int STRINGS_ARE_EQUAL = 0;

Has anyone ever seen anything like this? Is there any way to rationalize using constants to represent language constructs? IE: C#'s first index in an array is at position 0. I would think that if a developer needs to depend on a constant to tell them that the language is 0 based, there is a bigger issue at hand.

The most common usage of these constants is in handling Data Tables or within 'for' loops.

Am I out of place thinking these are a code smell? I feel that these aren't a whole lot better than:

const int ZERO = 0;
const string A = "A";
like image 695
Bryan Rowe Avatar asked Dec 07 '09 20:12

Bryan Rowe


People also ask

What happens to a person who is constantly abused?

Staying in an emotionally or verbally abusive relationship can have long-lasting effects on your physical and mental health, including leading to chronic pain, depression, or anxiety. Read more about the effects on your health. You may also: Question your memory of events: “Did that really happen?” (See Gaslighting.)


2 Answers

Am I out of place thinking these are a code smell? I feel that these aren't a whole lot better than:

Compare the following:

if(str1.CompareTo(str2) == STRINGS_ARE_EQUAL) ...

with

if(str1.CompareTo(str2) == ZERO) ...
if(str1.CompareTo(str2) == 0) ...

Which one makes more immediate sense?

like image 66
Anon. Avatar answered Sep 23 '22 18:09

Anon.


Abuse, IMHO. "Zero" is just is one of the basics.

Although the STRINGS_ARE_EQUAL could be easy, why not ".Equals"?

Accepted limited use of magic numbers?

like image 33
gbn Avatar answered Sep 24 '22 18:09

gbn