Skip to content

Commit d64d134

Browse files
committed
Simplify passing parameters to application without conditional statements
1 parent 2b3f765 commit d64d134

3 files changed

Lines changed: 15 additions & 33 deletions

File tree

python/main.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@
99
unit = input('Provide your preferred unit d(daily - default), w(weekly) or m(monthly): ').strip()
1010
year = input('Provide the specific year you would like to create working report templates for(Default: the current year): ').strip()
1111

12-
if username and unit and year:
13-
app = Application(username = username, unit = unit, year = year)
14-
elif username:
15-
app = Application(username = username)
16-
elif unit:
17-
app = Application(unit = unit)
18-
elif year:
19-
app = Application(year = year)
20-
else:
21-
app = Application()
12+
params = dict()
13+
for key, value in { 'username': username, 'unit': unit, 'year': year }.items():
14+
if value:
15+
params[key] = value
2216

23-
app.run()
17+
Application(**params).run()
2418

2519
pycaches = glob.glob(os.path.join('.', '**', '__pycache__'), recursive = True)
2620
for pycache in pycaches:

ruby/main.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@
99
puts 'Provide the specific year you would like to create working report templates for(Default: the current year)'
1010
year = gets.chomp.strip
1111

12-
username = _username.empty? ? nil : _username
13-
unit = _unit.empty? ? nil : _unit
14-
year = _year.empty? ? nil : _year
12+
params = { username:, unit:, year: }.reject { |_, value| value.empty? }
1513

16-
if username && unit && year
17-
Application.run(username:, unit:, year:)
18-
elsif username
19-
Application.run(username:)
20-
elsif unit
21-
Application.run(unit:)
22-
elsif year
23-
Application.run(year:)
24-
else
25-
Application.run
26-
end
14+
Application.run(**params)

ruby/src/application.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ class DigitLengthError < StandardError; end
88
USERNAMES = ['hayat01sh1da'].freeze
99

1010
def self.run(username: 'hayat01sh1da', unit: 'd', year: Time.now.year.to_s)
11-
instance = new(username, unit, year)
12-
instance.validate_username!(username)
13-
instance.validate_unit!(unit)
14-
instance.validate_year!(year)
11+
instance = new(username:, unit:, year:)
12+
instance.validate_username!
13+
instance.validate_unit!
14+
instance.validate_year!
1515
instance.run
1616
end
1717

18-
def initialize(username, unit, year)
18+
def initialize(username: 'hayat01sh1da', unit: 'd', year: Time.now.year.to_s)
1919
@username = username
2020
@unit = unit
2121
@year = year
2222
end
2323

24-
def validate_username!(username)
24+
def validate_username!
2525
raise ValueError, "#{username} is NOT a permitted username." unless USERNAMES.include?(username)
2626
end
2727

28-
def validate_unit!(unit)
28+
def validate_unit!
2929
case unit
3030
when 'd', 'w', 'm'
3131
unit
@@ -34,7 +34,7 @@ def validate_unit!(unit)
3434
end
3535
end
3636

37-
def validate_year!(year)
37+
def validate_year!
3838
Integer(year)
3939
if year.length > 4
4040
raise DigitLengthError, 'Year must be 4 digits.'

0 commit comments

Comments
 (0)