News for hackers. Stuff that matters.
Re:John Carmack on Inlined Code by lencastre on Wednesday October 09, 2024 @03:34PM (#41791775)
I’m not even pretending I understood Carmack’s email/mailing list post but if more intelligent/experienced programmers than me care to help me out, what exactly is meant by this he wrote in 2007:

_If a function is called from multiple places, see if it is possible to arrange for the work to be done in a single place, perhaps with flags, and inline that._

Thanks,

Re:John Carmack on Inlined Code by tcoville on Wednesday October 09, 2024 @03:45PM (#41791866)
This is a heavily simplified version of what I'm suspecting he's trying to portray, key this wouldn't be useful for utility functions like string manipulation but more business logic being used across similar functions:

  def processOrder():
      # Some common processing logic
      print("Processing the order...")
  
  def placeOnlineOrder():
      processOrder()
      print("Sending confirmation email...")
  
  def placeInStoreOrder():
      processOrder()
      print("Printing receipt...")
  
  # Calls from different locations
  placeOnlineOrder()
  placeInStoreOrder()
Could become:

  def processOrder(order_type):
      # Common processing logic
      print("Processing the order...")
  
      if order_type == "online":
          print("Sending confirmation email...")
      elif order_type == "in_store":
          print("Printing receipt...")
  
  # Unified calls with different flags
  processOrder("online")
  processOrder("in_store")
Re:John Carmack on Inlined Code by fluoridation on Wednesday October 09, 2024 @04:33PM (#41792332)
That... looks decidedly worse. Now you have fewer functions that need to be concerned with multiple unrelated things for no reason.