Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method array input conventions

 
Saed Hussein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i saw that loops e.g for loop has array identifier convention like for example
so i would like to know array passing method convention suppose an array that accepts array of integers would be like this: so what is the convention to change , or sometimes
 
Liutauras Vilda
Bartender
Pie
Posts: 2792
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Saed.

Saed Hussein wrote:i saw that loops e.g for loop has array identifier convention like
Not sure you expressed yourself precisely as you wanted, but it is not exactly that. But you are right, that it is something to do with convention.

Usually i, j, k variables are used to control loop iteration. If 1 loop, then i is used, j and k are being used if loops being nested. i.e.:
Apart from that, quite often those variables as i or j or k being used as indices to access particular element of an array for instance. i.e.:

Saed Hussein wrote:so i would like to know array passing method convention suppose an array that accepts array of integers would be like this:
Not sure I understand this part correctly (sorry if not), but if you meant to make method to have parameter as an array whose elements are integers, then yes, you're right, this is how you'd do.
One thing probably to improve would be, not to use such variable name as a, better to use something meaningful, i.e.: books, cars, etc.. by omitting any single letters.

Please let know, if your questions has no been answered as you expected
 
Saed Hussein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Liutauras Vilda;
thank your reply just i was thinking if there is a convention to use array variables in method inputs.
your post was helpful.
 
Liutauras Vilda
Bartender
Pie
Posts: 2792
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saed Hussein wrote:i was thinking if there is a convention to use array variables in method inputs
No, there are no strict convention. As mentioned earlier, important part is, that variable would be meaningful, not "a", or "dr" or similar.

One more thing. Method inputs better call as: method arguments or method parameters <- these two are not the same. i.e.:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saed Hussein wrote:i was thinking if there is a convention to use array variables in method inputs.

Although there are (very old but still used) Code Convetions for the Java Programming Language, there is no specific convention for method parameters. Method parameters follow the same naming convention as variables. And it's just a convention so you are not required to follow these conventions (meaning: the compiler will not produce a compiler error).

For variables there is only one requirement: it must be a valid identifier. And for the OCA (and OCP) exam you have to know when an identifier is (in)valid. So let's test your knowledge a bit Which of the following methods won't compile? And why not?

Whenever you use a valid identifier for a method parameter (and/or variable), it's ok for the compiler. So it's perfectly valid to have a method likeBut it's a best practice (and a naming convention) to use short and meaningful names for variables (and methods and constants and ...). So despite a is a valid identifier, it's definitely not very meaningful and therefore a very poor variable name. For this method numbers is a much better choice. That's why one-character variable names should be avoided. One exception to this rule: temporary "throwaway" variables (like in for loops) can have one-character names (e.g. i, j, and k). If you have carefully read the row "Variables" of the naming convetions, you'll notice that this convention is part of the naming conventions

Hope it helps!
Kind regards,
Roel
 
Campbell Ritchie
Sheriff
Posts: 51419
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:. . . Although there are (very old but still used) Code Convetions for the Java Programming Language. . .
Hope it helps!
Kind regards,
Roel
I wish Oracle had maintained and updated those conventions.
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saed Hussein wrote:so i would like to know array passing method convention suppose an array that accepts array of integers would be like this:

Like the others, I'm not exactly sure what you're asking, but there's one thing I will say: There are two ways of defining arrays:
  int[] array   and
  int array[]  
My advice: DON'T use the second one. It's a throwback to C, and TBH I don't know why the Java authors ever decided to allow it.

Other than that:
  • Keep to conventions.
  • Make your variable and parameter names as descriptive as possible. So, in answer to your original question:
  • Don't call an array 'a'.

    Winston
     
    Campbell Ritchie
    Sheriff
    Posts: 51419
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:. . . DON'T use the second one. It's a throwback to C, and TBH I don't know why the Java authors ever decided to allow it. . . .
    Because they wanted to make a language looking like C++. It cuts both ways. Now we have crappy syntactic conventions whereby the following code will compile nicely and confuse all but the most experienced users:-On the other hand, the fact that C++ users thought the syntax looked familiar attracted them and contributed to the early popularity of Java®.

    Of course, Winston, you are right. The similarity of syntax to C++ syntax has confused many people new to Java® into believing that similar syntax denoted similar meanings. And I am sure that has done much more harm than the good in my “on the other hand”.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:There are two ways of defining arrays:
      int[] array   and
      int array[]  
    My advice: DON'T use the second one. It's a throwback to C, and TBH I don't know why the Java authors ever decided to allow it.

    I definitely agree with the given advice!

    I also like to add if we consider method declarations, there's a third way to define arrays using a varargs parameterAnd although the square brackets ([]) can be placed before or after the variable name, the ellipsis (...) must be placed before the parameter name. So this method declaration will not compileAnd you'll probably already know, but just for completeness: not every invocation of a method with a varargs parameter is allowed for a method with an array parameter, as illustrated in this code snippet

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic