Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Best coding practices

    Cinema 4D SDK
    python
    2
    3
    588
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • mfersaouiM
      mfersaoui
      last edited by mfersaoui

      Hello,

      I'm cleaning my code and I have some of questions about the best practices to prevent any cinema 4d app crash.

      1 - Is it necessary to check if the object or tag it was created before inserting into the document?

      Example:

      obj = c4d.BaseTag(c4d.Onull)
      tag = c4d.BaseTag(c4d.Tdisplay)
      
      if not obj or not tag:
          return
          
      obj .InsertTag(tag)
      

      2- Is there a specific case to use the condition if value is None or if obj_or_tag is None instead of if not value or if not obj_or_tag

      I've always used the if not value method.

      Thanks

      1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand
        last edited by ferdinand

        Hi,

        no, it is not necessary in a strict sense of the word. The instantiation of a BaseList2D (i.e. a tag, an object, a shader, etc.) can fail when your computer runs out of memory. In that case the symbol of the node will reference None. You handling this case or not only makes a difference in with what error message your program will stop (this hold true for all exception handling). But it is easier to debug code if you get a message that specially gives you a reason why the program did stop, rather than your code failing because an object (in the pythonic sense) is not what the code assumes it to be (e.g. Python would raise something like AttributeError: NoneType has no attribute InsertTag when the node instantiation did fail).

        The practice to state something like if something is None: return makes sense if you design a function which specifically includes returning None in its signature. But it is often also used like shown here in your code, to irregularly stop a program when something is going wrong. While this usage is not specifically wrong, most style guides advise against it, because it makes it hard to understand why the program did stop and also that it did stop irregularly in the first place. Use exceptions and be verbose instead. So for your code this would be something like.

        if obj is None:
            raise MemoryError("Could not instantiate object.")
        

        Finally, if you say if not obj: or if obj is None: makes no real difference in this case (the literal None will evaluate as False and a node instance will evaluate as True). However, the first one is sometimes is considered to be bad style, because it lacks the verbosity and makes the code harder to read. It also might proof treacherous if you are not aware of the truth testing behaviour of the the Python object you are testing - for integer literals the value 0 evaluates as False for example.

        Cheers,
        zipit

        MAXON SDK Specialist
        developers.maxon.net

        mfersaouiM 1 Reply Last reply Reply Quote 1
        • mfersaouiM
          mfersaoui @ferdinand
          last edited by

          @zipit Thank you much.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post