JAVA: Create boolean variable or put argument within If statement? [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 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.

Related

My team mate seems to be offended often when I review his code [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 4 years ago.
Improve this question
My team uses Scrum and we have code review (using Fisheye crucible), but from my observation working together for 2 years is that they care less about good code quality.
I am trying to learn code quality from reading Clean Code, and try to apply things I learn in code and by giving code review. I also had one sharing session on Clean code with my team and hoped that we all follow a good coding standard. Except me and another guy in the team, the rest are not interested in good coding practice nor the term Clean code at all.
I sense a great conflict during code review if I am the one who review my team mate's code. The is one guy that making the conflicts are so badly which I am getting tired of reviewing his code now, eventually I try to avoid a heated discussion that leads to nowhere.
Below are several examples in the my review for his code:
Asking him to create a variable with meaningful name for a magic number that was randomly put in code.
Asking to remove hard-coded values.
Asking to remove duplicate codes (for a particular business logic) in almost everywhere in one class, by asking him to create a method for those duplicate code instead (this time he commented in my code review that: "I could not do it because of this and that, why don't you just do it instead of asking me?" I told him I am a reviewer and not an author of the code and as per our standard the reviewer should not modify the code as she/he likes.)
He used static variables a lot, without any particular reasons. Even in unit tests, public static variables are declared many in the test classes. So I reviewed and put my comment asking to remove static variables instead; or if it is really needed then I suggest to have a #AfterClass method to destroy static variables when not being used (I said it's good for GC). He commented back, kind of: "Having these static variables won't cause any problems to GC for just running unit tests. Why he has to care for GC in unit tests?" and refused to change. My initial intention was that not only in unit tests, but also in production code too that we should not declare static variables as whatever we want.
His method is generally very long, contains of different logic putting in one method. Similar to one unit test method, generally just have two unit tests: one for failed case and one for successful case. So the failure conditions are all putting in one method and the rest are for successful case being put into one method. So when I reviewed, I asked for splitting each possible business logic for testing in one smaller method, and he refused to do so a few times.
My questions are:
I think he gets offended or I get him wrong?
Is there any of above examples I gave wrong comments?
Did I step on wrong line that I really offended him without me knowing?
When you have similar situation for 2 years without getting code review process improved, what would you do?
I tend to avoid reviewing his code and instead work on other stuffs on the board. Is that okay?
I do care for code quality as much as I can within my knowledge, it's a way to maintain a good product and also help myself to learn and improve technically. But getting into heated discussions and sometimes I feel annoyed for his comments are something I wish to avoid. But avoiding this situation for long is not a good solution I think.

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.

Difference between i++ and i-- [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was asked this question in an interview.Please provide the solution if you know about this thing.Is there any difference between
for(int i=0;i<=N;i++)
and
for(int i=N;i>=0;i--)
which runs faster and why?
PS: Please differentiate on the basis of performance and the way compiler takes it.I am not asking about the basic difference between postfix and prefix notation.
I think this has the potential to become a better question, if we had more info. You should specify what language you are referring to. If this is JavaScript or something else then you need to mention that.
Secondly, consider revising your question. I suggest asking under which situations one is more appropriate than the other.
Whatever the language, the difference between the two is a stark contrast. The first for-loop involves incrementing a variable until a maximum value is reached (metaphorically-speaking, kind of like a race to the top), while the second involves decrementing a variable until a certain minimum is reached (like a race to the bottom).
Incidentally, in some languages, such as PHP performance is faster in a loop increment expression if you write the increment or decrement operators on the left instead of on the right.
This is an excellent interview question because any answer you give is likely to be wrong and more importantly be something you never previously thought seriously about.
The whole point is to throw you off your game. They want to see how you react when you're pushed into an area that you feel like you should be expert in yet find something about which you are not. Knowing the perfect answer to this question doesn't help you because they'll have 12 more questions lined up to throw you off.
What they want is to see how you respond to this situation. Do you make stuff up? Do you think about it carefully? Can you justify why it's not an important concern? Do you insist your way of looking at it is the only valid way? Do you listen when told of another way? Are you a pain to deal with if told to do it another way?
They will care so much more about the answers to this than whether or not you can save them a CPU clock tick.
But if it turns out you are an expert in this one dusty arcane corner you might earn a point.
If that is your real question then it's a duplicate of this question: Which of these pieces of code is faster in Java?

Naming methods a, b, c, etc [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lots of times I see people naming their methods a(), b(), c(), etc. instead of giving them names that describe what the method actually does. What is the point of this?
My guess is that you were reading obfuscated code.
Here's a nice article.
They shouldn't be doing this. It's bad practice.
Either the developer is being lazy, or they are showing a very simple example(still bad practice).
EDIT: (Given the extra detail in your comments)
In that case this is probably done by some automated program. The code the developer is actually working on wouldn't be using these names.
To minimize the load time for .js files, many developers will "compress" them. Compressing also has an option to obfuscate the code, making it more difficult to steal and change by making it difficult to understand. This happens just as you describe, by changing the variable names and function names to "a", "b", "c", etc. This has the side-effect of making the code smaller, since the names are now shorter. An example of a web tool that does this for you is here: http://www.developerfusion.com/tools/compressjavascript/
The code base that you maintain is certainly NOT the one that has been compressed and/or obfuscated.
Nothing. They are just too lazy to follow the standard naming conventions maybe because they are simply doing some quick POC(proof of concept) and they don't want to waste their time in typing some meaningful method name.
But one must always follow the naming conventions in their actual projects where others might be looking/maintaining their code.
This has always been discouraged by every community into programming and probably is a sign of a bad developer.
For more information on why and how do we use coding conventions see this

View Java functions inlined [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
First off all, I know there are several questions about "Java inline". But they are all about how the compiler or JVM inlines function calls. I'm interested in doing this myself, or create some kind of a View for it. I want to define a function call of a class, and want to see everything inlined. Every method call should get inlined. I'm not sure how to handle instantiation of new objects, but it doesn't matter as much.
The goal is manual optimization, i.e. if a parameter is checked too often against null. Is there a tool to to something like this? I would prefer a GUI, but some kind of command line tool where I can specify a class function and it dumps some text somewhere will suffice, too.
EDIT:
For clearification:
Today I argued to use the NullObjectPattern, because some are defensively overchecking for nulls everywhere. This makes the code unreadable and unclean. I dont like it and wanted to have some kind of a tool, to show them how often they are actually checking the very same parameter again and again for null.
As was said: Don't guess, especially when you don't know what the JIT compiler will do after the code has been running for a while. You can waste infinite time infinitely improving something that accounts for 1% of runtime and only save 1%, or you can spend a short time getting a 10% improvement of something that accounts for 20% of your runtime and save 2%; the latter is by far a better choice.
The way you determine what's worth improving is by properly profiling your code after it has been fully warmed up.
And the way you get a significant improvement generally has more to do with improved algorithms than with microtuning of single instructions.

Categories

Resources