473,657 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript Code Styling

I'm starting to do more quantity of javascript coding and thought this
might be a good time to investigate code styling. I primarily develop
in Java and currently use the code styling techniques spelled out for
Java.

For example, in my code blocks ( {} ) I always place the starting brace
on the same line as the first line of code. For example:

function myFunction {

But I've also noticed a lot of developers move it to the next line:

function myFunction
{

I also place a semicolon at the end of each line, although I see it's
also not mandatory.

Third (and lastly) I also declare all Objects in first letter uppercase
and all member variables/functions in lowercase. For example:

function MyObject() {

var myFirstMemberVa r;

function doSomething() {
....
}
}

Is there a coding standard for Javascript? Do my current practices seem
feasible?

Thanks in advance.

Sep 28 '06 #1
27 3334
Hi,

Tom Cole wrote:
I'm starting to do more quantity of javascript coding and thought this
might be a good time to investigate code styling. I primarily develop
in Java and currently use the code styling techniques spelled out for
Java.
A few years ago, when we (I work at Siemens) started working on our
current wep application, we decided to write our own JavaScript coding
guidelines. Mostly, they are inspired of our C/C++ coding guidelines and
also of Java guidelines (because code found online mostly follows the
Java guidelines for capitalization for example).

For example, in my code blocks ( {} ) I always place the starting brace
on the same line as the first line of code. For example:

function myFunction {

But I've also noticed a lot of developers move it to the next line:

function myFunction
{
This is mostly a matter of taste. Our guidelines allow both but
recommend the second one, which we find is more readable. If you work
with Visual Studio 2003/2005, it's easy to configure

I also place a semicolon at the end of each line, although I see it's
also not mandatory.
Not mandatory in JavaScript, but it's very recommended. We made it
mandatory in our guidelines. If you use a code minimizer like JsMin,
placing more than one statement on one single line, then the ';' is
mandatory anyway.

Third (and lastly) I also declare all Objects in first letter uppercase
and all member variables/functions in lowercase. For example:

function MyObject() {

var myFirstMemberVa r;

function doSomething() {
....
}
}
This is also what we recommend. However, we also recommend hungarian
notation for variables. This goes against our C# guidelines, but has a
reason: In JavaScript, objects are not strongly typed. Thus they may
contain a type first, and then another type. However, to avoid
confusion, our guidelines specify that this must be avoided, and a
variable must carry a single type all along its life. To facilitate
this, hungarian notation is used, for example strMessage (string),
iValue (integer numbers), fValue (float numbers), etc... Note that
JavaScript doesn't differentiate integer and float, so it's really just
to help the developer.

Generally, JavaScript coders follow the Java naming guidelines, like you.

Is there a coding standard for Javascript? Do my current practices seem
feasible?

Thanks in advance.
Yes, they seem very reasonable.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 28 '06 #2
Laurent Bugnion wrote:
<snip>
>For example, in my code blocks ( {} ) I always place the
starting brace on the same line as the first line of code.
For example:

function myFunction {

But I've also noticed a lot of developers move it to the
next line:

function myFunction
{

This is mostly a matter of taste. Our guidelines allow both
but recommend the second one, which we find is more readable.
<snip>

I have a feeling that the one of the two that is found more readable by
individuals is the one with which they are more familiar. I find the
former more readable. But I suspect that the most important thing is to
be consistent and use only one or the other (certainly within any single
project, and probably within any single company).
Generally, JavaScript coders follow the Java naming
guidelines, like you.
I certainly do.
>Is there a coding standard for Javascript? Do my current
practices seem feasible?

Thanks in advance.

Yes, they seem very reasonable.
Absolutely.

Richard.
Sep 29 '06 #3

Laurent Bugnion wrote:
Hi,

Tom Cole wrote:
But I've also noticed a lot of developers move it to the next line:
function myFunction
{

This is mostly a matter of taste. Our guidelines allow both but
recommend the second one, which we find is more readable.
We do a lot of JS, and we prefer using the next line also. The old way
of using the same line is no doubt related to early terminal / memory
problems.
[...] hungarian notation is used, for example strMessage (string),
iValue (integer numbers), fValue (float numbers), etc... Note that
JavaScript doesn't differentiate integer and float, so it's really just
to help the developer.
We use these, plus oReference for objects.

Cheers , Kev

Sep 29 '06 #4
Hi Richard,

Richard Cornford wrote:
>>This is mostly a matter of taste. Our guidelines allow both
but recommend the second one, which we find is more readable.

<snip>

I have a feeling that the one of the two that is found more readable by
individuals is the one with which they are more familiar. I find the
former more readable. But I suspect that the most important thing is to
be consistent and use only one or the other (certainly within any single
project, and probably within any single company).
Yes. We talked for a while about it, and decided to let the programmers
decide (for the record, the development lasted 2.5 years, involved about
30000 lines of JavaScript code, and we had 6 developers writing
JavaScript. Most of them had zero experience with the language, but are
very good C/C++/C# programmers. After a few internal courses, they were
totally up to the task). Mostly in my firm, the second form (bracket on
a different line) is used. This is probably why we found it more
readable. But as I said, it's very much personal.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 29 '06 #5
Hi,

Kevin Darling wrote:
Laurent Bugnion wrote:
>>[...] hungarian notation is used, for example strMessage (string),
iValue (integer numbers), fValue (float numbers), etc... Note that
JavaScript doesn't differentiate integer and float, so it's really just
to help the developer.


We use these, plus oReference for objects.

Cheers , Kev
Yes. We actually have more than these. For example, we also use "n" for
nodes (objects returned by document.getEle mentById). We made a list of
prefixes and strongly encourage the developers to use it. It's no
problem for them, as they were using hungarian notation in C++. It's
rather a problem that they have to abandon it in C# (we follow
Microsoft's recommendation for C#, for many reasons, including sharing
code with other Siemens divisions).

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 29 '06 #6

Tom Cole написав:
For example, in my code blocks ( {} ) I always place the starting brace
on the same line as the first line of code. For example:

function myFunction {

But I've also noticed a lot of developers move it to the next line:

function myFunction
{
Few days ago I found JS Editor and Code Chameleon developed by C-Point.
That tools can reformat any source and may be very useful sometimes. It
can be used as a reformatter ot beautifier.
I also place a semicolon at the end of each line, although I see it's
also not mandatory.
The semicolons may became a subject of one more Holy War :)
All programmers who used C/C++ before JS always use semicolons. I never
used C/C++ (we learned Pascal in school and college) and use semicolons
rarely. I think it is a benefit of language - free usage of semicolons.
And if some code minifier kills your code because of missing semicolons
then it is bad code minifier. Good code minifier never acts like this.

Sep 29 '06 #7
sc********@gmai l.com wrote:
Tom Cole написав:
<snip>
>I also place a semicolon at the end of each line, although I
see it's also not mandatory.

The semicolons may became a subject of one more Holy War :)
All programmers who used C/C++ before JS always use semicolons.
This would probably be true of anyone coming form any language that
required semicolons to terminate lines. Overall Java probably has the
biggest influence on code styling in javascript (though maybe that is
not such a good idea).
I never used C/C++ (we learned Pascal in school and college) and use
semicolons rarely. I think it is a benefit of language - free usage of
semicolons.
Javascript doesn't really have free use of semicolons. Intact they are
not even optional, they just may be inserted automatically so do not
need to be in the source code. the problem with that is that you need
to appreciate when they will be inserted when writing anything but the
most 'normal' code. For example, two consecutive instances of the
inline execution of a function expression:-

(function(){
...
})();
(function(){
...
})();

- Omit the semicolons to give:-

(function(){
...
})()
(function(){
...
})()

- and the second parenthesised function expression becomes an argument
to a call to the return value from the call to the first function
expression, and the final set of parenthesise then become a call the
return value from that call. That is, something along the lines of:-

(((function(){; })())(function( ){;}))();

- (which is completely legal and valid javascript)

The error is likely to be along the lines of "function expected" on the
line at the start of the second function expression, not easy to work
out what happened.
And if some code minifier kills your code because of missing
semicolons then it is bad code minifier. Good code minifier
never acts like this.
Which is not a reasonable comment on JsMin as JsMin requires that its
input first passes JsLint, and JsLint insists upon
statement-terminating semicolons as one of its 'best practice' criteria
for source code.

Richard.

Sep 29 '06 #8
Tom Cole wrote:
For example, in my code blocks ( {} ) I always place the starting brace
on the same line as the first line of code. For example:

function myFunction {

But I've also noticed a lot of developers move it to the next line:

function myFunction
{
I prefer the first form because it works better when a function literal is used
inline as an expression, particularly when passing a function as a parameter. In
that circumstance (which I use a lot), the second form reads badly.
I also place a semicolon at the end of each line, although I see it's
also not mandatory.
It is wise to include the semicolons. Semicolon insertion was intended to make
JavaScript more reliable, but dependence on it makes programs less reliable.
Third (and lastly) I also declare all Objects in first letter uppercase
and all member variables/functions in lowercase. For example:

function MyObject() {

var myFirstMemberVa r;

function doSomething() {
....
}
}
What you are calling an Object there is usually called a Constructor. It is very
good practice to capitalize the names of constructors. The reason is that
calling a constructor without the new prefix can cause clobbering of the global
object, which is very bad. There is no compile-time or run-time notification of
this error. The only defense we have is a coding convention that we should use a
new prefix when invoking a capitalized function. (The exceptions are when
invoking String, Number, and Boolean which should never be called with the new
prefix.)
Is there a coding standard for Javascript? Do my current practices seem
feasible?
This is what I use: http://javascript.crockford.com/code.html
Sep 29 '06 #9

Richard Cornford написав:
Javascript doesn't really have free use of semicolons. Intact they are
not even optional, they just may be inserted automatically so do not
need to be in the source code. the problem with that is that you need
to appreciate when they will be inserted when writing anything but the
most 'normal' code.
Yes, I know that JS parser/interpreter automaticaly inserts semicolons.
Thats why I love it :)
I am javascript/actionscript developer with ~6 years of experience. My
applications sometimes became really complicated (and I trying to use
full potential of JS). But no even one "Missing semicolon" errors or
any other problem with semicolons for last years.
And if some code minifier kills your code because of missing
semicolons then it is bad code minifier. Good code minifier
never acts like this.

Which is not a reasonable comment on JsMin as JsMin requires that its
input first passes JsLint, and JsLint insists upon
statement-terminating semicolons as one of its 'best practice' criteria
for source code.
I never said that JsMin or JsLint are bad or even not good enough. But
I saw many code minifiers that will destroy code if semicolons are not
used.

Val Polyakh
trickyscripter. com

Sep 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1410
by: Norman Swartz | last post by:
OS system is Windows XP Pro SP2. The following works perfectly in IE but not at all in Netscape: <tr> <td valign="middle" background="dull.gif" onMouseOver="this.background='bright.gif';" onMouseOut="this.background='dull.gif';" bgcolor="#FFFFFF"> <img src="vspacer.gif" height="46" width="20" align="left">
3
6673
by: Dan | last post by:
Hi Does anyone know of a good Javascript typewriter ticker that allows you to insert HTML into the ticker. I have found lots but when HTML is inserted, the ticker pauses at the point it reaches the HTML as well as applying the formatting. I need one whose character timer ignores characters in tags. I need to apply font color and weight styling to different words. Please see www.launchpr.co.uk for how the ticker currently looks in
15
4213
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is avaliable at... http://www.geocities.com/binnyva/code/javascript/advanced_tutorial/ If any of you could spare the time, please have a look at my tutorial
1
2140
by: Spartanicus | last post by:
I'm pondering what the various drawbacks are of the methods to code UI elements who's function relies on javascript and css. Currently on http://www.pan-europe.utvinternet.ie/ I use styled <a> elements to display two UI elements that via javascript select alternate stylesheets that enable or disable the left utilities panel and/or the navbar. (both are there for the convenience of desktop users, they are not essential, likely to be a...
1
1127
by: xx75vulcan | last post by:
Howdy! I've got a javascript menu that was handed to me. I believe it was hand coded- but not sure. I need to find a way to specify the font color and size of the menus. Currently, the menu is embeded into an html page that links a CSS file. The BODY tag in the CSS file will tell the javascript menu what to do, but the problem is, i need the javascript menu to have different font
2
1908
by: Reinhold Schrecker | last post by:
Hi, I am trying to generate a pulldown-menu with JavaScript/DOM: The following Code works fine with Opera an Mozilla but in the IE the width of the select element is too short: myCurrentElement = window.document.getElementsByName('par_role'); for (var i = 0; i < optionArray.length; i++) {
2
2144
by: KC | last post by:
Hi, Every JavaScript executive context has a top-level window and we can use window.open() to open another window ... Does this related to Windows created by click on "File"->"New Window" or "File" -"New Tab" in browser's menu bar ? I think window created by "File"->"New Window" and "File"->"New Tab" are independent to each other from JavaScript's point of view. Is this correct ?
3
4209
by: Momomo | last post by:
Hi, I am having a problem with a control which is caused by a style on the page which I am not able to trace. If I use the control in a page without any styling it looks ok. Is there a way to remove/turnoff all CSS styling inside a div?
19
2217
by: maya | last post by:
hi, so what is "modern" javascript?? the same as "DOM-scripting"? i.e., editing content (or changing appearance of content) dynamically by massaging javascript objects, html elements, etc? (in conjunction with css, you know, the usual...;) this is what is meant by "modern" javascript?? so how do folks feel about this who think javascript is so evil they disable it from their browsers?? do sites designed with "modern" javascript...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.