How can I write clean and simple code many if and else [on hold]
I just wrote this code. But I want to change this code simple and clean.
Do you have any feedback?
try {
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
if (!cnFile.exists()) {
cnFile.createNewFile();
} else {
cnFile.delete();
cnFile.createNewFile();
}
if (!enFile.exists()) {
enFile.createNewFile();
} else {
enFile.delete();
enFile.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
java
migration rejected from stackoverflow.com 11 hours ago
This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.
put on hold as off-topic by 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ 11 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ
If this question can be reworded to fit the rules in the help center, please edit the question.
comments disabled on deleted / locked posts / reviews |
I just wrote this code. But I want to change this code simple and clean.
Do you have any feedback?
try {
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
if (!cnFile.exists()) {
cnFile.createNewFile();
} else {
cnFile.delete();
cnFile.createNewFile();
}
if (!enFile.exists()) {
enFile.createNewFile();
} else {
enFile.delete();
enFile.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
java
migration rejected from stackoverflow.com 11 hours ago
This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.
put on hold as off-topic by 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ 11 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ
If this question can be reworded to fit the rules in the help center, please edit the question.
1
What is type ofvideoFile
,cnFile
,enFile
? If they are of same super type, you can create a method to accept superType, and then do if and else part.
– Mukesh prajapati
yesterday
1
As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles.
– BCdotWEB
18 hours ago
comments disabled on deleted / locked posts / reviews |
I just wrote this code. But I want to change this code simple and clean.
Do you have any feedback?
try {
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
if (!cnFile.exists()) {
cnFile.createNewFile();
} else {
cnFile.delete();
cnFile.createNewFile();
}
if (!enFile.exists()) {
enFile.createNewFile();
} else {
enFile.delete();
enFile.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
java
I just wrote this code. But I want to change this code simple and clean.
Do you have any feedback?
try {
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
if (!cnFile.exists()) {
cnFile.createNewFile();
} else {
cnFile.delete();
cnFile.createNewFile();
}
if (!enFile.exists()) {
enFile.createNewFile();
} else {
enFile.delete();
enFile.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
java
java
asked yesterday
Junburg
migration rejected from stackoverflow.com 11 hours ago
This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.
put on hold as off-topic by 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ 11 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ
If this question can be reworded to fit the rules in the help center, please edit the question.
migration rejected from stackoverflow.com 11 hours ago
This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.
put on hold as off-topic by 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ 11 hours ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – 200_success, Hosch250, BCdotWEB, Graipher, Sᴀᴍ Onᴇᴌᴀ
If this question can be reworded to fit the rules in the help center, please edit the question.
1
What is type ofvideoFile
,cnFile
,enFile
? If they are of same super type, you can create a method to accept superType, and then do if and else part.
– Mukesh prajapati
yesterday
1
As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles.
– BCdotWEB
18 hours ago
comments disabled on deleted / locked posts / reviews |
1
What is type ofvideoFile
,cnFile
,enFile
? If they are of same super type, you can create a method to accept superType, and then do if and else part.
– Mukesh prajapati
yesterday
1
As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles.
– BCdotWEB
18 hours ago
1
1
What is type of
videoFile
, cnFile
, enFile
? If they are of same super type, you can create a method to accept superType, and then do if and else part.– Mukesh prajapati
yesterday
What is type of
videoFile
, cnFile
, enFile
? If they are of same super type, you can create a method to accept superType, and then do if and else part.– Mukesh prajapati
yesterday
1
1
As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles.
– BCdotWEB
18 hours ago
As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles.
– BCdotWEB
18 hours ago
comments disabled on deleted / locked posts / reviews |
1 Answer
1
active
oldest
votes
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
As a general point of style, try to avoid "if not else" structures. An else
is implicitly an "if not". So a longer way to write your original is
if (!videoFile.exists()) {
videoFile.createNewFile();
}
if (!!videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
}
But there's no reason to write !!
, as that's essentially a no-op.
You can flip around the order and just write
if (videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
} else {
videoFile.createNewFile();
}
But as already noted, you don't need to say createNewFile
twice:
if (videoFile.exists()) {
videoFile.delete();
}
videoFile.createNewFile();
Now there's no need for an else
at all.
But you do this three times. So as already noted, better to create a function to handle the common functionality.
void createEmptyFile(File file) {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
You don't tell us what type these are. I'm calling it File
because that's the commonality in the names and the methods match.
And if later you want to switch to Files
, you can just change the method to
void createEmptyFile(File file) {
Files.deleteIfExists(file);
file.createNewFile();
}
You could also move the file or something else.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
As a general point of style, try to avoid "if not else" structures. An else
is implicitly an "if not". So a longer way to write your original is
if (!videoFile.exists()) {
videoFile.createNewFile();
}
if (!!videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
}
But there's no reason to write !!
, as that's essentially a no-op.
You can flip around the order and just write
if (videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
} else {
videoFile.createNewFile();
}
But as already noted, you don't need to say createNewFile
twice:
if (videoFile.exists()) {
videoFile.delete();
}
videoFile.createNewFile();
Now there's no need for an else
at all.
But you do this three times. So as already noted, better to create a function to handle the common functionality.
void createEmptyFile(File file) {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
You don't tell us what type these are. I'm calling it File
because that's the commonality in the names and the methods match.
And if later you want to switch to Files
, you can just change the method to
void createEmptyFile(File file) {
Files.deleteIfExists(file);
file.createNewFile();
}
You could also move the file or something else.
add a comment |
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
As a general point of style, try to avoid "if not else" structures. An else
is implicitly an "if not". So a longer way to write your original is
if (!videoFile.exists()) {
videoFile.createNewFile();
}
if (!!videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
}
But there's no reason to write !!
, as that's essentially a no-op.
You can flip around the order and just write
if (videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
} else {
videoFile.createNewFile();
}
But as already noted, you don't need to say createNewFile
twice:
if (videoFile.exists()) {
videoFile.delete();
}
videoFile.createNewFile();
Now there's no need for an else
at all.
But you do this three times. So as already noted, better to create a function to handle the common functionality.
void createEmptyFile(File file) {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
You don't tell us what type these are. I'm calling it File
because that's the commonality in the names and the methods match.
And if later you want to switch to Files
, you can just change the method to
void createEmptyFile(File file) {
Files.deleteIfExists(file);
file.createNewFile();
}
You could also move the file or something else.
add a comment |
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
As a general point of style, try to avoid "if not else" structures. An else
is implicitly an "if not". So a longer way to write your original is
if (!videoFile.exists()) {
videoFile.createNewFile();
}
if (!!videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
}
But there's no reason to write !!
, as that's essentially a no-op.
You can flip around the order and just write
if (videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
} else {
videoFile.createNewFile();
}
But as already noted, you don't need to say createNewFile
twice:
if (videoFile.exists()) {
videoFile.delete();
}
videoFile.createNewFile();
Now there's no need for an else
at all.
But you do this three times. So as already noted, better to create a function to handle the common functionality.
void createEmptyFile(File file) {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
You don't tell us what type these are. I'm calling it File
because that's the commonality in the names and the methods match.
And if later you want to switch to Files
, you can just change the method to
void createEmptyFile(File file) {
Files.deleteIfExists(file);
file.createNewFile();
}
You could also move the file or something else.
if (!videoFile.exists()) {
videoFile.createNewFile();
} else {
videoFile.delete();
videoFile.createNewFile();
}
As a general point of style, try to avoid "if not else" structures. An else
is implicitly an "if not". So a longer way to write your original is
if (!videoFile.exists()) {
videoFile.createNewFile();
}
if (!!videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
}
But there's no reason to write !!
, as that's essentially a no-op.
You can flip around the order and just write
if (videoFile.exists()) {
videoFile.delete();
videoFile.createNewFile();
} else {
videoFile.createNewFile();
}
But as already noted, you don't need to say createNewFile
twice:
if (videoFile.exists()) {
videoFile.delete();
}
videoFile.createNewFile();
Now there's no need for an else
at all.
But you do this three times. So as already noted, better to create a function to handle the common functionality.
void createEmptyFile(File file) {
if (file.exists()) {
file.delete();
}
file.createNewFile();
}
You don't tell us what type these are. I'm calling it File
because that's the commonality in the names and the methods match.
And if later you want to switch to Files
, you can just change the method to
void createEmptyFile(File file) {
Files.deleteIfExists(file);
file.createNewFile();
}
You could also move the file or something else.
answered 16 hours ago
mdfst13mdfst13
17.4k52156
17.4k52156
add a comment |
add a comment |
1
What is type of
videoFile
,cnFile
,enFile
? If they are of same super type, you can create a method to accept superType, and then do if and else part.– Mukesh prajapati
yesterday
1
As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles.
– BCdotWEB
18 hours ago