Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have an app that runs a C++ server backend and Javascript on the client. I would like to define certain strings once only, for both pieces of code. For example, I might have a CSS class "row-hover" - I want to define this class name in one place only in case I change it later.

Is there an easy way to include, or read, some sort of common definitions file into both C++ and JavaScript? Ideally as a compile / preprocessing step but any neat approach good.

share|improve this question
8  
Ah, I see we have a masochist here with us today. –  Neil Sep 28 '12 at 16:05
    
Actually even more masochistic: I need to ensure same ids/classes in CSS too. –  paperjam Sep 28 '12 at 16:29
add comment

3 Answers

I had proposed a quite similar approach here: http://stackoverflow.com/questions/4248831/shared-config-file-between-php-and-c/4248881#4248881 for a similar issue.

I would suggest to write a small script that generates code for C++ and JavaScript, with the configuration you want to have common. You can also use two templates in a template library, that you will run as part of your build process that will generate the appropriate files for each language.

Using a freemarker-like syntax (although I do not suggest including another language!), there could be something like this for JavaScript:

commonStuff = { config1: ${value1},
config2: ${value2}
}

and another template for the C++ implementation.

As suggested in the comments below, one good candidate for that, is to use sed, so that you would not have to introduce another language since your project has two already.

share|improve this answer
    
Writing a small templating step in your toolchain is a good answer. Common definitions go in the file that feeds the template, language/feature specific code goes in template files. sed, perl, python... any could be used to implement this pretty quickly. –  seggy Sep 28 '12 at 16:39
    
@seggy Yep, actually I wanted to suggest sed as an alternative to using a scripting language to minimize the number of "extra" languages. –  dimitris mistriotis Sep 28 '12 at 16:48
    
In general, I agree. sed should be on almost any system (and it's a really handy trick to have in your kit), but if he's on windows without Cygwin, he may have other options available. –  seggy Sep 28 '12 at 17:00
add comment

What you're describing is what is commonly done with languages like JSP and PHP, injecting names defined on the server. If you cannot use these languages, then I don't think it is worth the difficulty you're attempting simply to avoid having to change class names in more than one spot. Granted, that is generally a best practice, but if it makes your code unnecessarily unreadable, you should avoid it like the plague.

C# is similar enough to C++ that the transition should be smooth, and C# has Asp support which allows you to define constants and drop them directly in the page.

However assuming that you don't want to switch language, my advice is to do the next best thing: Have a C++ header and a css file which declare the same names. If you need to change the names, you need only change in these two places, and you've lost little extra time in doing so. Don't lose sight that simplicity is your objective, not avoiding duplication of code, and in this case, I can't possibly think that what you're attempting is the simplest solution.

share|improve this answer
add comment

Generate the javascript based on the definitions in the C++ file. If you already have a template system in place for the HTML, you can probably reuse that to interpolate C++ values into the javascript.

If server load or performance is an issue, consider caching the javascript (both on the server and on the client), or precompiling it altogether (it's a small step from a C++ program that serves dynamic javascript on-the-fly to one that saves it in a file instead).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.