How can I write clean and simple code many if and else [on hold]











-2














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













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 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
















-2














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













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 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














-2












-2








-2







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













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












share










share



share










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 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














  • 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








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










1 Answer
1






active

oldest

votes


















0















        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.





share



























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















            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.





    share


























      0















              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.





      share
























        0












        0








        0







                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.





        share













                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.






        share











        share


        share










        answered 16 hours ago









        mdfst13mdfst13

        17.4k52156




        17.4k52156















            Popular posts from this blog

            How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

            is 'sed' thread safe

            How to make a Squid Proxy server?