Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Coding preferences
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aziz




PostPosted: Fri Sep 02, 2005 11:42 am   Post subject: Coding preferences

This is more of a discussion, so if it needs to be moved please any mod do so Razz

But anywho, there are several coding preferences that differ but don't really make a difference. One I've been thinking about lately is this: Brackets {}

For example, two ways I've seen of coding is:

Java:
public class MyClass {
        public static void main(String[] args) {
                try {         
                        if (true) {
                                //blah
                        } else if (false) {
                                //fake blah
                        } else {
                                //default blah
                        }
                } catch (Exception e) {
                        //Oops
                } finally {
                        //Whatever
                }
        }
}


VS

Java:
public class MyClass
{
        public static void main(String[] args)
        {
                try
                {                     
                        if (true)
                        {
                                //blah
                        }
                        else if (false)
                        {
                                //fake blah
                        }
                        else
                        {
                                //default blah
                        }
                }
                catch (Exception e)
                {
                        //Oops
                }
                finally
                {
                        //Whatever
                }
        }
}


I know the code doesn't matter each way, but what do you guys do and why?

Another thing that's poked my curiosity is the array brackets

Do they go after the identifier, this this:

Java:
String[] names = { "Aziz", "Rizzix", "Dan" };


or to they go after the variable name, like this, and whats the difference?

Java:
String names[] = { "Aziz", "Rizzix", "Dan" };


(This always has annoyed me) is it main(String[] args) or main(String args[]) =\

I suppose it would be more effecient to do this: String[] names1, names2, names3; so it might answer my own dilemna. Anyways, I guessm discuss?
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Fri Sep 02, 2005 11:56 am   Post subject: (No subject)

array brackets "should" always go after the Type. like this:
Java:
String[] strarr = {"a", "b", "c"};
if you do it the other way... well let's just say no one's gonna consider you a java programmer... no really! you see.. java supported the other syntax so it would be easier for C programmers to adopt the language (back in those days). it continues to support it.. cuz not supporting it coud break legacy code.. anyways.. you are expected to use the syantax i mention above.. ALWAYS!

as for the braces.. well the standard java convention is to do it like this:
Java:
void something() {
    return;
}
yea we follow the standard C convention... it's usually the c++ programmers who do it the other way (where u open it in a new line)..

but then again.. no one's forcing you to do anything Wink they are just conventions..
wtd




PostPosted: Fri Sep 02, 2005 6:26 pm   Post subject: (No subject)

I find I prefer the style with braces on their own line. I like the extra space.
[Gandalf]




PostPosted: Fri Sep 02, 2005 7:21 pm   Post subject: (No subject)

I always put the braces on a seperate line. It makes it way more readable, and it makes me hate Java for having a weird convention. Actually, I see many C, C++, and Java programs that use the 'Java' way, but I really dislike it Smile.
[Gandalf]




PostPosted: Fri Sep 02, 2005 8:34 pm   Post subject: (No subject)

What do you think on conventions of naming variables/functions/etc.?

Generally, for myself...

Variables - variableName
Functions - Function_Name
Class' - ClassName
wtd




PostPosted: Fri Sep 02, 2005 8:37 pm   Post subject: (No subject)

[Gandalf] wrote:
What do you think on conventions of naming variables/functions/etc.?

Generally, for myself...

Variables - variableName
Functions - Function_Name
Class' - ClassName


For these the Java conventions are very good to follow.

variableNames
methodNames
CONSTANT_NAME
ClassName
Aziz




PostPosted: Fri Sep 02, 2005 10:22 pm   Post subject: (No subject)

Another thing, I've heard the expression "expression > statement" or so i think at least. Now, this means like this, right:

names = sort(names);

is more effiecient than

sort(names);
rizzix




PostPosted: Fri Sep 02, 2005 10:37 pm   Post subject: (No subject)

no not necesaritly efficient.. just the better way of going about with things.... as far as possible never ever modify the state of the objects passed as arguements to your methods or constructors.... it's just not the standard way of going about with things.. and it's not what would be considered.. expected behaviour... especially if sort is not a static method..
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Sep 02, 2005 10:38 pm   Post subject: (No subject)

The difference between expressions and statements is an important one.

An expression is a piece of code which takes one or more values and generates a new value.

Consider:

code:
int a = 5;


code:
a + 2


Is an expression which generates the value 7.

code:
a = a + 5;


Is a statement. It changes the value of the variable "a". Of course, this statement incorporates an expression.

Statements may contain expressions. Since expressions don't change existing values, they cannot include statements.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: