Making things the hard way
#4
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....

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.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }


Messages In This Thread
Making things the hard way - by kyonides - 09-24-2018, 02:28 AM
RE: Making things the hard way - by DerVVulfman - 09-24-2018, 04:31 PM
RE: Making things the hard way - by MetalRenard - 09-25-2018, 12:17 AM
RE: Making things the hard way - by DerVVulfman - 09-25-2018, 03:24 AM
RE: Making things the hard way - by kyonides - 10-01-2018, 07:59 AM
RE: Making things the hard way - by DerVVulfman - 10-02-2018, 03:10 AM
RE: Making things the hard way - by kyonides - 10-02-2018, 01:24 PM
RE: Making things the hard way - by DerVVulfman - 10-02-2018, 11:59 PM
RE: Making things the hard way - by kyonides - 10-03-2018, 12:55 AM
RE: Making things the hard way - by DerVVulfman - 10-03-2018, 03:36 AM
RE: Making things the hard way - by kyonides - 12-10-2021, 08:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Making a "Wait Until Button Input" PK8 1 6,345 05-28-2012, 12:50 PM
Last Post: yamina-chan
   Making a Successful comedy RPG DerVVulfman 0 5,299 05-17-2011, 05:18 AM
Last Post: DerVVulfman
   The Ultimate Guide for Making Nifty Fantasy RPGs by Magus Masque DerVVulfman 5 11,459 06-07-2010, 02:44 PM
Last Post: sakhawat21
   Making an Impact, Part 3 Kaos Tenshi 0 3,880 01-19-2009, 05:33 PM
Last Post: Kaos Tenshi
   Making an Impact, Part 2 Kaos Tenshi 0 3,826 01-19-2009, 05:30 PM
Last Post: Kaos Tenshi
   Making An Impact, Part 1 Kaos Tenshi 0 3,906 01-19-2009, 05:26 PM
Last Post: Kaos Tenshi
   Rose Skye's Game Making Tips RoseSkye 0 3,714 12-06-2008, 05:06 PM
Last Post: RoseSkye



Users browsing this thread: 7 Guest(s)