All Things Scripting - BASH, Puppet-Code (Ruby), Learning Python atm...
- Useful Commands & Tips
- BASH scripting in TUI < Great resource
- My Video example <= My first TUI, based on the working regular shell script 'c4-bug-report.sh'
- BASH Scripting with Sourced files
Objective- Effective Code Writing and Execution
1. Evaluate conditions
2. Take actions
3. Carry out tasks, repeat some of them
4. Cleanup and close.
Desired End Results
1. Must be OS/Release Agnostic
2. Must be scalable and allow for growth
3. Must be adaptable- able to adjust with changed/better tools easily [variables]
4. Must be portable
5. Must have structure, order, and Comments for easy reading/debugging [Consistency]
Guidelines on how to write good code.
Writing good code is not as easy as it seems. Good code exposes a long list of qualities that is quite hard to put together. Writing good code is, to some extent, an art.
The DRY (Don't Repeat Yourself) principle, which states that you should never repeat the same piece of code more than once in your application.
There are several reasons why repeating the same piece of logic can be very bad, the most important ones being:
- There could be a bug in the logic, and therefore, you would have to correct it in every place that logic is applied.
- You may want to amend the way you carry out the validation, and again you would have to change it in every place it is applied.
- You may forget to fix/amend a piece of logic because you missed it when searching for all its occurrences. This would leave wrong/inconsistent behavior in your application.
- Your code would be longer than needed, for no good reason.
You are free to choose the number of spaces of indentation to use, but you then need to stick with it. (See 'Two extremely important pieces of advice:' below)
Two extremely important pieces of advice:
- Whatever IDE you will chose to use, try to learn it well so that you can exploit its strengths, but don't depend on it. Exercise yourself to work with VIM (or any other text editor) once in a while, learn to be able to do some work on any platform, with any set of tools.
- Whatever text editor/IDE you will use, when it comes to writing Python (any code), indentation is four spaces. Don't use tabs, don't mix them with spaces. Use four spaces, not two, not three, not five. Just use four. The whole world works like that, and you don't want to become an outcast because you were fond of the three-space layout.
Learning Python By Fabrizio Romano from Packt Publishing