‘Static readonly’ vs. ‘const’

public static readonly fields are a little unusual; public static properties (with only a get) would be more common (perhaps backed by a private static readonly field). const values are burned directly into the call-site; this is double edged: it is useless if the value is fetched at runtime, perhaps from config if you change … Read more

How to reference constants in EL?

EL 3.0 or newer If you’re already on Java EE 7 / EL 3.0, then the @page import will also import class constants in EL scope. <%@ page import=”com.example.YourConstants” %> This will under the covers be imported via ImportHandler#importClass() and be available as ${YourConstants.FOO}. Note that all java.lang.* classes are already implicitly imported and available … Read more

How do I remove code duplication between similar const and non-const member functions?

For a detailed explanation, please see the heading “Avoid Duplication in const and Non-const Member Function,” on p. 23, in Item 3 “Use const whenever possible,” in Effective C++, 3d ed by Scott Meyers, ISBN-13: 9780321334879. Here’s Meyers’ solution (simplified): struct C { const char & get() const { return c; } char & get() … Read more

Is it better in C++ to pass by value or pass by constant reference?

It used to be generally recommended best practice1 to use pass by const ref for all types, except for builtin types (char, int, double, etc.), for iterators and for function objects (lambdas, classes deriving from std::*_function). This was especially true before the existence of move semantics. The reason is simple: if you passed by value, … Read more

Constants in Objective-C

You should create a header file like: // Constants.h FOUNDATION_EXPORT NSString *const MyFirstConstant; FOUNDATION_EXPORT NSString *const MySecondConstant; //etc. (You can use extern instead of FOUNDATION_EXPORT if your code will not be used in mixed C/C++ environments or on other platforms.) You can include this file in each file that uses the constants or in the … Read more