Organizing code

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5153

    by cookie.monster at 2012-09-28 10:53:28

    Hi all,

    Looking to tidy up my code. What do you use to tidy up?

    For example, could you enclose sections of code in $() to allow quick collapsing without creating any scoping issues?

    Your insight would be greatly appreciated!

    Regards,

    CM

    by poshoholic at 2012-09-28 12:12:45

    I use #region statements, with short descriptions of the business logic they contain. e.g.
    #region Get a list of computers.

    ...

    #endregion

    #region For each computer, check to see if it is responding to ping requests.

    ...

    #endregion

    #region If a computer is responding to ping requests, get the OS version and serial number from that computer.

    ...

    #endregion
    Regions are collapsible in modern editors and they make it very easy to read what a section of script is doing so that you can go into the right section when you need to test/change behaviour and so that you don't forget what something is used for. You can also nest regions inside of other regions, which is helpful as well for larger chunks.

    And don't forget to use the dry principle and encapsulate shared logic in functions.

    by cookie.monster at 2012-09-28 12:15:18

    Exactly what I was looking for, thanks for the tip!

You must be logged in to reply to this topic.