Jose Sandoval Google
 Resume     Book     Software     Drawings     Home     Subscribe to RSS feed Search Web Search

Java vs. AS3 coding styles
Friday, January 01, 2010

Coding styles evolve with the times and are as different as there are developers and programming languages. Where do coding styles come from?

We all have our ingrained way of writing and formatting code. The coding styles I've followed throughout these years come from my time in university while an undergrad student (University of Waterloo). Our programming assignments had specific requirements to follow (remember those pre/post statements for every function?). Depending on the programming class and project, skeletons of code were given to be filled with the real assignment. Because of this, I learned to write and comment code like my TAs and professors.

It was a vicious cycle: because everyone came from the same school, we all did the same things, for example, where the curly braces went, how many spaces between each line were required, where class members went. Coincidentally, the cycle continued into our co-op terms and first jobs around the Waterloo area: a large percentage of the senior developers in the software companies where we started our careers were also from Waterloo--and I believe that hasn't changed. We were a happy, uniformly trained coding family (mind you that this was a good thing).

I now have my own coding style and I notice when other developers do different things from what I do. Lately, I've been going through a lot of AS3 code and I have noticed, among other things, that curly braces are placed on their own lines. For example, a class definition may look as follows:
public class Button extends UIComponent
public function Button()
There's nothing wrong with this class definition or the syntax. However, most, if not all, Flash developers follow this style. Why? Where did it come from? Do they use it because every other Flash developer codes with this style and all the code samples they got a hold of when learning the language looked like this?

I think this is it. Code is cheap in the internet: someone will post a piece of code and someone else will use, borrow, and steal it (it's the way of the modern developer).

I've used a few languages in the past and I never liked this particular coding style--a whole line for a curly brace. Because most of the apps I've worked on are Java enterprise apps, I adhere, almost religiously, to Sun's Code Conventions for the Java Programming Language. I'm not a zealot, though I like to know that there are other programmers out there that adhere to same style standards I do and, therefore, I will know how to navigate their code when the time for maintenance comes--and that time will come.

By the way, the Button class above, should be written as:
public class Button extends UIComponent {
public function Button() {
If the code works, does it make a difference where the curly braces go? As with everything, it depends on how you look at it. I think my way is a more concise way of writing code. I can have a few more lines in front of me per screen page. I know many developers and they like to have a lot of code on the screen at one time as well. What's more, vertical real state keeps shrinking, depending on the display you use. I'm a laptop user (ThinkPad X200), and the form factor keeps getting wider but shorter. Who said that a larger horizontal form factor is better? Maybe it's the way we're using our computers--entertainment systems--and we need a wider aspect ratio for better movie quality.

So, in the world of AS3 development, I doubt I will convince these programmers to write more compact code a la C, C++, Java, C#. I've asked a couple of them why they code that way, and the answer I get sounds the same every time: "it's the right way of doing it." Further more, like me, they can only guess where their coding style comes from--school, books, code samples.

Most samples of AS3 and Flex code I have found on the net have this elongated, vertically wasteful style. And because of our need to borrow and steal code from other developers I think future AS3 and Flex code will continue to waste screen real state with those extra carriage returns.

I'll do my best to change this habit, and this I promise: every sample of AS3 or Flex code I'll ever publish will have the more compact coding style familiar to C, C++, Java, C# developers.

On a final note, please use curly braces for one-line statement inside for, while, if, or else conditionals. Yes to this:
    if (true) {
} else {
No to this:
    if (true)
And never to this:
    if (true) doSomethingAwesome(); else doSomethingAwful();
What about scripting language that don't require braces or semicolons? That's a whole different post.

Finally, yes, I know other C, C++, Java, C# developers use the elongated style. To them, I say, stop it.

8:52 PM | 5 comment(s) |


As a professional AS3 developer for over 5 years now, I just wanted to let you know that there's at least one developer out there that agrees with you 100%.

I have gotten into numerous heated debates with coworkers and other developers about where curly braces should go, and also whether curly braces should be placed around single-line if-statements. (Omitting curly braces leads to many problems down the road.)

If you need my help for starting a campaign to rid the world of extra newlines, let me know.
By OpenID clinch, at 10:14 AM

I must say I've moved from your preferred style to your hated style, and do the majority of my development in Java (work) or C# (hobby). I used to use "curly brackets on the same line", but when I started work about three years ago they used "brackets on a new line" and after a week or so it became so much more readable because the beginning and end were obviously marked. Why put the beginning bracket at the end of a line but the ending one at the start of a line? Brackets are unusual characters and so stand out more (for me) when trying to skim code blocks. That said, I'd never bother getting in to a heated debate about it - that's the way I like it, but if a project really needs it another way then so be it.
By Blogger IBBoard, at 11:23 AM

I've been developing in a number of languages now for 9 years. When I do Java, I do the more concise style and when I do as3 I do the elongated style. I change with the community, because after a while it just doesn't matter. It all blurs together much like learning multiple languages.
By Blogger Bailey, at 10:07 AM

It isn't "Java" style; it's "SUN" style. Nothing more. i.e just some companies preference.

Whole line for a curly brace ? its a single character, nothing more. I see people using SUN style and leaving a blank line after the opening curly brace on the previous line, so where's your "more concise" then.

Each project decides its own based on the people on it. That's it.
By Blogger andy, at 2:45 PM

I am C++ developer at Zaurmann Soft. No matter, what language we use, if it follows C-like syntax, we put the starting curly brace on the separate line. Of-course it is a matter of taste. This way does not save space, but finishing brace is under the matching starting brace.
Also I am a laptop user too, modern editors allow to close block clicking on the '-' sign before starting brace. It helps.
By Anonymous Alex, at 4:08 AM

This page is powered by Blogger. Isn't yours?

© Jose Sandoval 2004-2009