Coding style suggestions

I am currently in school for Cybersecurity and am taking Scripting Fundamentals. I came across this:

https://www.python.org/dev/peps/pep-0008/

Thought it might help someone.

Shawn

Comments

  • When I write Python I run it through a PEP-8 checker. This one seems to be useful:

    -- http://pep8online.com.

    I recently assisted a friend with some Python; she needed to create a tool for pulling and organizing data from firewall logs. We used this filter before handing it off to anyone else on her team -- wanted to ensure there were no silly mistakes.
  • JonnyMac wrote: »
    When I write Python I run it through a PEP-8 checker. This one seems to be useful:

    -- http://pep8online.com.

    I recently assisted a friend with some Python; she needed to create a tool for pulling and organizing data from firewall logs. We used this filter before handing it off to anyone else on her team -- wanted to ensure there were no silly mistakes.

    Oh. Cool thanks for posting Jon!
  • DavidZemonDavidZemon Posts: 2,907
    edited 2020-11-04 - 19:31:32
    In case you're not aware, code style is a religious war in this industry. Even whether or not you should follow a style is a war in and of itself! Personally, I believe any language-specific conventions trump personal preference (for instance, I'll follow PEP almost to the letter, despite preferring some things different) but my dad thinks PEP is dumb because he just wants to use the same style across every language.

    So... learn them. They're useful. Pick one. Or many. They're useful.

    I would say the most important thing is, if you're going to be strict about following one (or many) ruleset, let it be known. Clearly indicate "this code base follows PEP" in some fashion. And don't be surprised when someone else A) doesn't follow your rules, B ) doesn't know your rules exist, C) thinks your rules are dumb.
  • Wuerfel_21Wuerfel_21 Posts: 986
    edited 2020-11-04 - 19:28:07
    I don't really subscribe to any rigid style (but have utter disdain for auto-formatter carpet bombings...), but here's IMO the three magic principles:

    - Don't waste space and time! Waste as few lines as possible on syntax noise. Don't use overly long or annoying-to-type names for often used symbols.
    - Make it easy to read! This kinda goes with the above, but kinda beyond. Line stuff up! Leave lines blank to indicate destinct sections. If the code is very dense and you're using an editor without syntax features, put big seperators between larger blocks.
    - Add type information to symbol names (only) when neccessary. For example, some people always call their strings "something_s". In many languages, this is not really necessary (especially if your editor is language-aware), but in most assembly languages, it is incredibly helpful to not accidentally load a structure field with the wrong width (since that can create hard-to-find bugs).

    For a no-exhaustive example, in C
    // Bad - yes, some people would write it like this
    int myfunc(int x_i)
    {
            x_i /= 3;
            if (x_i > 0)
            {
                    return x_i/2;
            }
            else if (x_i < 0)
            {
                    return 50;
            }
            else
            {
                    return -1;
            }
    }
    
    // Good
    int myfunc(int x) {
      x /= 3;
           if (x > 0) return x/2;
      else if (x < 0) return 50;
      else            return -1;
    }
    

    Also, use spaces.
  • My pet peeve on syntax is explicitly calling out integers with variations of int. Isn't it obviously a number?

    Instead, what other useful info could be in the name? Is this an index? Is this a maximum limit? Is this in any particular unit of measure (mm, milliseconds, timer ticks, inches, psi, degrees of the 360 circle kind, degrees of the temperature kind? Etc.
  • Does everyone use the 4 spaces rule when writing spin code? Or Arduino code? Or is it particular to python?
  • I use 2 spaces with Spin, 4 with C (except inside the Arduino IDE which uses 2) and Python -- though I don't like that much space.
  • For what it is worth, I don't code as much sometimes as reuse parts of objects I like, so my log on the pyre would be 1) be consistent and 2) unless you are doing something really tricksy or obscure, a short line telling what the block should do makes it easier for me to follow and maybe use than n lines commented with the obvious. Just like languages, in order; 1) what you are paid to use, 2) what you like to use, 3) all others in that order.

    Now to run and don the asbestos UnderArmor..........
  • Shawn Lowe wrote: »
    Does everyone use the 4 spaces rule when writing spin code? Or Arduino code? Or is it particular to python?

    4-spaces is pretty common in Python because of PEP. Spaces vs tabs is its own holy war - one of the oldest. Whitespace-dependent languages like Python (as opposed to those that depend on curly braces or begin/end blocks) will often be better about consistent tabs vs spaces since it is so paramount to proper execution. So, Python settled on spaces fairly early on in order to make code sharing easier (imagine if you wrote with spaces and I wrote with tabs - it would be difficult to share code between us).

    The good news is, spaces is clearly better, as it earns you more money :smiley:
    https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/
  • Shawn Lowe wrote:
    Does everyone use the 4 spaces rule when writing spin code? Or Arduino code? Or is it particular to python?
    Oh heck no! Two for me, regardless of the language.

    -Phil
  • Wuerfel_21Wuerfel_21 Posts: 986
    edited 2020-11-05 - 20:29:04
    With Spin, you pretty much have to use two spaces, because that's what Propeller Tool will tabstop to - and it has a nasty habit of failing if you're nested in too deep. Sure, you can edit that in the configuration, but then you're pissing off everyone else.


    The more interesting Spin question: Indent top level code or not? I've seen and done both and don't really have a strong opinion.

    I.e.
    PUB foobar
    
      if condition
        do_stuff
    
    vs.
    PUB foobar
    
    if condition
      do_stuff
    
  • I always indent top-level code in Spin after a PUB or PRI.

    -Phil
  • Shawn Lowe wrote:
    Does everyone use the 4 spaces rule when writing spin code? Or Arduino code? Or is it particular to python?
    Oh heck no! Two for me, regardless of the language.

    -Phil

    So, I guess I'm coming from the idea of if it should probably get into a good habit. You're a much bigger contributor Phil (as well as I think you code for a living) so if it works for you of course do it. I want to start coding alot more, and think if I develop good habits right out the gate it would benefit me. But I truly appreciate the input form all!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 22,785
    edited 2020-11-06 - 04:39:07
    Shawn,

    The main thing is to develop a style that works for you and to use it consistently. You can observe the nattering style nazis for ideas and choose whichever ones seem reasonable. But in the end, readability and consistency are what matter most. And how to achieve that is up to you to decide.

    -Phil
  • Shawn,

    The main thing is to develop a style that works for you and to use it consistently. You can observe the nattering style nazis for ideas and choose whichever ones seem reasonable. But in the end, readability and consistency are what matter most. And how to achieve that is up to you to decide.

    -Phil

    Wise words. Thanks Phil!
Sign In or Register to comment.