How to organize this code? Memory handling vs readability

Hi there,

What is the best way to organize code?

  1. Organize by topic categories to be readable and follow logic:

    Add user texts: includes a loop for usertext asignment
    Add objects to group: includes another loop

# Add user texts:
rs.SetUserText(obj, "wallSurface", "Base", attach_to_geometry=False)
rs.SetUserText(obj, "SurfaceType", "Planar", attach_to_geometry=False)
for i in loft:
    rs.SetUserText(i, "wallSurface", "Edge_surface", attach_to_geometry=False)
# Add objects to group
group = rs.AddGroup()
rs.AddObjectsToGroup([obj,surfaceOffset[0]], group)
loft = rs.ExplodePolysurfaces (loft, True)
for i in loft:
    rs.AddObjectToGroup(i,group)
  1. Organize that efficient in terms of memory handling.

    Add user texts:
    Add objects to group: contains one loop (with user text asignment)

# Add user texts:
rs.SetUserText(obj, "wallSurface", "Base", attach_to_geometry=False)
rs.SetUserText(obj, "SurfaceType", "Planar", attach_to_geometry=False)
# Add objects to group
group = rs.AddGroup()
rs.AddObjectsToGroup([obj,surfaceOffset[0]], group)
loft = rs.ExplodePolysurfaces (loft, True)
for i in loft:
    rs.AddObjectToGroup(i,group)
    rs.SetUserText(i, "wallSurface", "Edge_surface", attach_to_geometry=False)

I’d go with 2), but with clarifying comments why you do something. And name your variables better. i doesn’t say much, and hiding loft with loft isn’t smart. What you really do is surfaces = rs.ExplodePolySurfaces(loft, True). Then your loop can look like for surface in surfaces:, and suddenly the code is much more readable, as the variable names are correct.

edit: a bit of a plug, I’ve been converting my scripts and personal tools to literate programming paradigm. I like being able to read my code weeks, months and years later and actually understand (and remember right away) what I wrote. A simple sample is

https://jesterking.github.io/rhipy/select_object_tweak_texture_mapping.html

Which I wrote as rhipy/select_object_tweak_texture_mapping.literate at master · jesterKing/rhipy · GitHub . The final source is created from this .literate program.

3 Likes