Learning a New Programming Language Part 3: Syntax Differences
(Page 1 of 7 )
Syntax differences aren't always the hardest part of learning a new language, but they can be an issue. In part 3 of my three part series on learning a new programming language, we examine syntax differences in many of the more common languages you may want to learn.
Code Comments
The character used for commenting code can vary. Many languages use C type comments. In C, a single line comment uses two forward slashes "//" while a multiline comment uses a forward slash followed by an asterisk, followed by the comment text, and then an asterisk and forward slash.
/*This is an example of
a multiline C comment*/
Other languages, such as Perl and Ruby, use a "#" character for single line comments. Multiline comments for both these languages end with "=end". Perl starts multiline comments with "=cut" while Ruby uses "=begin".
Comments can also be useful for documentation outside of the source code. There are applications for several languages that capture comments from code and automatically generate documentation from it. This can save a lot of time in developing documentation, provided the output makes some sense to those that don't know anything about the code ahead of time.
There is a lesson to be learned about comments. Not long ago you may have heard that some source code for Microsoft's Windows 2000 was leaked onto the Internet. There was talk of how this might affect Microsoft's trade secrets and what not, but what garnered the most attention were the comments contained in the code.
The comments did not contain any important secrets but many comments contained potentially embarrassing language as well as revealing the concern from Microsoft developers that the operating system was being patched and hacked together to make up for serious coding flaws in other Microsoft products, in order for them to run as advertised.
The lesson here? Never put anything in code comments that you wouldn't want your boss, the client or the general public to see.
Other Syntax Differences
Many procedural and OO languages have minor differences in syntax. Though they may be minor, if you are used to the syntax of one language to the point where you could practically write it in your sleep, getting used to another may cause some snags.
A good example is line endings. Languages that use C type syntax require code lines to end with a semicolon. In Javascript this can be optional, though it is good practice to include them. If you leave them out in PHP, the interpreter will throw an error.
Some languages, such as Python and Ruby, don't require line ending characters. In fact, in Python, where a block of code begins and ends is determined by the indentation. If you are used to logically indenting your code in another language, you will likely be able to write blocks of code in Python without error.
Other languages use begin and end keywords to mark the boundaries of blocks of code, such as in if and else constructs. If you're not used to this, you will see errors.
Next: Defining Variables >>
More Web Hosting How-Tos Articles
More By Chris Root