Dead code in Java/Android [closed] - java

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();

Related

Should I put if(variable < 10) in own method? [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
I have a check in a loop where I have to check if the number of occurrence is less than 10 which could be written as either
if(occ < 10){
}
or
if(checkIfOccurencyIsLessThan10(occ)){
values.add(current+"0"+occ);
}
else{
values.add(current+occ);
}
I'm reading Clean Code a handbook of agile software craftsmanship, where they say a method should do the least amount, and code should be hacked up into more pieces. Is this necessary right here? I'm trying to get a better grasp on how long a method should be, and how much it should be doing.
It depends on if this condition is spread across multiple pieces of code, and if this check could change in the future to include checking additional edge cases. If both of those things are true or could be true, then sure, extracting the check to its own function is wise. However, I would definitely say you should rename the function to not specify the functions implementation, because that defeats the purpose of being able to change out the conditional, right? Naming it something like occurenceNeedsZero is a much more flexible solution. Because if you come up with other use cases that need checking you can add them to this function as well!
However, if your question is "should I always make a simple conditional check such as "is x < 10" into its own function, then I would say no. That would be overengineering, in my opinion. Functions should be used to 1) separate logical portions of code, 2) increase readability, or 3) extract small pieces of code that are spread across multiple locations and likely to change in the future, as it simplifies future refactoring.
There are probably more cases than those 3, but those are the big ones.
It's better to use a static final variable to store this 10, instead hard code.
If there are other places need to check if occ < 10, you need extract it as a method. Otherwise it is unecessary.

Local variable is redundant [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am new to android, can anyone tell me what why is emailresult redundant?
From what I understand is that I retrieve textToUse from another method and name it email here, and then use email to undergo the matcher.find() with the result named emailresult. I then returned emailresult and after that returned the entire email.
I have mess around with it some time, like deleting emailresult and just use email. But then I will still have to create another variable to go under this location:
String emailresult = email.substring(matcher.start(), matcher.end());
It is redundant because you aren't doing anything with emailresult after assigning it a value besides returning it. You can simply do the following without the need to create a variable:
return email.substring(matcher.start(), matcher.end());
There is no need to create a variable
return email.substring(matcher.start(), matcher.end());

How does a return statement work in recursion? [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
When working with recursion I realized I'm not sure how the return statement works. Does it stop and return true when target.contains(key) returns true or does it fall out and return false, because of the line below? Does the previous iterations of the method get finished so that it instead return false?
The program creates passwords and this method is called to check that the password contains one of the required fields, such as upper case letters, symbols or numbers. It's called with 4 separate sources and they are then used to tell the program to keep the password or to create a new one if it doesn't meet the required standards. I've done this program for fun to refresh my memory of Java, it's not a real program that anyone will ever use.
private static boolean containsKeyword(String target, String source, int placement){
String key = String.valueOf(source.charAt(placement));
if(target.contains(key))
return true;
if(placement==0)
return false;
containsKeyword(target, source, placement-1);
return false;
}
You seem to be missing the whole point of the recursion step.
Change this:
someFunc(a, b, nbr-1);
return false;
To this:
return someFunc(a, b, nbr-1);
By the way, recursively calling this function with the exact same data (the strings a and b) is pointless.
There must be something else that you want to call this function with (perhaps sub-strings of a and b).
Your method will always return false if it doesn't get into the first if. You need to change this:
someFunc(a, b, nbr-1);
return false;
to
return someFunc(a, b, nbr-1);
Maybe if you update your question with what exactly you are trying to do, you will get a more targetted answer that will help you understand the recursion better.

Text not being read into variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Here is my code, I am supposed to read in data such as AAABBBCCCDDD and output a3b3c3d3.
I have updated the code, and now the code compiles and runs, however nothing is output. I dont know if its the way im reading data in or if the code is incorrect.
String text;
FileReader data = new FileReader("input.txt");
BufferedReader in = new BufferedReader(data);
text=in.readLine();
in.close();
//Counter looks at length of data
int counter=0;
//Counter2 looks at current letter or number to make see if its the same then iterates it
int counter2=0;
while (text.charAt(counter)<=text.length())
{
while (text.charAt(counter)==text.charAt(counter2+1))
{
counter2++;
}
System.out.println(text.charAt(counter) + counter2);
counter=counter2;
}
The reason the compiler is complaining is because in this loop:
while (in.readLine()!=null)
{
text = in.readLine();
}
it's possible that the body of the loop will never get executed, which means it's possible that text will never be set to anything. And the Java compiler doesn't like it when you use a variable that may not have been set to anything.
But the whole loop is wrong anyway. You're only using one input string, so why is this a loop? Before we can help fix this problem, we need to know what you're trying to accomplish. And if you really do want a loop, it would be wrong to call in.readLine() twice as you have above, since that means it will read two lines each time through the loop.
Assuming you've properly imported all the java.io classes, here are the two problems causing compilation to fail:
String text;
This must be initialized to something. Such as
String text = null;
Possibly setting it in the while loop is not good enough.
The other problem is this variable, output doesn't exist anywhere, which is why this line won't compile:
System.out.println(output);
I think you mean for it to be text

IF statement and few conditions in JAVA [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to check 9 conditions for true or false and they need to be all true in one time.
How can it be possible checked, besides
if (condition1 && condition2 && condition3 ... && condition9) {
...
}
Think I used word "condition" right.
It is hard to know if you don't show more code than that.....
However, if I only have that piece of code, I can recommend three things:
1. If you have this information, start with the condition that is most likely to be false, that may improve performance..... more information here short circuit
2. Another thing, if you have all the conditions in an array, you can loop trough the array... Something like this:
public boolean testCondition(boolean conditionsArray[]){
for(int i = 0; i < array.length; i++){
if(!conditionsArray[i])
return false;
}
return true;
}
Post more code and I will try to improve my answer.
3. You might want to rethink about the data structures also. Probably something is not very clean in your design if you need to test for 9 given conditions like that (just probably).
Hope it helps.
Whenever I get into such a situation in code, I know I'm doing it wrong, and need to rethink my object structure.
However, just at the pure level of the question, as long as the conditions are in a collection of some kind, this will work:
boolean allTrue = true;
for (boolean condition : conditions) {
allTrue &= condition;
}
And then do the if on the allTrue variable. Note that I am assuming that the collection is not empty, or if it is, that should be regarded as a true condition.
Most likely in the real world you have to wrap these conditions in some kind of object with a common interface with a method that returns the boolean and use that, rather than having a collection of pure booleans.

Categories

Resources