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 6 years ago.
Improve this question
Is it a good practice to write in file with Formatter?
For example:
Formatter g = null;
try {
g = new Formatter("data.out");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int x = 3;
g.format("abc" + Integer.toString(x));
g.close();
I have been writing with Formatter all semester long, to find out that one of the common ways to write in files is with PrintWriter. Is there any possibility that some programs of my homework won't write what they were meant to write? Thanks!
I wouldn't say there's anything "wrong" with it per se. You simply gain c-style formatting options for your output, though you do lose some compatibility, since Formatter does not extend Writer.
Based on documentation, I wouldn't expect your output to be wrong for your homework. If you really wanted to be sure, though, Formatter does happen to have a constructor that accepts Appandable objects, and PrintWriter, by virtue of extending Writer, happens to implement that interface. This means you could construct a Formatter such that it uses a PrintWriter to do its dirty work, instead of the default BufferedWriter it would normally internally use to output to a file. However, this would be overkill, as while Print and BufferedWriter have internal differences with exception handling, exposed methods, and performance, the printed output would ultimately be identical since Formatter interacts with both the same way.
Related
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 1 year ago.
Improve this question
I have a few questions,
first - should naming of the method also follow BDD approach when Given, When, Then is used inside a method?
For example, Given_Preconditions_When_StateUnderTest_Then_ExpectedBehavior or It will be excessive and repeat itself and it's better to use something like methodNameUnderTest_givenCondition_expectedBehavior
as naming?
Second:
Do I need to write comments in tests marking Given, When, Then. Example:
#Test
void findById() {
// GIVEN
Visit visit = new Visit();
given(visitRepository.findById(1L)).willReturn(Optional.of(visit));
// WHEN
Visit foundVisit = service.findById(1L);
// THEN
assertThat(foundVisit).isNotNull();
then(visitRepository).should().findById(anyLong());
}
I personally like having the method name resemble what the method is testing in a bdd approach. So I don't think there are requirements, but it depends on your preference.
I always use a should_when principle, in naming my tests.
shouldFindVisit_whenIdExists
Short and simple, with given_when_then the name could quickly grow big, even though I don't think it is a big problem in tests!
For your second question, I think it depends on your preference again, but I think yes, it always helps seeing where I currently am in my test. And I can structure my test more easily in case I add something to it.
void shouldFindVisit_whenIdExists() {
// given
Visit should = ...;
// when
actual = service.findById(...);
// then
assertThat(actual, is(should));
}
I also always prefix my expected variables with a should, to mark them as what it should expect in the end. And the actual results from the service call in the when section as actual.
The verification in the end then is very clear and simple.
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 1 year ago.
Improve this question
I want to use both python and java in the same program. Since the print() function of python is better, but java's int variable; is more efficient.
If I'm interpreting correctly, you want to use to use both interchangeably in the same file, so you'd end up with code like:
def main():
int x = 5;
print(x)
This is impossible, because there would be ambiguity when trying to interpret code if you allowed constructs from both languages. For example, "X" + 1 is allowed in java, and would give you the string "X1". In python, it would give you an error because you can't add an int to a string. This would mean that there would be no way to know what your code should do because it's runnable in both languages.
This is a problem that all of us face, where we like some parts of some languages and other parts of other languages. The solution is pretty much just to decide what's most important, choose one language based on that, and then put up with the parts you don't like.
You can use Jython, which is a Python implementation based on the JVM/JDK. This allows calling between Java and Python code in both directions.
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 6 years ago.
Improve this question
I have a fairly dumb question. We all know that stream can have a number of intermediate operations but the real computation is done only when we call some terminal operation. Is it common to pass streams around without calling terminal operation for a long time?
Let me explain what I mean. Consider the following example with Iterator/Iterable
Read file with buffered reader in lines, return Iterator with
next() overrided to call reader.readLine()
In the upper class use guava's Iterators.transform to say lowercase
everything.
In the upper class wrap with another Iterator which may be split's
the line from upstream iterator by coma and returning tuples of
words in the line
In the final class consume the iterator by iterating over it and
writing to some OutputStream.
With all that I have completely lazy computation done from the start till the end. No intermediate collections are used, etc.
If I wanted to do the same with streams I guess I should pass around the Stream object itself. Is it very common to do so? Can you share some links with me?
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 6 years ago.
Improve this question
My question is general. When should I consider splitting a statement to multiple lines?
I'm writing code on my own, and never worked in a team. I always prefer to make my code as compact as it can get.
For instance, instead of writing:
depth = depth - randomNumbers.nextInt(depth) -1;
Expression expA = createRandomExp(depth);
Expression expB = createRandomExp(depth);
SubtractionExpression subExp = new SubtractionExpression(expA,expB);
return subExp;
I will just write:
return new SubtractionExpression(createRandomExp(depth - randomNumbers.nextInt(depth) - 1), createRandomExp(depth - randomNumbers.nextInt(depth) - 1));
The pros as I see it are:
Less lines of code.
No need for declaration of variables.
Cons:
Can be less readable
Some stuff are written multiple times, like: randomNumbers.nextInt(depth) -1
What are the standards in the industry? And what should I consider when writing statements? Some guidelines might help.
I came over this, but it doesn't really answer my question.
Multiple lines make easy to read when another developer needs to read your code, and use comment lines to clarify functions, variables, classes, etc. Also, You may leave the company you work and someone needs to improve your code or there may be a dysfunctionality in your code so in case of those reasons, you should write your code with multiple lines in order to makes it easier to read and understand.
There should not be any standarts to my knowledge but above reasons will be enough to write codes with multiple lines not in a one line.Additionaly, when you get compiler error while writing single line codes, compiler will say "error found in X line" and you may not be able to understand where is the error in that line. However, multiple lines will ease the situation. I suggest you to write multiple lines instead of single line.
It is good practice to not declare one-use variables. I really like aproach, when you use your "(" bracket as "{" bracket, like:
return new SubtractionExpression(
createRandomExp(
depth - randomNumbers.nextInt(depth) - 1
),
createRandomExp(
depth - randomNumbers.nextInt(depth) - 1
)
);
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
is there a way to tell the compiler in Java or Android to not remove some statements in code -which are intended to clean up variables after use to prevent any data remnant in ram-??
would creating a dummy method solve this issue??
these statements basically set the variables to their type-based initial values..
Thanks in advance!
The code that you describe is not dead code.
Dead code is code that will never execute.
Here is an example:
private int secretSchmarr;
public boolean blammo()
{
boolean returnValue;
secretSchmarr = calculateSecretValue();
returnValue = useSecretValue(secretSchmarr);
secretSchmarr = 99; // this is not dead code.
return returnValue;
secretSchmarr = 98; // This is dead code because it can never execute.
}
I answer under the odd assumption that you have a good reason to believe that the code is still useful even though it is dead.
Store the value false in some obfuscated form that the compiler can't understand. Then, conditionally branch to that code using your obfuscated value. The compiler will not know it is dead, so it will not be removed.
I'll use a file for my example, but it is probably not the most efficient way. Say your code that the compiler thinks is dead code was in a function called myCode(). Assume that fin is reading from a file that only contains false followed by EOF
if(Boolean.parseBoolean(fin.next()))
myCode();