Coding style suggestions
Shawn Lowe
Posts: 635
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
https://www.python.org/dev/peps/pep-0008/
Thought it might help someone.
Shawn
Comments
-- 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!
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.
- 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
Also, use spaces.
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.
Now to run and don the asbestos UnderArmor..........
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
https://stackoverflow.blog/2017/06/15/developers-use-spaces-make-money-use-tabs/
-Phil
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. vs.
-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!
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!