Help contents
Constants and Enums window
The Constants and Enums window lets you do a constant analysis. You can access this window via the View menu.
- All constants and Enums
- List all constants and enumeration constants in the system.
- Duplicate names
- List all the cases where 2 or more constants/enum constants share the same name.
- Duplicate values
- List all the cases where 2 or more constants/enum constants share the same value. Use this option to search for constants to join together. Defining a constant value at just one location is good for both reuse and prevention of errors. When you need to change the value, you only need to do it once.
- Duplicate name+value
- List all the cases where 2 or more constants/enum constants share the same name and value. You should remove the duplicates if possible.
- Same name, different value
- List all the cases where 2 or more constants/enum constants share the same name but have a different value. These are potential errors. Why do they have a different value? Is it by design or by error? Even if the code is correct now, having several different values is likely to confuse developers later. Consider renaming the constants. Right-click the one you want to rename to get a list of references. After renaming, remember to rewrite each of the use locations as well, otherwise your code will not function the way it did before.
- Same value, different name
- List all the cases where 2 or more different constants/enum constants share the same value. This list may reveal constants that have been defined several times with (slightly) differing names. As described above, defining each constant at just one location is better.
The toolbar buttons let you access more features. You can also access these features by right-clicking the list.
- View declaration of selected constant. You can also access this option by pressing Enter or double-clicking the constant.
- References (Ctrl+X). Show cross-references to the selected constant to see where it is being used. You can also right-click the constant and select References.
- Tabular report (Ctrl+R). This report lists what you see in the dialog. Tip: Take this report in HTML mode by selecting HTML as the report type in the main window.
- List report makes a longer list out of what you see.
- Copy (Ctrl+C) puts the dialog contents to the clipboard for pasting into your documentation or spreadsheet.
Guidelines for constants
- Define each constant just once. Reuse your constants across the entire system. When you need to change the value, you only need to change it once.
- One name must mean one thing. The same name shouldn't refer to many values depending on code location. — The one exception is Enum constants in VB.NET. Each Enum constant is always prefixed with the Enum name (MyEnum.MyConst), making confusion less likely.
- Use descriptive names for both the constants and the enumerations.
- Centralize constant definitions in a special global module when it seems sensible. When the constant is only used in a certain module or procedure, you can also keep it local.
- Centralize your strings. In classic VB, each string gets written into the executable every time it occurs in the code. By defining your strings as constants you can reuse the strings without bloating the executable.
VB.NET does a better job at this as it saves each string just once. See String literal analysis.
- Don't use literal numeric values in your code. Declare constants and Enums instead. Constants make your code more self-documenting. Again, when you need to change the value, you only need to do it once.
- Prefer Enums over constants. An Enum is a better choice for related constants. You can use the Enum as a data type for variables and parameters, which makes them more self-documenting.
- Add enough precision to floating-point values. Reuse is more successful when constants are accurate. As an example, declare
PI=3.14159265358979
rather than PI=3.14
. Even if the program doesn't need all the decimals right now, reusing an inaccurate value may lead to problems later. An inaccurate or incorrect constant value may be hard to detect.
©Aivosto Oy Project Analyzer Help