Skip to content

Instantly share code, notes, and snippets.

View mcsee's full-sized avatar
🏠
Working from home

mcsee mcsee

🏠
Working from home
View GitHub Profile
<?
$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
<?
$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
const pets = '😺🐶😺';
const justDogs = pets.replaceAll('😺', '🐩');
// Or
const justDogs = pets.replace(/😺/g, '');
const catsArePresent = justDogs.includes('😺');
// returns false
const pets = '😺🐶😺';
const justDogs = pets.replace('😺', '🐩');
const catsArePresent = justDogs.includes('😺');
// returns true
class CreditCard {
private String number;
private MonthOfYear expiryDate;
// expiryDate is the role
// MonthOfYear is the type
}
class MonthOfYear {
private Month month;
private Year year;
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?
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;
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
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 *')",''))
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)}"