How To Find the Stinky Parts of Your Code: Code Smell 248 – Unreliable Copy
source link: https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-code-smell-248-unreliable-copy
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How To Find the Stinky Parts of Your Code: Code Smell 248 – Unreliable Copy
Maximiliano Contieri
@mcsee
I’m senior software engineer specialized in declarative designs and S.O.L.I.D....
Too Long; Didn't Read
The article emphasizes the need to verify file copies to avoid silent modifications and adhere to software principles. It discusses problems like Least Surprise and Fail Fast Principle violations and offers solutions such as checking postconditions and using mature languages to ensure reliable file copy operations.
audio
element.Picture this: You copy a file and don't verify it
TL;DR: Don't rely on external solutions without good handlers
Problems
-
Silent Modifications
-
Least Surprise Principle violation
-
Fail Fast Principle Violation
Solutions
-
Ensure you meet your function's postconditions
-
Use mature languages
Context
The copy() function is used to copy files from one location to another.
However, when used on some systems, it can fail silently or make unexpected conversions.
For example, Windows interprets paths ending with a backslash () as directories.
If the intended destination file has the same name as a directory in the path, copy() will silently create an empty file with the intended filename within that directory.
This can be confusing and lead to data loss.
Sample Code
Wrong
<?
$sourceFile = 'C:\temp\source.txt';
$destination = 'C:\temp\destination.txt';
$copyWasSuccessful = copy($sourceFile, $destination); // true
$destinationFileExists = file_exists($destination); // true
$sourceFile = 'C:\temp\source.txt';
$destination = 'C:\temp\destination :txt';
// The filename is simplified
// and might come from a programmatic construction
$copyWasSuccessful = copy($sourceFile, $destination);
// true - this is a mistake
$destinationFileExists = file_exists($destination);
// false since it was not created
$destinationChangedFileExists = file_exists('C:\temp\destination ');
// true but unexpected
Right
<?
$sourceFile = 'C:\temp\source.txt';
$destination = 'C:\temp\destination :txt';
// The filename is simplified
// and might come from a programmatic construction
$copyWasSuccessful = copy($sourceFile, $destination);
if (!$copyWasSuccessful || !$file_exists($destination)) {
// Don't trust the function result. Handle the postcondition error
}
Detection
- [x]Semi-Automatic
You can check all copy() handlers and wrap them
- Fail Fast
Level
- [x]Beginner
AI Generation
Gemini is the only generator that avoided the problem dealing with ":" on file names
AI Detection
With this prompt:
What happens with this code on windows and what is the value of copyWasSuccessful
ChatGPT found the mistake and (wrongly) predicted the operation would fail.
Gemini, on the other hand, found the typo but also couldn't accurately predict the behavior of the operation.
Claude also noticed the mistake but refused to display the execution result
Conclusion
Always check important function's post-conditions even if you think you will have performance penalties. You can never be too safe.
Relation Code Smells
Code Smell 15 - Missed Preconditions
Disclaimer: Code Smells are my opinion.
Blaming programmers has been the prevailing approach for a half century of software development: It has not solved the problem yet, so it is time to look in different directions.
- Boris Beizer from Software Engineering Great works.
This article is part of the CodeSmell Series: How to Find the Stinky Parts of your Code
Recommend
-
9
@mcseeMaximiliano ContieriI’m senior software engineer specialized in declarative designs and S.O.L.I.D. and Agile lover.
-
2
@mcseeMaximiliano ContieriI’m senior software engineer specialized in declarative designs and S.O.L.I.D. and Agile lover.Yet more code smells? More?...
-
3
How to Find The Stinky Parts of Your Code [Part XIV]February 24th 2022 new story4More smell...
-
0
@mcseeMaximiliano ContieriI’m senior software engineer specialized in declarative designs and S.O.L.I.D. and Agile lover.
-
3
How to Find the Stinky Parts of Your Code [Part XVI]April 6th 2022 new story4Infinite co...
-
8
@mcseeMaximiliano ContieriI’m senior software engineer specialized in declarative designs and S.O.L.I.D. and Agile lover.
-
1
Code smells are a classic. It smells because there are likely many instances where it could be edited or improved. Most of these smells are just hints of something that might be wrong. They are not required fixed per se… (You shoul...
-
4
Code smells Are a Classic. It smells because there are likely many instances where it could be edited or improved. Most of these smells are just hints of something that might be wrong. Therefore, they are not required to be fixed p...
-
4
Your browser does not support theaudio element.Read by Dr. One (en-US)
-
5
@mcseeMaximiliano ContieriI’m senior software engineer specialized in declarative d...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK