How can I write clean and simple code many if and else
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
migrated from stackoverflow.com 36 mins ago
This question came from our site for professional and enthusiast programmers.
add a comment |
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
migrated from stackoverflow.com 36 mins ago
This question came from our site for professional and enthusiast programmers.
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
2 hours ago
@TimBiegeleisen agree with you.
– Common Man
1 hour ago
add a comment |
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 2 hours ago
Junburg
migrated from stackoverflow.com 36 mins ago
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com 36 mins ago
This question came from our site for professional and enthusiast programmers.
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
2 hours ago
@TimBiegeleisen agree with you.
– Common Man
1 hour ago
add a comment |
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
2 hours ago
@TimBiegeleisen agree with you.
– Common Man
1 hour 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
2 hours ago
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
2 hours ago
@TimBiegeleisen agree with you.
– Common Man
1 hour ago
@TimBiegeleisen agree with you.
– Common Man
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
Looks like you need to create a private method:
private void createFile(File file) { // if not File, then whatever type you need
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
}
Then call it multiple times:
try {
createFile(videoFile);
createFile(cnFile);
createFile(enFile);
} catch (IOException e) {
e.printStackTrace();
}
add a comment |
In both branches of the if/else, you create the file. Move it out of the if statement
if (videoFile.exists())
videoFile.delete();
videoFile.createNewFile();
if (cnFile.exists())
cnFile.delete();
cnFile.createNewFile();
if (enFile.exists())
enFile.delete();
enFile.createNewFile();
add a comment |
Since you are deleting the file if exists, You just delete the file always without checking if the file exists or not and create the new file.
videoFile.delete();
videoFile.createNewFile();
cnFile.delete();
cnFile.createNewFile();
enFile.delete();
enFile.createNewFile();
add a comment |
Your if
statement is not strictly needed as File.delete
does not throw an exception if the file doesn't exist, returning false
instead (and it seems you are not interested in that result).
And iterating over a file list can even simplify it further:
File files = new File{videoFile, cnFile, enFile};
for(File f: files) {
f.delete();
f.createNewFile();
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211012%2fhow-can-i-write-clean-and-simple-code-many-if-and-else%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looks like you need to create a private method:
private void createFile(File file) { // if not File, then whatever type you need
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
}
Then call it multiple times:
try {
createFile(videoFile);
createFile(cnFile);
createFile(enFile);
} catch (IOException e) {
e.printStackTrace();
}
add a comment |
Looks like you need to create a private method:
private void createFile(File file) { // if not File, then whatever type you need
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
}
Then call it multiple times:
try {
createFile(videoFile);
createFile(cnFile);
createFile(enFile);
} catch (IOException e) {
e.printStackTrace();
}
add a comment |
Looks like you need to create a private method:
private void createFile(File file) { // if not File, then whatever type you need
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
}
Then call it multiple times:
try {
createFile(videoFile);
createFile(cnFile);
createFile(enFile);
} catch (IOException e) {
e.printStackTrace();
}
Looks like you need to create a private method:
private void createFile(File file) { // if not File, then whatever type you need
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
}
Then call it multiple times:
try {
createFile(videoFile);
createFile(cnFile);
createFile(enFile);
} catch (IOException e) {
e.printStackTrace();
}
answered 2 hours ago
Jason
1113
1113
add a comment |
add a comment |
In both branches of the if/else, you create the file. Move it out of the if statement
if (videoFile.exists())
videoFile.delete();
videoFile.createNewFile();
if (cnFile.exists())
cnFile.delete();
cnFile.createNewFile();
if (enFile.exists())
enFile.delete();
enFile.createNewFile();
add a comment |
In both branches of the if/else, you create the file. Move it out of the if statement
if (videoFile.exists())
videoFile.delete();
videoFile.createNewFile();
if (cnFile.exists())
cnFile.delete();
cnFile.createNewFile();
if (enFile.exists())
enFile.delete();
enFile.createNewFile();
add a comment |
In both branches of the if/else, you create the file. Move it out of the if statement
if (videoFile.exists())
videoFile.delete();
videoFile.createNewFile();
if (cnFile.exists())
cnFile.delete();
cnFile.createNewFile();
if (enFile.exists())
enFile.delete();
enFile.createNewFile();
In both branches of the if/else, you create the file. Move it out of the if statement
if (videoFile.exists())
videoFile.delete();
videoFile.createNewFile();
if (cnFile.exists())
cnFile.delete();
cnFile.createNewFile();
if (enFile.exists())
enFile.delete();
enFile.createNewFile();
answered 2 hours ago
AJNeufeld
4,467318
4,467318
add a comment |
add a comment |
Since you are deleting the file if exists, You just delete the file always without checking if the file exists or not and create the new file.
videoFile.delete();
videoFile.createNewFile();
cnFile.delete();
cnFile.createNewFile();
enFile.delete();
enFile.createNewFile();
add a comment |
Since you are deleting the file if exists, You just delete the file always without checking if the file exists or not and create the new file.
videoFile.delete();
videoFile.createNewFile();
cnFile.delete();
cnFile.createNewFile();
enFile.delete();
enFile.createNewFile();
add a comment |
Since you are deleting the file if exists, You just delete the file always without checking if the file exists or not and create the new file.
videoFile.delete();
videoFile.createNewFile();
cnFile.delete();
cnFile.createNewFile();
enFile.delete();
enFile.createNewFile();
Since you are deleting the file if exists, You just delete the file always without checking if the file exists or not and create the new file.
videoFile.delete();
videoFile.createNewFile();
cnFile.delete();
cnFile.createNewFile();
enFile.delete();
enFile.createNewFile();
answered 1 hour ago
Rajkumar Seenappa
112
112
add a comment |
add a comment |
Your if
statement is not strictly needed as File.delete
does not throw an exception if the file doesn't exist, returning false
instead (and it seems you are not interested in that result).
And iterating over a file list can even simplify it further:
File files = new File{videoFile, cnFile, enFile};
for(File f: files) {
f.delete();
f.createNewFile();
}
add a comment |
Your if
statement is not strictly needed as File.delete
does not throw an exception if the file doesn't exist, returning false
instead (and it seems you are not interested in that result).
And iterating over a file list can even simplify it further:
File files = new File{videoFile, cnFile, enFile};
for(File f: files) {
f.delete();
f.createNewFile();
}
add a comment |
Your if
statement is not strictly needed as File.delete
does not throw an exception if the file doesn't exist, returning false
instead (and it seems you are not interested in that result).
And iterating over a file list can even simplify it further:
File files = new File{videoFile, cnFile, enFile};
for(File f: files) {
f.delete();
f.createNewFile();
}
Your if
statement is not strictly needed as File.delete
does not throw an exception if the file doesn't exist, returning false
instead (and it seems you are not interested in that result).
And iterating over a file list can even simplify it further:
File files = new File{videoFile, cnFile, enFile};
for(File f: files) {
f.delete();
f.createNewFile();
}
answered 1 hour ago
ernest_k
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211012%2fhow-can-i-write-clean-and-simple-code-many-if-and-else%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
2 hours ago
@TimBiegeleisen agree with you.
– Common Man
1 hour ago