13

Recently i get in touch with Empire-db. The project doc. stated that they are using "string-free coding", intended to use less constant strings as possible to avoid typos and use the force of the compiler.

I was always a big fan of this principle, because i think as Java is a formal language it could be expresive for configuration like properties or xml and gives extra value by checking thru the compiler. Maybe at runtime there are less posibilities to changes without recompiling but this depends on each case.

Do you now more references or readings about this term/principle/design pattern ?

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • 1
    Thanks for pointing out Empire DB. It is a tool which merits further intvestigation. I was particularly struck by the analysis of Hibernate: http://incubator.apache.org/empire-db/empiredb/hibernate.htm – APC Sep 29 '09 at 09:28
  • While it's a nice principle, as always, use common sense first. Mindlessly following a good design practice with good intentions can also lead to bad code, check out http://thedailywtf.com/Articles/It-Gets-Worse-Each-Year.aspx and http://thedailywtf.com/Articles/Soft_Coding.aspx – Tamas Czinege Sep 30 '09 at 10:18
  • Conversely, the anti-pattern is known as [stringly-typed](http://wiki.c2.com/?StringlyTyped) code. – jaco0646 Oct 15 '18 at 18:56

5 Answers5

6

This is the same thing as magic number vs constants. Using constants raises maintainability and readability. Constants also encapsulate change: a change in constant value is automatically propagated and, as for string-free coding, a misspelled constant is a compile-time error.

Rich Seller
  • 83,208
  • 23
  • 172
  • 177
dfa
  • 114,442
  • 31
  • 189
  • 228
4

I know little about Java, but this idiom is famous in modern C++. For example, Xpressive is a regular expression library that allows the programmer to write regex in C++, and does compile-time checking. Spirit is another example, where you could write EBNF in C++, and the compiler can check the syntax for you also :)

I think the term you are looking for is Embedded Domain Specific Languages.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • Qt has something related too, but that's because of the way translations work there. Basically you encapsulate every user-visible string with "tr( ... )", and the app looks it up dynamically at runtime, only using the given string when no suitable translation was found. – György Andrasek Sep 29 '09 at 08:45
1

Never heard the particular expression, but Effective Java recommends to "avoid strings where other types are more appropriate" (Item 50).

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
0

The famous Pragmatic Programmer mentions this principle especially in connection with metadata handling. One of the tips about this is:

Put Abstractions in Code, Details in Metadata

Program for the general case, and put the specifics outside the compiled code base.

It is worth reading the book anyway.

Community
  • 1
  • 1
rics
  • 5,494
  • 5
  • 33
  • 42
0

Regarding empire-db, it's like 'type-safety' for SQL queries.

Somatik
  • 4,723
  • 3
  • 37
  • 49