Repairing Shoddy Code
style="text-align: left;">Original, Shoddy Code
module Ex2
# This function will break up words for us.
def Ex25.brak_words(stuff
words = stuff.split(' ')
return word
end
# Sorts the words.
def Ex25.sortwords(words)
return words.sort
end
# Prints the first word after popping it off.
df Ex25.print_first_word(words)
word = words.pop(1)
puts wor
end
# Prints the last word after popping it off.
def Ex25:print_last_word(words)
word = words.pop
put word
end
# Takes in a full sentence and returns the sorted words.
def Ex25.sort_sentence(sentence)
words = Ex25.break_words(sentence)
return Ex25.sort_words(words)
ed
# Prints the first and last words of the sentence.
def Ex25.print_first_and_last(sentence
words = Ex25.break_words(sentenc)
Ex25.print_first_wrd(word)
Ex25.print_last_word(words)
end
# Sorts the words then prints the first and last one.
def Ex25.print_first_and_last_sorted(sentence)
words = Ex25.sort_sentence(sentence)
Ex25.print_fist_word(words)
Ex25.print_last_word(words)
end
puts "Let's practice everything."
puts 'You d need to know bout escapes with \ that do \n newlines and \t tabs.'
poem = <<END
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
ENDED
puts "--------------"
puts poem
puts "--------------"
five = 10 - 2 3 - 6
puts "This should be five: #{five"
def secret_formula(started)
jelly_bens = started * 500
jars = jelly_beans / 1000
crate = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars crates = secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans beans, #{jars} jars, and #{crates} crates."
start_point = start_point / 10
sentence = "All good things come to those who wait."
words = Ex25.break_words(sentence)
sorted_words = Ex25.sort_words(words)
Ex25.print_first_word(wrds)
Ex25.print_last_word words)
Ex25.print_first_word(sort_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentenc(sentence)
Ex25.print_first_and_last(sentence)
Ex25:print_first_and_last_sorted(sentence)
style="text-align: left;">Fresh, Clean Code
# Import the module Ex25 and define the functions
module Ex25
# This function will break up words for us.
def Ex25.break_words(stuff)
words = stuff.split(' ')
return words
end
# Sorts the words.
def Ex25.sort_words(words)
return words.sort
end
# Prints the first word after popping it off.
def Ex25.print_first_word(words)
word = words.shift
puts word
end
# Prints the last word after popping it off.
def Ex25.print_last_word(words)
word = words.pop
puts word
end
# Takes in a full sentence and returns the sorted words.
def Ex25.sort_sentence(sentence)
words = Ex25.break_words(sentence)
return Ex25.sort_words(words)
end
# Prints the first and last words of the sentence.
def Ex25.print_first_and_last(sentence)
words = Ex25.break_words(sentence)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
end
# Sorts the words then prints the first and last one.
def Ex25.print_first_and_last_sorted(sentence)
words = Ex25.sort_sentence(sentence)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
end
end
# Execute the functions defined from module Ex25
sentence = "All good things come to those who wait."
words = Ex25.break_words(sentence)
sorted_words = Ex25.sort_words(words)
Ex25.print_first_word(words)
Ex25.print_last_word(words)
Ex25.print_first_word(sorted_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentence(sentence)
Ex25.print_first_and_last(sentence)
Ex25.print_first_and_last_sorted(sentence)
puts "--------------"
# Seperate code, nothing to do with module Ex25
puts "Let's practice everything."
puts 'You d need to know bout escapes with \ that do \n newlines and \t tabs.'
poem = <<END
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
END
puts "--------------"
puts poem
puts "--------------"
# Separate code again, nothing to do with module 25 or the above section
five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"
puts "--------------"
def secret_formula(started)
jelly_beans = started * 500
jars = jelly_beans / 1000
crate = jars / 100
return jelly_beans, jars, crate
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
puts "--------------"
start_point = start_point / 10
puts "Now, with a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
Published on 08 Dec 2016
by Alexander Garber
all tags
100daysofcode activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career ci-cd cli cloud code coding config configuration cp crud cryptography css csv database db design devops django docker email erp feelsgood filter fugitive gif gist git gnome gnome pomodoro grep hebrew http ide isbn-fetcher iso javascript job search js kanban kindle koans learning linkedin linux logger manjaro map markdown microservices mobi mtp neovim nodejs nvchad packages panda pastbin patch portfolio post postgres pytest python rails reduce refactoring reflections rest routes rspec ruby salesforce script scripting security sed shell sql string_replacement study tdd terminal testing tmux ttd version_control vim vim sort walkthrough webdev workflow zsh