Skip to content

Instantly share code, notes, and snippets.

@ceddlyburge
Created February 17, 2022 12:46
Show Gist options
  • Save ceddlyburge/b0f66fb15237972aa81bc154a1590a9f to your computer and use it in GitHub Desktop.
Save ceddlyburge/b0f66fb15237972aa81bc154a1590a9f to your computer and use it in GitHub Desktop.
/*...*/ namespace BeerSong {
public class BeerSongGenerator {
public string Verses(int begin, int end) { /*...*/ return string.Join("\n", VersesList(begin, end)); }
IEnumerable<string> VersesList(int begin, int end) {
for (int verse = begin; verse >= end; verse--)
yield return Verse(verse); }
string Verse(int verse) => new VerseGenerator(verse).Verse; }
public class VerseGenerator {
readonly int verse;
public VerseGenerator(int verse) { /*...*/ this.verse = verse; }
public string Verse => (verse == 0) ? VerseZero : VerseOneOrLater;
string VerseZero =>
$"No more bottles of beer on the wall, no more bottles of beer.\n" +
$"Go to the store and buy some more, 99 bottles of beer on the wall.\n";
string VerseOneOrLater =>
$"{NumberOfBottles} of beer on the wall, {NumberOfBottles} of beer.\n" +
$"Take {oneOrIt} down and pass it around, {OneLessBottle} of beer on the wall.\n";
string NumberOfBottles => HowManyBottles(verse);
string HowManyBottles(int bottles) {
if (bottles == 0) return "no more bottles";
else if (bottles == 1) return "1 bottle";
else return $"{bottles} bottles"; }
string oneOrIt { get { /*...*/ return (verse == 1) ? "it" : "one"; } }
string OneLessBottle { get { /*...*/ return HowManyBottles(verse - 1); } } } }
@ceddlyburge
Copy link
Author

Part of We should format code on demand blog post

Currently our source code is saved to disk already formatted, and our editors display this saved format. There are many auto formatting tools, but the results always get saved back to disk. What happens if we save to a standardised text representation, and instead format code on demand, in the editor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment