• Post Reply Bookmark Topic Watch Topic
  • New Topic

How far do conventions go?  RSS feed

 
Ewan McTiernan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone. First post here and I'm sure its one of many to come.

I have (i hope) a relatively simple question that will (I suspect) have a simple answer.

I'm a relative Java newbie (I've come from a background of C# and LISP) who has found himself responsible for an application that someone else built with a lot of knowledge and over a long period of time.

The issue I have at the moment is I'm seeing a lot of code that breaks what I thought was pretty standard Java conventions and I don't know if it implies something more significant or if it was just lax coding...

In some cases the classes have camel case names starting with lower-case and variables/methods have camel case starting with upper case. In most other cases they are the correct way around so I'm not sure if this is being done to infer special significance to these objects/methods or if it was just an oversight.

I'm just worried as I know sometimes an underscore or m can be used to differentiate class member variables from local variables - is this likely to be something like this?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ewan McTiernan wrote:I'm not sure if this is being done to infer special significance to these objects/methods or if it was just an oversight.

Just sloppy coding on someone's part.

I'm just worried as I know sometimes an underscore or m can be used to differentiate class member variables from local variables

Gross! I only see raw newbies doing something grody like that.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Ewan McTiernan wrote:
I'm just worried as I know sometimes an underscore or m can be used to differentiate class member variables from local variables

Gross! I only see raw newbies doing something grody like that.


Indeed. If that's actually necessary to make the code understandable, then the class is probably too big and/or too messy. Fix the underlying design problem and the naming issue magically goes away.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ewan McTiernan wrote:
The issue I have at the moment is I'm seeing a lot of code that breaks what I thought was pretty standard Java conventions and I don't know if it implies something more significant or if it was just lax coding...


The level of adherence to standards depends entirely on the shop and the individual coder. You can get used to anything though.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naming member variables with m_ is left over from hungarian notation. Its a really bad idea for modern languages.

The compiler won't care if you ignore conventions, and on some operating systems it won't even complain if you do really dumb things like have different capitalization of your java source files and the class name within the source. But just because the compiler doesn't complain doesn't mean its OK to do.

Any time you write code, you do so for two audiences. One is the compiler. The second is for the human who has to modify, understand, reuse, extend and improve your code. Some of us think that this second audience is actually much more important than the first one.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ewan, welcome to the Ranch!

I do projects at lots of different companies. Almost all Java code that I have come across is written using the de-facto style that Sun came up with a long time ago (with small deviations).

Sometimes for some reason people adopt some really different style. I can get used to that, but what I really hate is inconsistency. It's much better to have a strange, but consistent style than to have an inconsistent style.
Ewan McTiernan wrote:In some cases the classes have camel case names starting with lower-case and variables/methods have camel case starting with upper case. In most other cases they are the correct way around ...

That's exactly what I mean by an inconsistent style, and you already experienced how confusing it can be if the style is inconsistent.
 
Ewan McTiernan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Ewan, welcome to the Ranch!

I do projects at lots of different companies. Almost all Java code that I have come across is written using the de-facto style that Sun came up with a long time ago (with small deviations).

Sometimes for some reason people adopt some really different style. I can get used to that, but what I really hate is inconsistency. It's much better to have a strange, but consistent style than to have an inconsistent style.
Ewan McTiernan wrote:In some cases the classes have camel case names starting with lower-case and variables/methods have camel case starting with upper case. In most other cases they are the correct way around ...

That's exactly what I mean by an inconsistent style, and you already experienced how confusing it can be if the style is inconsistent.


Hahah, thankyou and everyone else for the help. As you touched on its largely consistent except for say, 2% of the classes and variables. As I've learnt it makes it hard when you're looking at a static method of a class as opposed to a public method of an instantiated object - I kept scrolling up thinking "where did that little fella come from?" and when a String variable named "ServerName" gets passed as a parameter I become wildly confused.

Now that I know its not all just some metaconcept with implied meaning that I didn't get I feel alot more relieved... relieved and FRUSTRATED
EDIT: Hahah Alot - Hyperbole and a half reference
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ewan McTiernan wrote:. . .
EDIT: Hahah Alot - Hyperbole and a half reference
I hope the joke about that spelling error still works
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:... and on some operating systems it won't even complain if you do really dumb things like have different capitalization of your java source files and the class name within the source.


Yup. I just got bit by that one... we had a web application which had been running for about 6 years on a Windows server, and then we moved it to an AIX server and all of a sudden "Index.class" didn't match "index.class".
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ewan McTiernan wrote:As I've learnt it makes it hard when you're looking at a static method of a class as opposed to a public method of an instantiated object - I kept scrolling up thinking "where did that little fella come from?" and when a String variable named "ServerName" gets passed as a parameter I become wildly confused.

Another tip: Using a good IDE such as Eclipse can make it a lot easier to navigate the code in a project. In Eclipse, you can put the cursor on a name and press F3 (or hold Ctrl and click the mouse) and it will take you to the place that the name (variable, method, class, ...) is defined. That makes it easier to find where things come from.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!