Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

mcsee mcsee

🏠
Working from home
View GitHub Profile
View nestedExceptions.js
try {
transaction.commit();
} catch (e) {
logerror(e);
if (e instanceOf DBError){
try {
transaction.rollback();
} catch (e) {
doMoreLoggingRollbackFailed(e);
}
View normalFlow.js
try {
transaction.commit();
} catch (transactionError) {
this.withTransactionErrorDo(transationError, transaction);
}
//transaction error policy is not defined in this function
//so we don't have repeated code
//code is more readable
//It is up to the transaction and the error to decide what to do
View goodNames.js
var averageSalary;
averageSalary = calculateAverageSalary();
//..
var averageSalaryWithRaises;
averageSalaryWithRaises = calculateAverageSalary();
View insdictinct.js
var result;
result = getSomeResult();
var theResult;
theResult = getSomeResult();
View callbacks.js
var fs = require('fs');
var fileWithData = '/hello.world';
fs.readFile(fileWithData, 'utf8', function(err, txt) {
if (err) return console.log(err);
txt = txt + '\n' + 'Add Data!';
fs.writeFile(fileWithData, txt, function(err) {
if(err) return console.log(err);
console.log('Information added');
View modular.js
var fs = require('fs');
function logTextWasAdded(err) {
if(err) return console.log(err);
console.log('Information added');
};
function addData(error, actualText) {
if (error) return console.log(error);
View sequence.py
numbers = range(1, 100000)
#create a sequence of numbers and use them with a hotspot
#or
sequence = nextNumber()
View timestamp.py
# using time module
import time
# ts stores the time in seconds
ts1 = time.time()
ts2 = time.time() #might be the same!!
View specific.py
square = Square(5)
assert square.area() = 25
# Assertion should be precise
View tooGeneric.py
square = Square(5)
assert square.area() != 0
# This will lead to false negatives since it is too vague