Java naming conventions versus English language correctness [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm not sure is this right place to ask so correct me if I'm wrong.
The case is that:
one category has one code
we want to get list of codes for multiple categories
Which version is correct and why?
getCategoriesCodes()
or
getCategoryCodes()
I see this problem from two points of view, one is the English grammar, and the second one is the clean code and code meaningful naming.
Please give me your opinion which method name is better and add note is English you native language.

As both a native English speaker and a Java programmer: getCategoryCodes() is preferable. getCategoriesCodes() implies to me that I am getting multiple codes for each category, or that the codes relate to the collection of categories, rather than a code for each category.
As an example from "real" English: a car has one driver; you would refer to the drivers of many cars as "car drivers", not "cars drivers".

I would side with the clean code and meaningful naming. Nobody will judge your English in your code and also looking at the code in a month or a year will make it easier to understand if its worded correctly

There is no harm in giving names grammatical correct provided it is readable and understandable and not become very long because long names are difficult to understand.

Related

JAVA get Plural/Singular for a given String/Word [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
trying to match a word with some hard coded values, let's say i have this word
'revenue' but 'revenues'
should also be a match.same way like this
'liability' > 'liabilities' .
what would be the approach we should take here, thanks in advance.
I have tried using my own algorithm but it is very difficult maintain word library and its respective plural or singular.
If you don't want to maintain full dictionary, then you might try to implement some general rules plus dictionary of exceptions from those rules.
But these are all quick and hacky solutions. Depending on how good must it be, different approaches would also be available like machine learning and maybe some language services available on clouds like AWS or Azure...
You might want to look at PorterStemmer of lucene. The idea is to compare the stems of both the words instead of comparing singulars and plurals. You can read more about it here.
Here is the maven dependency and below is an example:
PorterStemmer stemmer = new PorterStemmer();
stemmer.setCurrent("liability");
stemmer.stem();
System.out.println(stemmer.getCurrent());
stemmer.setCurrent("liabilities");
stemmer.stem();
System.out.println(stemmer.getCurrent());
The above returns same stems for both the words.

How should I name a Java method that needs a subchapter form? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I need to implement a set of rules in a validation class. Each rule needs to be checked in its own method (because of the ability to configure which rules should be checked). The problem is that the rule names come from an official entity(naming must be preserved) which defined them in the format:
Rule 1.1
Rule 1.2
Rule 1.3
...
Rule 2.29
Currently I am using a format like testRule1_1() , but it does not comply to the official Java naming conventions. How could this be renamed to comply?
Thank you in advance!
I believe that your goal should not be to slavishly follow naming conventions, but to remember their original purpose. The official Java naming conventions are designed to (1) make code more readable, and (2) prevent naming conflicts (e.g., between class and method names). If the conventions are counterproductive to achieving those purposes, it's correct to ignore them. They are only recommendations and emphatically not rules, and even in the standard API the naming conventions are sometimes (deliberately) ignored.
The method name testRule1_1() is unquestionably legal in the Java language, and is readable, and so it is not wrong to use it. Besides, I'm not convinced it is against convention. I don't recall ever seeing written guidance about how to separate numbers in a method name, but you would surely not be the first person to use an underscore.
Just as an opinion, I think testRule1_1() is fine, but I most prefer testRule1x1() as suggested by Joop Eggen in the comments above. Any other word inserted between the numbers as a spacer seems like an unsound sacrifice of brevity, but a simple x is short, and compared to an underscore it is quick to mentally read out.

Java: Is creating a "System" class a bad thing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.

JAVA: Create boolean variable or put argument within If statement? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've been running through a lot of code reviews and keep running into a situation where I see conflicting coding standards regarding boolean variables and if statements.
Here is an example of one method(1):
boolean isXTrue = getBooleanValue(DataSetX);
if (isXTrue) {
//do code
}
Here is an example of the other method(2):
if (getBooleanValue(DataSetX)) {
//do code
}
Both do the same thing and function just fine. In some cases, method 1 is a lot more readable since the boolean variable can be named something meaningful, while method 2 saves more lines and unnecessary boolean variable creations.
Maybe I am reading too deep into a simple coding standard, but I'm rather curious that if we use method 1 more often, we could have unnecessary booleans being made.
Sorry if this is a stupid question, but I wanted to get some opinions anyway :)
It's likely that the compiler will optimize both cases so that either way is identical at run time. Of course, that depends on code outside the context that you've provided.
As for the question at large: it's something that you and your coworkers or group need to come to a consensus about. If you're looking for a definite answer about which one to choose, I don't think you're going to get anything convincing other than personal preferences of readability vs line count.
Discuss this with the others that maintain your code base and decide on which should be preferred. Clearly explain why. Then move on to more...err...important issues.
As for my preference? I like option 1. To me, it's more readable, the variable name can be something descriptive like isActive, which makes the code easier to read. Also, inspecting values during debugging is probably easier as you have a definite variable with which to reference prior to its use later in the chain. Again, that's my preference.

PMD Issues in Code - Are they critical? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
We have a number of issues that PMD has identified for us - a lot of which seems nit picky. I have a list below - may I ask the hive mind - if you had to pick the top 5 crticial rules out of below list what would they be?
I am trying to balance effort with criticality of what would be bad if it made to production.
Thanks in advance!
AssignmentInOperand
AvoidDeeplyNestedIfStmts
AvoidDuplicateLiterals
AvoidInstantiatingObjectsInLoops
ClassWithOnlyPrivateConstructorsShouldBeFinal
CollapsibleIfStatements
CyclomaticComplexity
EmptyIfStmt
ExcessiveMethodLength
ExcessivePublicCount
ImmutableField
InefficientStringBuffering
InsufficientStringBufferDeclaration
LocalVariableCouldBeFinal
LooseCoupling
MethodArgumentCouldBeFinal
NcssMethodCount
NPathComplexity
PackageCase
PositionLiteralsFirstInComparisons
SignatureDeclareThrowsException
SingularField
TooManyFields
UncommentedEmptyConstructor
UncommentedEmptyMethod
UnusedImports
UnusedPrivateField
UseSingleton
Yikes! One could argue that all of these point to a code base that really needs refactoring to improve reusability and future maintainability, and none of them are "nitpicky". Keep in mind that static analysis is making recommendations, and that you're the best judge of what's good for your code, your customers, and your budget.
But if I had to pick a few to focus on first, I'd go after those that indicate deeper architectural problems: AvoidDeeplyNestedIfStmts, CyclomaticComplexity, LooseCoupling, TooManyFields, and ExcessiveMethodLength. Just my $0.02...
PMD or any other static code analyzer helps you to write better code. There are certain parameters/rules against which your code is analyzed. Anything critical will be catch by java compiler itself. These tools simply helps you to find few flaws in your code and format your code in a cleaner way.
The more of these you fix, the better and cleaner your code will be. If you think a rule is too "nitpicky", and you don't want it to be reported by PMD, then remove the rule from your rules.xml file. PMD also allows you to place comments in your code to ignore that one occurrance.

Categories

Resources