Skip to content

Instantly share code, notes, and snippets.

@coka
Last active September 3, 2024 13:44
Show Gist options
  • Select an option

  • Save coka/f0458394b3b64cbbf8ffe2d1cf4db546 to your computer and use it in GitHub Desktop.

Select an option

Save coka/f0458394b3b64cbbf8ffe2d1cf4db546 to your computer and use it in GitHub Desktop.
IF IF THEN IF ELSE IF reacc https://oblac.rs/if-if-then-if-else-if/
boolean hasHttp = false;
boolean hasHttps = false;
boolean hasHsts = false;
if ((hrs.getHasHttp() != null) && (hrs.getHasHttp())) {
hasHttp = true;
httpHosts++;
}
if (hrs.getHasHttps() != null) {
httpsHosts++;
if (hrs.getHasHttps()) {
hasHttps = true;
httpsHostsPresent++;
} else {
httpsHostsNone++;
}
} else {
httpsHostsUnknown++;
}
if (hasHttps) {
if (hasHttp) {
if (hrs.getHasHttpsRedirection() != null) {
if (hrs.getHasHttpsRedirection()) {
httpsRedirectionPresent++;
} else {
httpsRedirectionNone++;
}
} else {
httpsRedirectionUnknown++;
}
} else {
httpsRedirectionPresent++;
}
} else {
httpsRedirectionNotApplicable++;
}
if (httpsRedirectionPresent
+ httpsRedirectionNone
+ httpsRedirectionUnknown
+ httpsRedirectionNotApplicable
!= httpsHosts) // ERROR
/*
* Svakog leta zaboravim kako se koristi `javac`, pa sam odabrao da tvoj
* primer za početak prevedem u jezik najsličniji Javi. :^)
*/
// u sred nekog programa dešava se neka ✨
function wizardry(hrs) {
let hasHttp = false;
let hasHttps = false;
let hasHsts = false;
// ove dodatne definicije su nam potrebne za izvršavanje logike ispod
let httpHosts = 0;
let httpsHosts = 0;
let httpsHostsPresent = 0;
let httpsHostsNone = 0;
let httpsHostsUnknown = 0;
let httpsRedirectionPresent = 0;
let httpsRedirectionNone = 0;
let httpsRedirectionUnknown = 0;
let httpsRedirectionNotApplicable = 0;
if (hrs.getHasHttp() !== null && hrs.getHasHttp()) {
hasHttp = true;
httpHosts++;
}
if (hrs.getHasHttps() !== null) {
httpsHosts++;
if (hrs.getHasHttps()) {
hasHttps = true;
httpsHostsPresent++;
} else {
httpsHostsNone++;
}
} else {
httpsHostsUnknown++;
}
if (hasHttps) {
if (hasHttp) {
if (hrs.getHasHttpsRedirection() !== null) {
if (hrs.getHasHttpsRedirection()) {
httpsRedirectionPresent++;
} else {
httpsRedirectionNone++;
}
} else {
httpsRedirectionUnknown++;
}
} else {
httpsRedirectionPresent++;
}
} else {
httpsRedirectionNotApplicable++;
}
// radi jednostavnijeg testa, vratićemo ova dva broja
return {
redirections:
httpsRedirectionPresent +
httpsRedirectionNone +
httpsRedirectionUnknown +
httpsRedirectionNotApplicable,
hosts: httpsHosts,
};
}
// testovi u istom fajlu! 🙀
import { test } from "node:test";
import assert from "node:assert/strict";
test("oblac is happy", () => {
const result = wizardry({
getHasHttp: () => true,
getHasHttps: () => null,
getHasHttpsRedirection: () => true,
});
// zaista, u slučaju kada getHasHttps() vraća `null`, brojevi nisu
// jednaki, i test pada
assert.equal(result.redirections, result.hosts);
});
function wizardry(hrs) {
// neka ono što ne utiče na test bude malo dalje (čitljivost++?)
let httpsHostsPresent = 0;
let httpsHostsNone = 0;
let httpsHostsUnknown = 0;
let hasHttp = false;
let hasHttps = false;
// blog 🐛 !! hasHsts se ne koristi u primeru
let httpHosts = 0;
let httpsHosts = 0;
let httpsRedirectionPresent = 0;
let httpsRedirectionNone = 0;
let httpsRedirectionUnknown = 0;
let httpsRedirectionNotApplicable = 0;
if (hrs.getHasHttp() !== null && hrs.getHasHttp()) {
hasHttp = true;
httpHosts++;
}
if (hrs.getHasHttps() !== null) {
httpsHosts++;
if (hrs.getHasHttps()) {
hasHttps = true;
httpsHostsPresent++;
} else {
httpsHostsNone++;
}
} else {
httpsHostsUnknown++;
}
if (hasHttps) {
if (hasHttp) {
if (hrs.getHasHttpsRedirection() !== null) {
if (hrs.getHasHttpsRedirection()) {
httpsRedirectionPresent++;
} else {
httpsRedirectionNone++;
}
} else {
httpsRedirectionUnknown++;
}
} else {
httpsRedirectionPresent++;
}
} else {
httpsRedirectionNotApplicable++;
}
return {
redirections:
httpsRedirectionPresent +
httpsRedirectionNone +
httpsRedirectionUnknown +
httpsRedirectionNotApplicable,
// minimalna izmena koja popravlja test, intuirana na osnovu:
// (a) naziva promenljivih ("not applicable");
// (b) mogućnosti da je jedna grana u velikom granjanju™️ progutana;
// (c) pretpostavke da je broj host-ova zavistan HTTPS prisustva.
hosts: hasHttps ? httpsHosts : httpHosts,
};
}
import { test } from "node:test";
import assert from "node:assert/strict";
// pravimo yugotehnika property-based test da invarijanta važi uvek
function hrsFixture(http, https, redirection) {
return {
getHasHttp: () => http,
getHasHttps: () => https,
getHasHttpsRedirection: () => redirection,
};
}
test("oblac is happy (✅)", () => {
const result = wizardry(hrsFixture(true, null, true));
assert.equal(result.redirections, result.hosts);
});
// pada npr. za (false, false, true) |> da li je ovo validno u domenu?
test("the world is fine (❌)", () => {
const possibilities = [true, false, null];
for (const http of possibilities) {
for (const https of possibilities) {
for (const redirection of possibilities) {
const result = wizardry(hrsFixture(http, https, redirection));
assert.equal(
result.redirections,
result.hosts,
`Failed for combination: ${http}, ${https}, ${redirection}`,
);
}
}
}
});
function wizardry(hrs) {
let httpsHostsPresent = 0;
let httpsHostsNone = 0;
let httpsHostsUnknown = 0;
let httpHosts = 0;
let httpsHosts = 0;
let httpsRedirectionPresent = 0;
let httpsRedirectionNone = 0;
let httpsRedirectionUnknown = 0;
let httpsRedirectionNotApplicable = 0;
// malo manje mutacije, i očuvana semantička jednakost provera
const hasHttp = hrs.getHasHttp() === true;
const hasHttps = hrs.getHasHttps() === true;
// jasnija nijansa između između brojanja HTTP i HTTPS host-ova
if (hasHttp) httpHosts++;
if (hrs.getHasHttps() !== null) httpsHosts++;
// razdvajanjem vrsta brojanja, nije više toliki gužv
if (hrs.getHasHttps() === null) {
httpsHostsUnknown++;
} else if (hrs.getHasHttps()) {
httpsHostsPresent++;
} else {
httpsHostsNone++;
}
// skoro pa simetrično brojanje onom iznad; invarijanta sad uvek važi,
// što dodatno potkrepljuje hipotezu gutanja grane u odabranom IDE-u
if (hasHttp) {
if (hrs.getHasHttpsRedirection() === null) {
httpsRedirectionUnknown++;
} else if (hrs.getHasHttpsRedirection()) {
httpsRedirectionPresent++;
} else {
httpsRedirectionNone++;
}
} else if (hasHttps) {
httpsRedirectionNotApplicable++;
}
return {
redirections:
httpsRedirectionPresent +
httpsRedirectionNone +
httpsRedirectionUnknown +
httpsRedirectionNotApplicable,
hosts: hasHttps ? httpsHosts : httpHosts,
};
}
import { test } from "node:test";
import assert from "node:assert/strict";
function hrsFixture(http, https, redirection) {
return {
getHasHttp: () => http,
getHasHttps: () => https,
getHasHttpsRedirection: () => redirection,
};
}
test("oblac is happy (✅)", () => {
const result = wizardry(hrsFixture(true, null, true));
assert.equal(result.redirections, result.hosts);
});
test("the world is fine (✅)", () => {
const possibilities = [true, false, null];
for (const http of possibilities) {
for (const https of possibilities) {
for (const redirection of possibilities) {
const result = wizardry(hrsFixture(http, https, redirection));
assert.equal(
result.redirections,
result.hosts,
`Failed for combination: ${http}, ${https}, ${redirection}`,
);
}
}
}
});
/*
* Naziv ovog fajla je naravno referenca na strukturirano programiranje
* za koje sam sada još sigurniji da je generacijski pogrešno shvaćeno
* na našim prostorima.
*
* "Structured Programming"
* by O.-J. *D*ahl,
* E. W. *D*ijkstra, (eve ga opet!)
* C. A. R. *H*oare
*
* https://www.youtube.com/watch?v=SFv8Wm2HdNM (strikes again)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment