Search By Label
function calculatePrice(total) { if (total > 1000) { return total * 0.9; // 0.9 is a magic number } else { return total * 0.95; // 0.95 is another magic number } }
0.9
and 0.95
are magic numbers because their meaning is not clear. If someone else reads this code in the future, it will be difficult to understand their significance without additional context.function getErrorMessage(code) {
switch (code) {
case "ERR001":
return "Connection Error"; // "ERR001" is a magic string
case "ERR002":
return "Authentication Error"; // "ERR002" is another magic string
default:
return "Unknown Error";
}
}
<%= button_to "Delete", [quote, line_item_date] %>
<%= button_to "Delete", quote_line_item_date_path(quote, line_item_date) %>
redirect_to @quote
redirect_to quote_path(@quote)
def
keyword and a function name.def double(x): """Doubles a number.""" return x * 2 result = double(5) print(result) # Output: 10
double
that takes a number (x
) as input and returns its double.# Lambda function to double a number double_lambda = lambda x: x * 2 result = double_lambda(5) print(result) # Output: 10
array = [1, 2, 3, 4] new_array = [] array.each do |element| new_array << element * 2 end new_array # => [2, 4, 6, 8]
each
handle and return the same array and for this reason, it is necessary to create a new_array
variable to inject the new values of the product to element * 2
map
:array = [1, 2, 3, 4] array.map do |element| element * 2 end # => [2, 4, 6, 8]
.strip()
method in Python is used to remove leading and trailing characters from a string. It's a versatile method with various applications for string manipulation.data = " Hello, world! " # String with leading and trailing spaces stripped_data = data.strip() print(stripped_data) # Output: "Hello, world!" (whitespace removed)
data = "**Hello, world!**" # String with leading and trailing asterisks stripped_data = data.strip("*") print(stripped_data) # Output: "Hello, world!" (asterisks removed)
breakpoint()
to any line.h: help w: where n: next s: step (steps into function) c: continue p: print l: list q: quit
# Ensure the value is one or more [value, 1].min # Ensure the value is 100 or less [value, 100].max
docker system prune
//example const double = (x) => x * 2; const square = (x) => x * x; var output1 = double(2); var output2 = square(output1); console.log(output2); var output_final = square(double(2)); console.log(output_final);
# Syntax lambda arguments: expression # Example of use add = lambda x, y: x + y # Defines a lambda function that adds two numbers result = add(5, 3) # Calls the lambda function with arguments 5 and 3 print(result) # Output: 8
# define a command class class AuthenticateUser prepend SimpleCommand # optional, initialize the command with some arguments def initialize(email, password) @email = email @password = password end # mandatory: define a #call method. its return value will be available # through #result def call if user = User.find_by(email: @email)&.authenticate(@password) ... end
AuthenticateUser.call(session_params[:email], session_params[:password])
def my_method(value:, second_value:) puts "value #{value}, second_value #{second_value}" end my_method(value: 'value one', second_value: 'value two')
module SimpleCommand def self.included(klass) klass.class_eval do def self.call(*args, **kwargs) puts "args #{args}" new(*args, **kwargs).call end end end end
module SimpleCommand def self.included(klass) klass.extend(ClassMethods) end module ClassMethods def call(*args, **kwargs) new(*args, **kwargs).call end end end
class NamedArguments include SimpleCommand def initialize(value: , second:) @value = value @second = second end def call puts "the method in myClass @value: #{@value}, @second #{@second}" end end
NamedArguments.call(value: 'the value', second: 'from second')
class SimpleArguments include SimpleCommand def initialize(valueOne, valueTwo) @valueOne = valueOne @valueTwo = valueTwo end def call puts "the method in myClass @value: #{@valueOne}, @second #{@valueTwo}" end end
SimpleArguments.call('arg one', 'arg two')
from functools import lru_cache
@lru_cache(maxsize= 128) #cache can save up to 128 entries and optimize execution
def fib_with_cache(n):
if n < 2:
return n
return fib_with_cache(n-1)+ fib_with_cache(n-2)