Placeholders in strings

I find that I sometimes use placeholders in strings, like this:

$ cat example-apache
<VirtualHost *:80>
    ServerName ##DOMAIN_NAME##
    ServerAlias www.##DOMAIN_NAME##
    DocumentRoot /var/www/##DOMAIN_NAME##/public_html
</VirtualHost>

Now I am sure that it is a minor issue if the placeholder is ##DOMAIN_NAME##, !!DOMAIN_NAME!!, {{DOMAIN_NAME}}, or some other variant. However, I now need to standardize with other developers on a project, and we all have a vested interest in having our own placeholder format made standard in the organization. Are there any good reasons for choosing any of these, or others? I am trying to quantify these considerations:

  1. Aesthetics and usability. For example, __dict__ may be hard to read as we don’t know how many underscores are in there.
  2. Compatibility. Will some language try to do something funny with {} syntax in a string (such as PHP does with "Welcome to {$siteName} today!")? Actually, I know that PHP and Python won’t, but others? Will a C++ preprocessor choke on ## format? If I need to store the value in some SQL engine, will it not consider something a comment? Any other pitfalls to be wary of?
  3. Maintainability. Will the new guy mistake ##SOME_PLACEHOLDER## as a language construct?
  4. The unknown. Surely the wise folk here will think of other aspects of this decision that I have not thought of.

I might be bikeshedding this, but if there are real issues that might be lurking then I would certainly like to know about them before mandating that our developers adhere to a potentially-problematic convention.

2

I use placeholders myself, and over the years have sedimented the following considerations, in descending priority:

  • no escape sequences or quote symbols, they are awkward everywhere: ' "
  • no shell metacharacters, for I often use scripts. Avoid: $ < > ( ) { } | ! and backticks
  • no regexp-risky characters, searching which could be an escape nightmare: . - * ?
  • no common and mathematical operators: , + - / *
  • no UTF-8 or higher ASCII characters that can mess binary searches

This leaves few candidates: % : ^ # _ @ ; (where ^ and % are often operators and ought to be excluded).

Personally, I use the last three – # _ @

Language constructs would probably lead to eschew %, :, ; @ and ^ (even if @@@DOMAIN_NAME@@@ is a nice kick in the teeth: you can’t miss it).

Using fixed length fonts, I use indifferently _ and #. Maybe I’m more inclined on __THIS_FORM__ which is even legal in C (danger, Will Robinson! You might have C constants formatted like that!).

I think that # is a better choice if you have legibility issues. ###DOMAIN_NAME### looks pretty good and is unlikely to come by in any language I’m familiar with. And ###_DOMAIN_NAME_### is more complicated, but maybe more readable.

Of course in some specific domains you might actually want to ape some existing schemes (e.g. use shell metacharacters), either to make it clearer that those strings are really placeholders, or because you can leverage (with all due care) some tool to expand them: hence e.g. {{ varname }}, ${ varname }, $(varname) or [[ varname ]].

In scripts such as VirtualHosts, I tend to use @@@NAMES@@@… maybe I shouldn’t have attempted to answer, since I’m unable to agree on one standard even with myself!

3

Take a look at some projects that you admire and follow their convention.

Don’t make up something new, try to find out what a typical convention is and go with it.

Make sure to include a way to escape that character sequence so you can include it as a literal without it being interpreted as a token.

3

In the weeks since I first asked this question, I’ve learned which characters to avoid. I list them here for my own use and for others:

  • Avoid * and _ as some issue-tracking software (such as redmine) use these for wiki-syntax and they will replace the values in copy-pasted bug reports.
  • Bash absolutely chokes on !, in fact you cannot even up-arrow to get the back back to fix the line.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật