Skip to content

Instantly share code, notes, and snippets.

@djleeds
djleeds / BadIdentifiers.java
Created January 7, 2013 19:35
Demonstrates suboptimal variable and method names. Copyright (c) 2012 Lampo Licensing, LLC and released under the MIT license
public Address processData(String city) {
Address ret = null;
List<Address> list = getAddresses();
for(Address obj : list) {
if(obj.getCity().equals(city)) {
if(ret == null) {
ret = obj;
} else {
@djleeds
djleeds / GoodIdentifiers.java
Created January 7, 2013 19:37
Demonstrates variable and method names that align better with the abstraction level of the code. Copyright (c) 2012 Lampo Licensing, LLC and released under the MIT license
public Address findAddress(String city) {
Address matchingAddress = null;
List<Address> addresses = getAddresses();
for(Address address : addresses) {
if(address.getCity().equals(city)) {
if(matchingAddress == null) {
matchingAddress = address;
} else {
@djleeds
djleeds / htb-html5-date-picker-1.html
Last active December 12, 2015 09:19
Converting an HTML5 Date Picker value to a JavaScript Date object might not produce the result you expect. These gists demonstrate how you can create the Date object at GMT or your local time zone.
<script>
$(document).ready(function() {
$("#execute").bind("click", function() {
var dateString = $("#date").val();
var date = new Date(dateString);
$("#dateObject").text(date.toString());
});
});
</script>
@djleeds
djleeds / gplus-prog-01-trycatch.java
Last active December 16, 2015 17:59
One way to get around a need for a try/catch block, when you can change the code you're calling into. Doesn't apply in all situations.
// If you call records.find() with an id that doesn't exist, you get a RecordNotFound Exception
int id = 1;
try {
records.find(id).setName("Joe");
} catch(RecordNotFoundException e) {
System.out.println("Record wasn't found!");
}
@djleeds
djleeds / htb-35cf-1a.cfc
Last active December 17, 2015 01:39
Demonstrates inheritance, composition, mixins, and patches in ColdFusion. The article that corresponds to these code snippets can be found here: http://www.hitthebits.com/2013/05/coldfusion-code-reuse.html
<!--- EggLayer.cfc --->
<cfcomponent>
<cffunction name="layEggs">
<cfargument name="count" />
<cfreturn "I just laid #count# eggs." />
</cffunction>
</cfcomponent>
@djleeds
djleeds / htb-whats-a-unit-1.java
Last active December 18, 2015 22:29
Code samples supporting the article _"Unit Testing: What's a Unit?"_ on the *Hit the Bits!* programming blog.
public int triple(int x) {
return x * 3;
}
@djleeds
djleeds / htb-css3-leaderboard-full.html
Last active February 24, 2022 21:21
Hit the Bits! - CSS3 Leaderboard - Full
<style type="text/css">
#leaderboard li {
font-family: sans-serif;
font-size: 12px;
line-height: 12px;
}
#leaderboard #players li {
display:block;
clear: both;
position: absolute;
@djleeds
djleeds / htb-css3-leaderboard-full-multiple.html
Created August 26, 2015 23:30
Hit the Bits! - CSS3 Leaderboard - With Multiple Lists
<html>
<head><title>Animating a List with CSS3 Transitions - Multiple Lists</title></head>
<body>
<!-- Demonstrates one way to animate multiple lists -->
<style type="text/css">
.leaderboard li {
font-family: sans-serif;
font-size: 12px;
@djleeds
djleeds / ExampleFlowWithPropertyDelegate.kt
Last active October 27, 2023 23:52
Simple example demonstrating how you might be able to use a delegated property with a flow in Kotlin, in response to a comment on YouTube here: https://www.youtube.com/watch?v=KFgb6l1PUJI&lc=Ugy1TJWi4MwXZOtTGOF4AaABAg. Warning - I'm not very experienced with Flow at this point, so there might be a better way to do this!
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty