Skip to content

Instantly share code, notes, and snippets.

View JohnMGant's full-sized avatar

Michael Gant JohnMGant

View GitHub Profile
@JohnMGant
JohnMGant / Spaghetti.asp
Last active August 1, 2019 13:22
Example of classic ASP spaghetti code
<html>
<body>
<table>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft SQL Server"
conn.Open "Server=SQLPROD;Database=HR;UserID=SuperDuperAdmin;Password=ABC123!!!"
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT ID, Name, FavoriteColor " &
"FROM Employees " &
@JohnMGant
JohnMGant / ormexamples.cs
Created August 1, 2019 14:04
ORM examples
internal IEnumerable<Employee> GetEmployees1(string lastName) {
var query =
from emp in context.Employee
where emp.LastName.StartsWith(lastName)
orderby emp.LastName, emp.FirstName
select new Employee {
Id = emp.Id,
Name = emp.FirstName + ' ' + emp.LastName,
FavoriteColor = emp.FavoriteColor ?? "Green"
};
@JohnMGant
JohnMGant / ormexamples.sql
Last active August 1, 2019 20:12
SQL for ORM Examples
select Id, FirstName + ' ' + LastName as Name, coalesce(FavoriteColor, 'Green') as FavoriteColor
from Employees
where LastName like '[whatever you passed in]%'
order by LastName, FirstName
@JohnMGant
JohnMGant / ForLoopAdder.cs
Last active June 26, 2020 20:04
Adds an array of integers using a for loop
internal class ForLoopAdder : IIntegerAdder
{
public int Add(int[] values)
{
int sum = 0;
int count = values.Length;
for (int i = 0; i < count; i++)
{
int value = values[i];
sum += value;
@JohnMGant
JohnMGant / GotoAdder.cs
Last active December 16, 2020 22:38
Adds an array of integers using goto (don't do this!)
internal class GotoAdder : IIntegerAdder
{
public int Add(int[] values)
{
int sum = 0;
int count = values.Length;
int index = 0;
BEGIN:
if (index == count) goto RETURN;
int value = values[index++];
@JohnMGant
JohnMGant / ArrayPointerAdder.cs
Last active June 26, 2020 20:02
Adds an array of integers using a point (don't do this!)
internal unsafe class ArrayPointerAdder : IIntegerAdder
{
public int Add(int[] values)
{
int sum = 0;
int count = values.Length;
fixed (int *p = values)
{
for (int i = 0; i < count; i++)
{
internal class ForEachLoopAdder : IIntegerAdder
{
public int Add(int[] values)
{
int sum = 0;
foreach (var value in values)
{
sum += value;
}
return sum;
internal class EnumeratorAdder : IIntegerAdder
{
public int Add(int[] values)
{
IEnumerator<int> enumerator = values.AsEnumerable().GetEnumerator();
int sum = 0;
while (enumerator.MoveNext())
{
sum += enumerator.Current;
}
static void Main() {
foreach (var line in TellJokeAboutMice())
{
Console.WriteLine(line);
}
}
private static IEnumerable<string> TellJokeAboutMice()
{
yield return "MR MICE.";
@JohnMGant
JohnMGant / AddArrayForLoop.js
Last active August 7, 2020 17:35
Adding an array of numbers in JavaScript using for loop
function Add(values) {
let sum = 0
const length = values.length
for (var i = 0; i < length; i++) {
sum += values[i]
}
return sum
}