Skip to content

Instantly share code, notes, and snippets.

Patrick Michaelsen prmichaelsen

Block or report user

Report or block prmichaelsen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View snippet-2.js
function getParticipantNames() {
// Because queries are cached, using the `allParticipants` query
// prevents another roundtrip to the database
const participants = database.getAllParticipants();
return participants.map(p => p.name);
}
View snippet-1.js
function getParticipantNames() {
const participants = database.getAllParticipants();
return participants.map(p => p.name);
}
View self-documenting-code.md

The Myth of Self Documenting Code

Or, how I learned to stop worrying and love the comment.

This article is a response to Joe Eames' article on self-documenting code. If you haven't read it yet or aren't familiar with the concept of self-documenting code, please check out the article [here][3].

I consider self-documenting code to be a myth and perpetuating it to be dangerous (especially to new programmers).

Before we get into why, let me summarize the two primary arguments for sefl-documented code:

  1. Comments that explain what the program are doing are redundant. As code changes, comments can also become out-of-date and stale, making the code harder to maintain overall.
View stuff-you-should-know-snip-02.ts
let prev = null;
let reverse = null;
let it = head;
while (it !== null) {
prev = it;
it = it.next;
let temp = {
val: prev.val,
next: reverse,
};
View stuff-you-should-know-snip-01.java
// creating an array of ints without generics
int[] myArray = new int[] {};
// creating lists for ints and strings, respectively
IntList myIntList = new IntList();
StringList myStringList = new StringList();
// creating a list for each using generics
List<int> myGenericIntList = new ArrayList<int>();
List<string> myGenericStringList = new ArrayList<string>();
@prmichaelsen
prmichaelsen / deep-complete.ts
Created May 16, 2019
recursively mark all properties of a typescript type as defined
View deep-complete.ts
/** private type, not exported */
declare type NonObject = undefined | null | boolean | string | number | Function;
/**
* This type allows you to mark an object with
* optional properties as required.
*/
export declare type Complete<T> = {
[K in keyof T]-?: T[K];
}
/**
@prmichaelsen
prmichaelsen / stuff-you-should-know.md
Last active Aug 13, 2019
stuff you should _probably_ know before you walk into an interview for a big name tech company
View stuff-you-should-know.md

Stuff you should probably know before going for a code interview

This is a quick and dirty list of all the stuff you should know before you walk into an interview for a big name tech company. This will not cover any software topics, only computer science topics. This will help you in the coding portion of interviews, not in design portions.

ℹ - Use the language you are most familiar with for any of this content. Don't use a language you don't use everyday, or it will show. "...how do you truncate an array again?" Remember that a lot of interviewers expect you to code on a whiteboard. There will be no autocomplete or google to help you if you forget the name of a function.

At the very least, you should have written code to do these things at least once. Ideally, you could do each without a reference.

you should know:

@prmichaelsen
prmichaelsen / PrivateInvocationTestUtil.java
Created May 2, 2019
a snippet I wrote to test a private method before I made the philosophical decision to just not test private methods
View PrivateInvocationTestUtil.java
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class TestClass {
class PrivateInvocationException extends Exception {
public PrivateInvocationException(String message) { super(message); }
}
@SuppressWarnings("unchecked") // method with throw if type-cast is wrong, anyway.
private <TClass, TOut> TOut invokePrivateMethod(
Class testClass,
@prmichaelsen
prmichaelsen / mappedGenericObject.ts
Created Feb 11, 2019
neat trick for typing an object transform function
View mappedGenericObject.ts
/**
this lets you type a function that transforms a map
from a map with KV such that typeof V is some type A<T>
to a a map with KV such that typeof V is some type B<T>
In this case, we go from DataRequest<T> to ISubscription<T>
*/
class Definition<T> {}
interface DataRequest<T> {
def: Definition<T>;
View lotto.py
#############################################################
# lotto.py
#############################################################
# Description: Compare avg return of 2018-10-23
# mega million lottery compared to number of tickets bought.
# Note: this script doesn't appear to implement the correct
# behavior for doing so. I think my math is off. Oh well, it
# was fun trying.
#############################################################
You can’t perform that action at this time.