Unified coding style for Javascript, PHP, C, and C++ - php

I'd like to have one coding style across multiple languages. Can anyone provide a link to code that is styled nicely, that I can use for each language?
It is easiest to just look at an example rather than read for the multitude of cases that arise.

There's not a chance in hell. There's no such thing as code that's styled nicely for C and C++, let alone across the other languages too. You haven't exactly picked C# and Java here in terms of language similarities.
Edit: Do not, ever, use the Google Style Guide. It is horrendous and terrible. It was only ever devised to make C++ compatible with C, which basically means not using virtually all of the useful language features and writing some terrible code. And even then, you'd never make it compatible with JavaScript and PHP.

Wikipedia has an article that lists several C coding styles. Anything that works for C should work similarly for all the languages you ask for with little additional thinking.

Javascript - Probably not compares with the others.
Worth reading the coding styles from ESA wrt Java, C++.
Also getting documentation out of Doxygen is another good starting point.

It's really a matter of taste. There are no definite standards to follow in this case.
See the many questions on this subject:
Are there any coding standards for JavaScript?
PHP coding standards
Coding style checker for C
And more. All you need to do is search :)

I would suggest something like this, it seems the way most people are heading, at least on the non-microsoft side of things:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
Although that is for C++, it also applies to the c-like languages you mention. You can look at example code Google provides for other languages, it all tends to stick this this general style. For instance the code within these pages:
http://code.google.com/apis/maps/documentation/javascript/basics.html
and
http://code.google.com/apis/calendar/data/1.0/developers_guide_php.html

Related

PHP preprocessing script? [duplicate]

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. Does anything like this exist?
I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!
The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.
Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.
JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)
All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.
Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.
If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.
I've only ever used it for Flash but found it very useful.
If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.
#gosukiwi made Blueberry, which looks like this:
/*
I'm a multiline comment
*/
a = 1 # variable definition
# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }
if a == 1
echo("Hello, World!")
end
for i in (0..10)
echo(i)
end
class MyClass < MyParentClass
#name
def Greet
echo("Hello! My name is " & #name)
end
end
They also mentioned it in this comment.
Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.
I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.
https://github.com/c9s/coffeephp
Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)
This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor
Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

Is there anything like CoffeeScript for PHP?

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. Does anything like this exist?
I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!
The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.
Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.
JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)
All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.
Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.
If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.
I've only ever used it for Flash but found it very useful.
If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.
#gosukiwi made Blueberry, which looks like this:
/*
I'm a multiline comment
*/
a = 1 # variable definition
# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }
if a == 1
echo("Hello, World!")
end
for i in (0..10)
echo(i)
end
class MyClass < MyParentClass
#name
def Greet
echo("Hello! My name is " & #name)
end
end
They also mentioned it in this comment.
Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.
I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.
https://github.com/c9s/coffeephp
Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)
This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor
Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

CoffeeScript-esque language for PHP? [duplicate]

PHP interpreters are very common, but the PHP syntax & libraries are inconsistent & cumbersome (IMO, of course). I think a language that compiles into PHP but provides higher level level features (like, modules, mixins, list comprehensions, etc...) and easier syntax (like optional semicolons, implied returns, no dollar sign for variables, optional brackets and braces, etc...) would be valuable. Does anything like this exist?
I've been researching this a lot and at the moment it seems the answer is no. I'm the author of exactly such a project called Snowscript - it is far from complete, but the documentation is pretty good and some things do work. Would love to hear feedback of what you think about the syntax!
The short answer is "no." CoffeeScript rose to popularity because of a unique confluence of factors. For one, as Wesley points out, JavaScript has a monopoly on the browser platform, while PHP only has a monopoly on .php files. On your own servers, if you don't like PHP, you can just use Ruby, Python, Perl, or any of the myriad JVM or .NET languages.
Another factor is that JavaScript's design was something of an accident. Its creator, Brendan Eich, was told to "make it look like Java"; but semantically, it has more in common with Lisp and Smalltalk. CoffeeScript arguably provides a syntax that's a better fit with JavaScript's inner workings.
JavaScript's own syntactic evolution is severely hindered by the need to maintain compatibility with older browsers. PHP suffers no such limitations, as anyone who's transitioned their code from PHP4 to PHP5 can attest. If you want to make JavaScript a better language, you need a precompiler. If you want to make PHP a better language, post a feature request for PHP6. (Edit: In my original answer, I fell for an April Fool's joke claiming that PHP6 had been released in 2010. Obviously I'm not a PHP guy...)
All of that said, it could be cool to have a language that's like CoffeeScript for PHP. The ongoing success of WordPress, and its use on servers that users often have little control over, attests to PHP's unique place as a deployed language. It's also difficult to use PHP with alternative markup languages like Haml. Perhaps an alternative markup language combined with a fresh PHP syntax could produce a compelling enough reason for people to precompile their PHP.
Browsing and surfing the web I've found http://mammouth.boutglay.com/ looks like the most similar to coffee-script language for PHP. Seems to do the job.
If I've understood what you want correctly, then there's Haxe, which can target PHP, as well as Flash, JavaScript, and others.
I've only ever used it for Flash but found it very useful.
If you like Lisps, have a look at Pharen. I haven't needed to use it yet, but it looks pretty nice - it has defmacro and even transforms tail recursion into loops.
#gosukiwi made Blueberry, which looks like this:
/*
I'm a multiline comment
*/
a = 1 # variable definition
# you can use JSON syntax to define associative arrays
arr = { "name": "Mike", "age": 18, "meta": { "items": [1, 2, 3] } }
if a == 1
echo("Hello, World!")
end
for i in (0..10)
echo(i)
end
class MyClass < MyParentClass
#name
def Greet
echo("Hello! My name is " & #name)
end
end
They also mentioned it in this comment.
Currently there is no production-ready or completed coffeescript-like language/compiler for PHP.
I am the author of CoffeePHP, and is working on the compiler for the shorter syntax. it's actually another language.
https://github.com/c9s/coffeephp
Of course, you might be aware of this, but you could simply use nodejs with CoffeeScript... (unless you're specifically attached to PHP)
This library isn't like CoffeeScript, in itself, but it's a foundation for rewriting PHP to declare and use your own syntax. I don't have any experience with it, so don't read this as an endorsement, just an observation. https://github.com/theseer/preprocessor
Take a look at coffescript-php project which is compatible with coffescript 1.3.1 on github can be found at https://github.com/alxlit/coffeescript-php

Meta programming into several output languages

I'm looking for a way to have to write and maintain a certain algorithm (a graphics rendering sub-module of my code, actually) only once. I need the algorithm in C++, PHP and Javascript. Theoretically I could write it in C++ and wrap it into a PHP extension; but that has many issues of itself and doesn't solve the Javascript link.
What I'm looking for, I think, is a tool that converts from a language (doesn't matter which one) into the three (or two, if the source language is one of the three) output languages I'm targetting. I've found MetaL (http://www.meta-language.net/) which seems to do what I want but also looks dead (no updates since 2007) and only targets one of the three languages I need. It needs to be quite flexible and allow me to update the results - for example, I use Cairo in my C++ and PHP rendering, and HTML Canvas on the Javascript side. So I need to customize to the API for certain effects.
Alternatively, I'd settle for a PHP parser and lexer that would give me an AST with enough information for me to write generators for C++ and Javascript as an alternative backend.
Any ideas? Thanks.
You could take a look at Haxe. Haxe is an open source programming language. It can be compiled to JavaScript, Flash/ActionScript, PHP, C++, Java, C#, Python and Lua.
The Emscripten project (which I only spotted last week) might interest you: http://syntensity.blogspot.com/2011/04/emscripten-10.html
This guy has basically written a compiler for C/C++ that compiles to Javascript code.
That should solve the Javascript side of your problem.
Hope that helps.
Another product along the same lines, and a bit more well known is Google Web Toolkit (GWT). It's based on Java, but the end result is similar -- you write your web application in Java code and it compiles the front-end parts into Javascript and the back-end parts into regular Java bytecode. I know you're not asking for Java, but if it interest you, the link is here: http://code.google.com/webtoolkit/
Slightly less useful, but possibly more relevant to your question is PHPJS. This is a project to implement as much of the PHP language in Javascript as possible. They're doing it on a function-by-function basis, so it's only ever going to be an approximation, but given that the language syntaxes are similar, it may be possible to use it to write code that works unchanged in native PHP and also in Javascript on the client side.
Of course the one big down-side of compiling one language into another is that the resulting code is always going to be sub-optimal. There's not much you can do about that, but it's worth bearing in mind before you start down the path of writing a shared code-base in a single language.
Maybe look into 'coding' your original algorithm in xml and using various xslt templates to output to your target languages ? Or possibly antlr (http://www.antlr.org/ http://www.amazon.com/Definitive-Antlr-Reference-Domain-Specific-Programmers/dp/0978739256/ref=sr_1_1?s=books&ie=UTF8&qid=1303114884&sr=1-1).
Maybe you can just Write it in javascript and then use a C++ and PHP javascript interpreter.
A completely different approach would be to use assembly code. Write the algorithm in a language of your choice, compile it to ASM source. Then provide the interface wrappers in the deployment languages.
Of course this is all so much 'air pie'. It depends upon so many variables, number of target platforms, importance of optimization, frequency of interface change related to implementation change etc etc

Generating php from delphi - has anyone done it?

See How to escape quote PHP strings generated by Delphi?
I am just interested to hear if anyone has used Delphi (or possibly BCB) as a code generator for PHP ...
(or thoughts about code generation from one language to another in general)
Hmm, any good books about code generation ?
I've generated javascript, SQL and Delphi many times. But mostly is basic substitution, (and the example in the post you mention looks the same), not really codegeneration in the "compiler" sense of the word.
But there are also many real compilers in Pascals and Delphi like dialects. The biggest one I think is Free Pascal (http://www.freepascal.org), which is a compiler for Object Pascal (aka delphi)
(added later:)
Besides variable substitution, basic templating engines also fall in this category. Templates are sometimes easier maintainable than the same fragement code. Specially in html/cgi land this is used a lot.
You can generate anything from a tool which can export text files no?
You can write all by the hand, or in a "delphi style" by using Delphi for PHP http://www.embarcadero.com/products/delphi-for-php
best regards,
anyone has used Delphi (or possibly BCB) as a code generator for PHP
PHP - no, but I'm generating a lot of Delphi/Pascal code from Delphi. I've also generated all other things used for a web application: HTML, JavaScript, CSS - but never PHP because I didn't need that. So it's possible, but simply knowing it's possible is not going to help you much.
thoughts about code generation from one language to another in general
You need to look into "text template engines" for Delphi. I can't suggest any because I wrote my own (and I'm not planing on releasing my own under any license).

Categories