How can I write clean and simple code many if and else












0














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









share|improve this question













migrated from stackoverflow.com 7 mins ago


This question came from our site for professional and enthusiast programmers.











  • 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
    1 hour ago










  • @TimBiegeleisen agree with you.
    – Common Man
    1 hour ago
















0














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









share|improve this question













migrated from stackoverflow.com 7 mins ago


This question came from our site for professional and enthusiast programmers.











  • 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
    1 hour ago










  • @TimBiegeleisen agree with you.
    – Common Man
    1 hour ago














0












0








0







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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago







Junburg











migrated from stackoverflow.com 7 mins ago


This question came from our site for professional and enthusiast programmers.






migrated from stackoverflow.com 7 mins ago


This question came from our site for professional and enthusiast programmers.










  • 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
    1 hour ago










  • @TimBiegeleisen agree with you.
    – Common Man
    1 hour ago














  • 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
    1 hour 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
1 hour 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
1 hour ago












@TimBiegeleisen agree with you.
– Common Man
1 hour ago




@TimBiegeleisen agree with you.
– Common Man
1 hour ago










4 Answers
4






active

oldest

votes


















1














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





share|improve this answer





























    1














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





    share|improve this answer





























      1














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





      share|improve this answer





























        1














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





        share|improve this answer





















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


          }
          });














          draft saved

          draft discarded


















          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









          1














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





          share|improve this answer


























            1














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





            share|improve this answer
























              1












              1








              1






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





              share|improve this answer












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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              Jason

              1113




              1113

























                  1














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





                  share|improve this answer


























                    1














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





                    share|improve this answer
























                      1












                      1








                      1






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





                      share|improve this answer












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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 1 hour ago









                      AJNeufeld

                      4,467318




                      4,467318























                          1














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





                          share|improve this answer


























                            1














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





                            share|improve this answer
























                              1












                              1








                              1






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





                              share|improve this answer












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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              Rajkumar Seenappa

                              112




                              112























                                  1














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





                                  share|improve this answer


























                                    1














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





                                    share|improve this answer
























                                      1












                                      1








                                      1






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





                                      share|improve this answer












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






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 1 hour ago







                                      ernest_k





































                                          draft saved

                                          draft discarded




















































                                          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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin