This is such a common thing and I still don't get why people do it, like you clearly know .exists well. . . .exists so why shove it in its own function? Not calling out OP specifically but if all the shit things I've done related to code this one never even occurred to me but it apparently is like first thought for a lot of people
It’s not a bad practice. Like the other guy said it’s useful for testing.
It’s also useful in case you’d want to add any kind of extra rule. Perhaps you want to ignore a certain file extension. Or null check and sanitize the path argument. Or catch exceptions.
You shouldn’t wrap every single external function, but you probably should for ones that may throw a lot of errors.
I would also argue that a benefit of doing this can be to make a low level API call a bit more clear to what it's doing at a high level.
For example CanvasRendeingContext2d for an HTML Canvas has a function called arc which can draw a circle if you provide a start angle of 0 and an end angle of 2 radians (Math.PI * 2).
Besides testing, it can be useful for encapsulation. If you have a custom File class, you might want to expose a method for checking for a file exists without publicly exposing the File object itself.
Not sure what to call it, but it’s sometimes what happens when you learn a new concept. Oh, I learned about functions! I’ll make everything a function!
Personally I do this when I "stub out" functions. I write just the return value, arguments and name and leave the implementation until later. When I come back around to it, I might just keep it this way (although without the if statements, just a plain wrapper) just so I don't have to go back and refactor
Hey you never know when File.Exists will be insufficient. I mean what is existance? How do I know I even exist?? Then we'll need to implement our own version of FileExists that is compatible with the multiverse! And then where will you be?? You'll have to change all the references!!!
//this code is completely useless, it serves no purpose, but Its being used, I will
rewrite this later. (I wrote this a few days into C#, dont judge :) )
public static bool FileExists(string path)
{
//returns a bool depending on if a path exists.
if (!File.Exists(path))
{
return false;
}
else
{
return true;
}
}
public static bool DirExists(string path)
{
//returns depedning on if a directory exists.
if (!Directory.Exists(path))
{
return false;
}
else
{
return true;
}
}
oof I’m learning c++ but can’t you just streamwriter a = new streamwriter(“path”); If(!a){} I need to up load some one my earlier assignments but it seems to me we all wrote the most unnecessary shit starting out.
90
u/A_Guy_in_Orange Feb 03 '22
This is such a common thing and I still don't get why people do it, like you clearly know .exists well. . . .exists so why shove it in its own function? Not calling out OP specifically but if all the shit things I've done related to code this one never even occurred to me but it apparently is like first thought for a lot of people
9
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 03 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
19
u/Beautiful-Musk-Ox Feb 03 '22
"it's still being used so i'm going to leave it in" -- 2 references
1
u/Objective-Cat-9608 Nov 24 '22
oof I’m learning c++ but can’t you just streamwriter a = new streamwriter(“path”); If(!a){} I need to up load some one my earlier assignments but it seems to me we all wrote the most unnecessary shit starting out.
13
u/HeyItsMassacre Feb 03 '22
i don’t even know if this works i only know c++ but that’s the point of this subreddit
3
u/tcbenkhard Feb 04 '22
Brilliant
3
u/kristallnachte Feb 04 '22
Narrator: "...And later never came"
1
u/[deleted] Feb 03 '22
What was you learning to make and if possible can you send me a link of this??
1
u/ForwardBias Feb 03 '22
I will admit, the use of !File/Directory.Exists made me laugh out loud.
1
u/Curtmister25 Feb 03 '22
I do like your smiley face, however, be advised that you can use an EM🧐JI
1
u/KnockKnockP wicked Feb 04 '22
well thats one way to be abstract i guess
1
u/Fluxriflex Feb 11 '22
You could have refactored it in the time it took to write out that comment.
1
u/Brelee2222 Feb 21 '22
1
u/[deleted] Feb 04 '22
This is fine, you have been forgiven.
On phone, so excuse the formatting and syntax.
If(!File.Exists(path)){ //error handing }
Might have been better, but I am actually against writing logic in return line so I wouldn’t write,
Return File.Exists(path) and would write
bool found = File.Exists(path);
Return found;
If it’s in its own function.
-1
u/[deleted] Feb 04 '22
Since you mentioned this is for testing you could use System.IO.Abstractions instead