09-25-2018, 03:24 AM
Back in the earlier days of programming, coders developed ways to break up a program into smaller units. Back then, they were called 'functions' and 'subroutines'. The differences were that 'functions' returned some value with the data given, while subroutines merely performed a job. Within some languages such as Visual Basic, they had to be actively defined as a function or subroutine. But that is not the case with RUBY where we define them when written with the 'def' (as in define) statement.
Now that THAT is out of the way....
If you have a method (or function, subroutine, code-sammich, whatever), do you have everything in it within a single IF...then block of code? Why? Just look at this section from GAME_MAP. And pardon the indenting....
Other than the line with @need_refresh, everything else is in an IF... block. True, it works and works well. But less elegant and kind of sloppy. Instead of performing everything in the method if the condition is true, why not just exit out of the method if it isn't? Of course, the statement of @need_refresh = false must execute either way.
Though it reduces the method by merely one line, all the content now requires less indenting which could make the code easier to read.
Now that THAT is out of the way....
If you have a method (or function, subroutine, code-sammich, whatever), do you have everything in it within a single IF...then block of code? Why? Just look at this section from GAME_MAP. And pardon the indenting....
Code:
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
# If map ID is effective
if @map_id > 0
# Refresh all map events
for event in @events.values
event.refresh
end
# Refresh all common events
for common_event in @common_events.values
common_event.refresh
end
end
# Clear refresh request flag
@need_refresh = false
end
Other than the line with @need_refresh, everything else is in an IF... block. True, it works and works well. But less elegant and kind of sloppy. Instead of performing everything in the method if the condition is true, why not just exit out of the method if it isn't? Of course, the statement of @need_refresh = false must execute either way.
Code:
#--------------------------------------------------------------------------
# * Refresh
#--------------------------------------------------------------------------
def refresh
# Clear refresh request flag
@need_refresh = false
# If map ID is effective
return unless @map_id > 0
# Refresh all map events
for event in @events.values
event.refresh
end
# Refresh all common events
for common_event in @common_events.values
common_event.refresh
end
end
Though it reduces the method by merely one line, all the content now requires less indenting which could make the code easier to read.