Template engines [closed] - template-engine

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
Are there any for purposes other than web? e.g. for use in script generators, c++ code generators or other related, generic operations.

Have a look at http://velocity.apache.org/ or http://nvelocity.sourceforge.net/

In Visual Studio there is a templating engine primarily for code generation, called T4.
Here is an entry point for documentation on MSDN.
So, to answer your question, yes they to exist for other purposes than the web.

Yes there are many out there. One that I know of that allows you to generate more templates if you need them is MyGeneration. Another, is you could always build your own xslt template engine then you could build whatever you need. However there are several opensource and commercial code generators.

E.g. StringTemplates is a Java-based template engine for generating all sorts of text artifacts, and model generator frameworks like openArchitectureWare (or GeneSEZ) use the Expand template engine.

Imatix GSL is the most impressive (and simplest) of the tools that I have encountered. Plus it has been used to generate large amounts of complex code.
Also, lua is a programming language whose initial purpose was data definition, and I have found it to be very capable in this area. So, you define your data in lua and you execute the data definition files (valid lua programs) and you can generate any code from it.
Consider the following model for a C-function in Lua.
> func {
> name { "xyz" }
> parameters {
> { name= "x" , type="uint32_t" } ,
> { name = "y" , type = "uint32_t"}
> }
>
> ret { type="uint32_t" }
>
> psuedocode {
> "getLock(lockName)" ,
> "getSessionMemory" ,
> "addSession" ,
> "releaseLock"
> }
> }

They can generate configuration files.
Puppet and Chef rely on erb templates a lot, for example.

Related

question about System.Linq.Dynamic [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have a question regarding using some features from the System.Linq.Dynamic assembly.
I needed using queries like "#"NOT (Person.Name = ""test"")" for example, now the problem is that I do not have a certain object type, but instead need reading the property names and their values (and may be types, though I actually must deduce the types from the values) ... I solved this by using reflection (created the type and the properties as needed at runtime) .... but I still wonder whether that is possible without creating the types and properties, but using anonymuous types (I basically need to not have to enter those types, just the values ... of course I can write code to find out the type of the value entered for ex it has quotes - that means it's a string ...), or if there is a another library in .Net for accomplishing this task (I did not have very much time for looking into the Dynamic class ... how it's working etc)
.
The Dynamic Linq parser always requires an actual type to parse against because it is parsing the dynamic expression into System.Linq.Expression expression trees, which are based on types. However, the Dynamic Linq library contains a method for quickly creating anonymous types like this at run time. Here is an example of using that (taken from the html documentation file that comes packaged with the DynamicLinq.cs file):
DynamicProperty[] props = new DynamicProperty[] {
new DynamicProperty("Name", typeof(string)),
new DynamicProperty("Birthday", typeof(DateTime)) };
Type type = DynamicExpression.CreateClass(props);
object obj = Activator.CreateInstance(type);
t.GetProperty("Name").SetValue(obj, "Albert", null);
t.GetProperty("Birthday").SetValue(obj, new DateTime(1879, 3, 14), null);
Console.WriteLine(obj);

How to build a static code analysis tool?

I m in process of understanding and building a static code analysis tool for a proprietary language from a big company. Reason for doing this , I have to review a rather large code base , and a static code analysis would help a lot and they do not have one for the language so far.
I would like to know how does one go about building a static code analysis tool , for e.g. Lint or SpLint for C.
Any books, articles , blogs , sites..etc would help.
Thanks.
I know this is an old post, but the answers don't really seem that satisfactory. This article is a pretty good introduction to the technology behind the static analysis tools, and has several links to examples.
A good book is "Secure Programming with Static Analysis" by Brian Chest and Jacob West.
You need good infrastructrure, such as a parser, a tree builder, tree analyzers, symbol table builders, flow analyzers, and then to get on with your specific task you need to code specific checks for the specific problems of interest to you, using all the infrastructure machinery.
Building all that foundation machinery is actually pretty hard, and it doesn't help you do your specific task. People don't write the operating system for every application they code; why should you build all the infrastructure? Like an OS, it is better if you simply acquire good infrastructure.
People will tell you to lex and yacc. That's kind of like suggesting you use the real time keneral part of the OS; useful, but far from all the infrastructure you really need.
Our DMS Software Reengineering Toolkit provides all the necessary infracture. It has been used to define many language front ends as well as
many tools for such languages.
Such infrastructure would allow you to define your specific nonstandard language relatively quickly, and then get on with your task of coding your special checks.
There is a blog by DeepSource that covers everything one needs to know to build an understanding of static code analysis and equip you with the basic theory and the right tools so that you can write analyzers on your own.
Here’s the link: https://deepsource.io/blog/introduction-static-code-analysis/
Obviously you need a parser for the language. A good high level AST is useful.
You need to enumerate a set of "mistakes" in the language. Without knowing more about the language in question, we can't help here. Examples: unallocated pointers in C, etc.
Combine the AST with the mistakes in #2.

Why is there excessive use of whitespaces in expressions in most sample code? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
A statement that could be written as:
foo=(bar*5)+baz;
is usually written in sample code (documentation, tutorials etc) as:
foo = ( bar * 5 ) + baz;
This appears to require extra work and seems counter-productive to me. Is there a rational reason for this? Is this a good coding practice, or just for sample code?
(The reason I ask this is to make sure my coding style is right, and understand why most code I see online is written like this).
I don't put spaces after ( or before ), but I know people that do; other than that it's how I would write it:
foo = (bar * 5) + baz;
I think it's easier to read, and about the tiniest amount of "extra work" you could possibly create. I used to code without much spacing and now I look back and think it looks terrible. There is no "right" coding style though; if it's your project format the source code however you want
It's no extra work with an IDE that applies the spacing according to your preferences. FWIW, my favourite spacing here would be:
foo = (bar * 5) + baz;
which is not quite a lengthy as your second example, but to my mind gives the right balance between brevity and readability. And ultimately that's what it's all about. It doesn't affect what it compiles to, so if it makes it easier to read, what's wrong with it?
That's how I write all my code - and it's how you should write all of yours.
Its easier to read, that's it.
Usually I place whitespace around operators, not braces. The primary reason is readability.
As a rule of thumb, "punctuate like you would in a natural language". People read more easily if what they see matches the patterns they expect. They expect a space after semicolons, commas, etc....
To increase the readability of sample -- especially if its the first time you're looking at it and you are trying to understand how it works.
only for clarity.
Because it is more readable in most cases, although I'd skip the spaces after the opening parenthesis and before the closing parenthesis. Furthermore, this doesn't have to cost more time to write, due to formatting tools like in Eclipse. Here we can write down the short version and Eclipse will insert the spaces automatically. You can even configure the style that you like.
Spacing helps you see what terms are supposed to be grouped together.
The example you give is pathological: the parentheses are redundant and the spacing counterproductive, but look at these examples:
foo = bar * 5+baz;
foo = bar*5 + baz;
The spacing in the first line groups the substring 5+baz together, which in turn suggests that + has, contrary to fact, the highest precedence.
If you're worrying about the extra work to type anything, you're probably running at risk of creating write-only code. I never worry about how long something is to type in, the difference of a few more characters here and there doesn't matter compared to the time you might save, by it being clearer, in a few months when you're trying to remember how it works.
Just write it whatever way looks clearest to you and the other people in your team.
Just because you CAN write code without whitespaces doesn't mean you SHOULD. Ifmysentencesdidn'thaveanyspacesthey'dbemuchhardertoreadtoo.
Distinct elements in code should be visible on their own. As you get older (or your eyesight gets worse), you'll appreciate not having to squint at the screen to make out the text, and the people you work with will appreciate the readability of your code.

Which tools can help in translating (as in french -> english and not C++ -> java) source code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have some code that is written in french, that is the variables, class, function all have french names. The comments are also in french. I'd like to translate the code to english. This will be quite a challenge, since it's a 18K lines project and I'd like to know if there is any tool that could help me, especially with the variables/class/function names, since it will be error prone to rename them all.
Is there any tools that can help me? Advices?
edit : I'm not looking for machine translation. I'm looking for a tool that would help me translate the code. Let's say there is class name C and this class has a method named TraverserLaRue and I rename it CrossTheRoad I'd like all references to TraverserLaRue in all files to be translated as CrossTheRoad. However I don't want the method TraverserLaRue of class B to be translated.
I assume the langauge in question is one of the common ones, such as C, C++, C#, Java, ...
(You don't have a language with French keywords? I once encountered an entirely Swedish version of Pascal, and I gave up on working that).
So you have two problems:
Translating identifiers in the source code
Translating comments
Since comments contain arbitrary natural language text, you'll need an arbitrary translation of them. I don't think you can find an automated tool to do that.
Unlike others, however, I think you have a decent chance at translating the identifiers
and changing them en masse.
SD makes a line of source code "obfuscator" products. These tools don't process the code as raw text, rather they process the source code in terms of the targeted language; they accurately distinguish identifiers from operators, numbers, comments etc. In particular, they
operate reliably as need on just the identifiers.
One of the things these tools do is to replace one identifier name by another (usually a nonsense name) to make the code really hard to understand. Think abstractly of a map of identifier names I -> N. (They do other things, but that's not interesting here). Because you often want to re-obfuscate a file that has changed, the same way as an original, these tools allow you to reuse a previous cycle's identifier map, which is represented as list of I -> N pairs.
I think you can abuse this to do what you want.
Step 1: Run such an obfuscator on your original French code. This will produce a text file containing all the identifiers in the code as a map of the form
I1 -> N1
I2 -> N2
....
You don't care about the Ns, just the I's.
Step 2: Manually translate each French I to an English name E you think fits best.
(I have no specific suggestions about how to do this; some of the other answers here
have suggestions).
Some of the I's are likely to be library calls and are thus already correct.
You can modify the text obfuscation map file to be:
I1 -> E1
I2 -> E2
Step 3: Run the obfuscation tool, and make it use your modified obfuscation map.
It can be told to do that.
Viola, all the identifiers in your code will be changed the way you specify.
[You may get, as a freebie, the re-formatting of your original text. These tools can also format code nicely. Your name changes are likely to screw up the indentation/spacing in the original text so this is a nice bonus].
Any refactoring tool has a rename feature. Many questions on SO address language specific refactoring tools.
For the comments, you will have to handle them manually.
I did this with German code a while ago, but had mixed results because of abbreviations in names, etc. Using regular expressions, I wrote a parser that removed all of the language specific keywords and characters, then separated comments from the rest of the code, and now I had a lot of words that didn't necessarily mean anything to me by themselves. So I wrote a unique word finder that added them all to a ordered text file. Next stop was Google's language tools that attempted to translate every word in the list. I ran through the list to see if each word really translated, and if it did, I did a replace all in the code with the english equivalent. The comments I put back in with the complete translation, if it worked. What I found was that I ended up having to talk with someone who understood "Germish" to translate the abbreviations, slang terms, and mixed language pieces. So in short, regular expressions with a dictionary, unless someone has a real tool for this, which I would be interested in also.
You should definitely look into https://launchpad.net/rosetta
Ubuntu uses this to translate thousands of its packages written in hundreds of programming languages into hundreds of human languages, with updates for each new version. Truly herculean task.
edit: ...to clarify how Rosetta is used at Ubuntu: it modifies all natural language strings occuring in source code of the open-source apps, creating a language-specific source packages, which upon compiling create given kinds of binaries. Of course it does not edit binaries themselves.
First maintainers create "template files" which are something like "Patch with wildcards" - a set of rules what and where in the source tree needs to be translated, but not to what. Then Rosetta displays strings to be translated, and allows volunteering translators to provide translations to their language for each entry. Each entry can be discussed, modified, suggestions submitted and moderated. Stats are provided how much needs to be translated, which translations are unsure, which are missing etc. When the translation is complete, patch of given language is applied to the source creating its version for given language. Then a distribution is compiled from the modified sources.
This allows translation both for sources that use some external resources for multilingual allowing for language change on the fly, and for ones that have literal native language strings right in the source code, mixed with business logic.
When a new version of the package is released, template must be edited to include all new strings but it has quite good automation for preserving the existing ones. Of course only translations for new strings are required.
IMHO automatic tools won't be of any help here. Just translating variable and function names is not enough and will make the code worse because they cannot infer the original programmer intent when he choose a variable name.
Depending on what programming language this code is written to there are modern IDEs that might ease the refactoring but if you want to have good results manual code review is a must.
A good IDE will be able to list classes, methods, variables. There's also documentation generation tools that'll do that such as Javadoc for Java, Doxygen for many languages, etc.
To do the actual translation, there will be no tool that will perform well, or even to a satisfactory level. The only way to get something worthwile is to have a bilingual translator translate the terms. I've been doing freelance translations for many years, and can tell you that trying to have some machine do the translating is a waste of time. Many examples, choice of words, will be relevant to your culture and not the other. And that's just the tip of the iceberg.
Unless you find someone that can do the translation, I suggest you abandon the idea. Leave the source code as is. If a non-French speaker reads it, and needs to understand something, let them do the Google lookup. If they are native English speakers they'll probably do a better job of understanding the automatic translated stuff than you would, being French. When translating, you always want to translate into your native language.
For translating only comments you may try this simple utility I wrote (it's using Microsoft's Translator API): transource.

Enforcing a coding style [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Years ago when I was starting a small development project, the other developers and I sat down and agreed on a compromise brace and indentation style. It wasn't anybody's favourite, but it was something that nobody really hated. I wrote a .indentrc configuration file to that style, and had a check-in trigger that would run indent on every file as it was being checked in. That made it so that it didn't matter what style you wrote your code in, it would end up being the group standard before anybody else saw it. This had the advantage of consistency. But I've never seen anybody else do it this way before or since.
So what say the rest of you? Great idea, or abomination?
I'd say good idea. I'd take it a step further and make everyone use the config file in their IDE so that they were writing in the agreed upon style by default. If they're going to have to look at everyone else's code in the neutral style, they might as well get used to it. Even their own code should be in the neutral style after one check-in check-out cycle, so why develop new code in their own personal style?
Adopting a neutral coding style is definitely a good idea. However, only enforcing the coding style when the source is checked in may or may not be a good idea (see also Bill's and Elie's answers below).
Using the check-in hook:
Pro: Allows coders to write however they wish, so they don't have to think about the standard or change the way they write code. This minimizes resistance to the policy, and won't negatively impact their productivity when writing code.
Con: Your coders may have only a passing familiarity with the neutral style, so you aren't getting the full benefit of everyone using "the same" style. If your programmers ever have to work together in a pair programming setup, they are still going to be subjected to one another's programming style on the screen, which is going to be different from their own style or the neutral style.
Going one step further, using the neutral style during development:
Pro: Encourages fluency in the neutral style, everyone can always read everyone else's code before and after it's checked in.
Con: You'll encounter more resistance from your developers doing it this way. Depending on your culture, it could be more trouble than it is worth.
If you limited it to enforcing style on braces and indentation, then I think it's a good idea. However, if you were to try to enforce every single formatting standard, then it probably wouldn't be. In my opinion, there are times when it makes sense to break the standard. For example, I prefer
int x = y * z;
to
int x = y*z;
because it's easier to read. However, I vastly prefer
int a = b*c + d*e;
to
int a = b * c + d * e;
because the spacing represents the order of operations.
So your policy of enforcing indentation and braces sounds really good. But if someone ever tried to blindly enforce other spacing rules, I don't think it would work well.
That sounds like a good idea. As long as the style you end up with is not something completely strange, that's a good way to make sure your developers use the style. And it has the added benefit that they don't have to code that way - it will get reformatted for them when they check in their changes. It would be nice to have a tool like that available that you can plug into your CVS (generic term).
We use TFS with a check in policy that runs a set of Stylecop rules. If your code doesn't pass, you can't check it in. Works very well indeed. Aside from a consistant style and good commenting throughout, it also seems to have increased the general quality of the code - perhaps because the developer is forced to describe what every method, event, etc does they are forced to think about the code more before check in.
Only an MS solution, but worthwhile if it's available to you.
The biggest problem with using automatic code formatters is when the code formatter can't handle every scenario.
For example, if you have lots of SQL in your code, you probably format the SQL automatically. But if your SQL is longer than one line (how long is a line, anyway?) then
you have to format it. So far I've yet to see a good formatter than can deal with this properly.
Example:
String sql = "SELECT * FROM USERS WHERE ID = ? AND NAME = ? AND IS_DELETED = 'N'";
vs
String sql =
"SELECT * " +
"FROM USERS " +
"WHERE ID = ? " +
" AND NAME = ? " +
" AND IS_DELETED = 'N'";
The second format is more readable when you have really long queries. Most formatters would de-format that into one long line up to the line length.
However if all you're doing is turning
if(x=1) print("blah"); else print("eep!");
into
if (x = 1) {
print("blah");
} else {
print("eep!");
}
then the formatter is ok. We do something similar at work; it isn't enforced by the CVS tool but rather by the IDE. Works reasonably well.
There is a project called EditorConfig can somewhat solve the issue. However, it currently only solves indentation issues.
EditorConfig contains plugins for many different editors, and a file format standard. By creating an .editorconfig file in the root of your project, and installing the corresponding plugin, the editor will format your code when you type them.
This is a general way (unlike indentrc, not limited to C/C++), but you can still take a look at this solution.
I believe you have decided on your development environment by now. If you use Eclipse you can enable the Format Source save action on the Java editor, which reformats at every save. The major benefit from this is that source chances are marked in your source repository at the time when they were done, not when the source was reformatted later.
Make it an automatic step. You will appreciate it later.

Resources