This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$sourceFile = 'C:\temp\source.txt'; | |
$destination = 'C:\temp\destination :txt'; | |
// The filename is simplified | |
// and might come from a programatic construction | |
$copyWasSuccessful = copy($sourceFile, $destination); | |
if (!$copyWasSuccessful || !$file_exists($destination)) { | |
// Dont trust the function result. Handle the postcondtion error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pets = '😺🐶😺'; | |
const justDogs = pets.replaceAll('😺', '🐩'); | |
// Or | |
const justDogs = pets.replace(/😺/g, ''); | |
const catsArePresent = justDogs.includes('😺'); | |
// returns false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pets = '😺🐶😺'; | |
const justDogs = pets.replace('😺', '🐩'); | |
const catsArePresent = justDogs.includes('😺'); | |
// returns true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreditCard { | |
private String number; | |
private MonthOfYear expiryDate; | |
// expiryDate is the role | |
// MonthOfYear is the type | |
} | |
class MonthOfYear { | |
private Month month; | |
private Year year; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreditCard { | |
private String cardNumber; | |
private int expiryMonth; | |
private int expiryYear; | |
public CreditCard(String cardNumber, int expiryMonth, int expiryYear) { | |
this.cardNumber = cardNumber; | |
this.expiryMonth = expiryMonth; | |
this.expiryYear = expiryYear; | |
// No validations on number ranges? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date; | |
public class CreditCard { | |
private String cardNumber; | |
private Date expiryDate; | |
public CreditCard(String cardNumber, Date expiryDate) { | |
// Not a complete date | |
this.cardNumber = cardNumber; | |
this.expiryDate = expiryDate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate(mathOperand, firstArgument, secondArgument): | |
if mathOperand == '+': | |
return firstArgument + secondArgument | |
elif mathOperand == '-': | |
return firstArgument - secondArgument | |
elif mathOperand == '*': | |
return firstArgument * secondArgument | |
elif mathOperand == '/': | |
if secondArgument != 0: | |
return firstArgument / secondArgument |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate(mathOperand, firstArgument, secondArgument): | |
return eval(f'{firstArgument} {mathOperand} {secondArgument}') | |
# Sample usage to multiply two numbers | |
result = calculate('*', 4, 6) | |
# Injection to remove all files | |
calculate('', "__import__('os').system('rm -rf *')",'')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
VALID_COLUMNS = ['name', 'gender', 'email'] | |
def process_API_information(data): | |
invalid_columns = [ | |
column for column in data.keys() if column not in VALID_COLUMNS | |
] | |
if invalid_columns: | |
raise ValueError( | |
f"Invalid columns detected: {', '.join(invalid_columns)}" |
NewerOlder