Skip to content

Instantly share code, notes, and snippets.

View raineorshine's full-sized avatar

Raine Revere raineorshine

  • New York, NY
View GitHub Profile
@raineorshine
raineorshine / avoid-break.cpp
Created March 21, 2013 00:13
How to avoid 'break' in your while loop logic (Beginner)
// A
// [get some input from the user and store it in string str]
bool zFound = false;
while(str != "done")
{
if(str.startsWith("Z"))
{
cout << "Z encountered! Terminating!" << endl;
break;
}
@raineorshine
raineorshine / responsive.css
Last active November 19, 2022 06:08
Cheat Sheet: Responsive Design
@media all and (max-width: 1024px) {...}
@media all and (min-width: 800px) and (max-width: 1024px) {...}
@media not screen and (color) {...}
@media only screen and (orientation: portrait) {...}
@media all and (max-width: 420px) {
section, aside {
float: none;
width: auto;
}
@raineorshine
raineorshine / recursive-catch-prey.js
Last active December 16, 2015 18:39
Write a recursive function that determines how many seconds it takes for a cheetah to catch a gazelle.
/*
Cheetahs can run up to 60mph, or 88 feet per second. Gazelles can run up to 30mph, or 44 feet per
second. Write a recursive function that determines how many seconds it takes for a cheetah to catch
a gazelle given an initial starting point and how far away the gazelle is.
e.g. catchPrey(0, 1000) returns the number of seconds it takes for a cheetah at 0 feet to catch a
gazelle at 1000 feet.
To make things a little more interesting, the gazelle sees the cheetah at 500 feet and starts running
like hell.
@raineorshine
raineorshine / java-exception-handling-best-practices.md
Last active May 19, 2024 10:57
5 Great Resources for Exception Handling Best Practices in Java

General Tip

"The trick is to catch exceptions at the proper layer, where your program can either meaningfully recover from the exception and continue without causing further errors, or provide the user with specific information, including instructions on how to recover from the error. When it is not practical for a method to do either of these, simply let the exception go so it can be caught later on and handled at the appropriate level."

Resources

Advantages of Exceptions
Excellent example of separating error-handling code from program logic

Three Rules for Effective Exception Handling
Longer explanation and case study of exception use, including the basic principles of "throw early" and "catch late". Clear and thorough.

@raineorshine
raineorshine / JListDemo.java
Created June 12, 2013 18:06
Demonstrates the use of a JList component along with how to add/remove elements.
import javax.swing.*;
// The primary class is a JFrame which can be shown when the program starts
public class JListDemo extends JFrame {
/* main is just housed inside of the primary class. It could be part of a driver
class but this implementation is simpler. Here we just create a new
instance of the class to show the frame which contains the UI and all the
application logic. */
public static void main(String[] args) {
@raineorshine
raineorshine / if-practice1.js
Created July 28, 2013 18:31
Practice with if statements
/*
Exercise #1:
1. declare a variable and assign it the value "hello"
2. write an if-then statement that prints "good" to the console if the variable contains more than 3 characters, otherwise it prints "not enough"
3. reassign the variable to have the value "boo"
4. run the if-statement again
*/
var hello= "hello"
if (hello.length >3) {
console.log ("good");
@raineorshine
raineorshine / intro-to-arrays.js
Created July 28, 2013 18:33
Basic array operations.
/* ARRAYS */
var fruit = ["pineapple", "mango", "limón"]; // array literal (i.e. creating a new array by typing in the values directly)
var empty = []; // an array with no items
// accessing an array
console.log( fruit[0] ); // print out 'pineapple'
console.log( fruit[1] ); // print out 'mango'
var myfruit = fruit[1];
@raineorshine
raineorshine / array-less-than-10-bool.js
Created August 3, 2013 18:43
5 beginner examples of loops and arrays.
// #2: Find out if all the names in a list are less than 10 characters. (Store true or
// false in a variable to indicate the final result)
var names = ["John", "Dave", "James Fenimore Cooper", "Sarah", "Gene", "Alexandria"];
var result = true;
for(var i=0; i<names.length; i++) {
if(names[i].length >= 10) {
result = false;
break; // special statement that immediately ends the loop
}
@raineorshine
raineorshine / sublime-cheatsheet.js
Created October 6, 2013 15:46
My Favorite Sublime Text 2 Commands (Mac)
// Files & Commands
cmd + p // quick file switcher
cmd + shift + p // command palette
cmd + r // quick function search
cmd + shift + r // rename file
// Selection
cmd + option + g // select all instances of word under cursor
// Find & Replace
@raineorshine
raineorshine / practical-vim-commands
Last active December 24, 2015 20:39
Practical Vim Commands: The most basic commands that I use the most.Sublime Text 2 Vintage Mode: http://www.sublimetext.com/docs/2/vintage.html. Learn Vim: http://openvim.com
// Navigation
w // move to the end of a word
b // move to the beginning of a word
% // go to matching bracket
0 // go to beginning of line
$ // go to end of line
{ // move up by a block
} // move down by a block
gg // move to the top of the file
G // move to the bottom of the file