-4

I have a body of code that has to insure a file exists and if it does load it to the system to be read if it is not true it will create the file in the proper format and save it. I have to do this numerous times and thus i created a function. I was wondering if there is a way to take these numerous similar if statements and cut them down so that it doesn't utilize as large of an if statement set up

Sample code:

//Currently my code calls a function called CheckVal to check a file and a file where the data type exists.
//the call looks like this:
public Object CheckVal (File valPath, File fullPath)
{
//this will then check if it exist using .exists() 
if (valPath.exists)
{
//load Value
}
else
{
//Create value
}
return Value;
}

This data is called several times hence why I wish to cut down on the number of calls to these functions to reduce the overall time the program may take. I was hoping a solution may be possible as even the smallest time difference can greatly impact the speed of the program.

Edit: For some clarification all of this method above is currently called roughly 6 times per run of a for loop which can be run multiple times using all the files in a certain directory. As such the impact small differences in length of computation can create large differences in the greater outcome of the program. In regards to the file directory it is not itself called 6 times; instead it is used as an extension and is altered to place the file in different folders. Thus this code is not inherently repetitive as it does not call the same directory but different variants to ensure that they are all of a type (Object) for computation later.

  • What do you mean with *several values are true*? – Lino Oct 19 '17 at 12:54
  • it is just one if statement ;) – Veselin Davidov Oct 19 '17 at 12:54
  • Then how would you like to cut it down when there's only an `if`, `else if` and an `else`? Because any smaller than that can it not get – Lino Oct 19 '17 at 12:56
  • 3
    The `if-else` isn't going to impact the speed of your program, trust me. – Kayaman Oct 19 '17 at 12:57
  • @Lino to clarify my Function above is called up to 6 times per run of a for statement for a File. This process may be done hundreds of times on large files. Hence i was unsure of if there is a better way to condense this data for the sake of time. – Andrew Linington Oct 19 '17 at 13:01
  • if that's the case you should not always make the check if a file exists (only at the beginning of the for-loop or so) – Lino Oct 19 '17 at 13:04
  • @kayaman Thanks for the information. To be honest all i needed to know is if thee was a way to ensure it wouldn't impact run time by a huge margin. if it does as you say then thank you. – Andrew Linington Oct 19 '17 at 13:04
  • You don't need to check whether a file exists more than once, before you start processing it. If that function is being called *up to 6 times per run of a for statement*, you've written poor code. If your process *may be done hundreds of times on large files*, then a single test for the file's existence should be done beforehand. – Ken White Oct 19 '17 at 13:05
  • @lino In the sense that the data should be confirmed to exist it has to be done each time the for loop runs as my for loop is in fact the file name which is passed in each time for each of the files in a folder. Thus this code has to run each of those times to ensure the data is present and accessible as a variable of type object. – Andrew Linington Oct 19 '17 at 13:08
  • @KenWhite The code itself does not call the exact same file but instead 6 seperate directories under a similar file name such as "/This/Is/file.txt" and "/This/can/be/changed.txt" – Andrew Linington Oct 19 '17 at 13:10
  • @AndrewLinington You can **[edit]** the question to add those clarifications. Putting this information self-contained in the question is much better than having it spread through the comments. –  Oct 19 '17 at 13:34
  • @Hugo I will elaborate. – Andrew Linington Oct 19 '17 at 13:36

1 Answers1

1

Using a ternary operator could be nice but i don't know if it would really improve performances anyway you can try this:

return valPath.exists() ? loadvalue() : createvalue();

You need to create two methods that create or load the value and return it.

Francky Vincent
  • 453
  • 4
  • 12
  • As a matter of fact i already have a load and save method in my code. so this may be useful only if this is faster then a normal if statement. – Andrew Linington Oct 19 '17 at 13:12
  • 1
    @AndrewLinington Related: https://stackoverflow.com/questions/4706696/which-if-construct-is-faster-statement-or-ternary-operator –  Oct 19 '17 at 13:37
  • @Hugo based on what i see it seems the run time is quite similar and definitely helps. – Andrew Linington Oct 19 '17 at 13:45
  • @AndrewLinington Actually, I suggest you to first [measure the code's performance](https://stackoverflow.com/q/504103/7605325) and [identify the bottlenecks](https://softwareengineering.stackexchange.com/q/212111/265778) of your system, [**before** start optimizing](https://softwareengineering.stackexchange.com/a/80092/265778). –  Oct 19 '17 at 14:27