myth or fact about functions in bash [closed] - bash

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I understand that functions in bash are like goto's in c, and this make the script a tiny bit slower. Is this true? Should I avoid or embrace functions?
function foo(){
bla..
}

If you're using a bash script and worrying about the speed decrease a function call imposes, you're doing things wrong. Use a compiled language (like c)...
You should embrace functions for the following reasons:
Reusability. You can call a function many times in your script. It's easier to call a function than it is to copy-and-paste the 10 lines to perform a task. It's easier to edit the task if it's located in one place, too.
abstraction. When you've got a function, you can re-use it without caring how it works. (The function becomes a "black box").
I also find functions much easier to read - the function name should tell me what the code in the function does, and I can gain a high-level view of the code by looking at the functions, not the specific steps to complete that function.

Related

How to improve coding skills? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I can't say I'm weak in programming but I can't come up with the logic faster. I can analyse others code and get to know the logic. But I can't do it on my own. How can I improve my programming skills?
Quite a broad question but from my own, 30yr experience I can tell you there is no way around starting to
analyze existing code,
modify some things (play with it until it feels like it's your own code)
see what the changes do
develop your own ideas on how to do things faster/better/more beautiful
implement your ideas
see if it works
go on to more complex tasks
read books (very important, because many things can't just be discovered by trial'n'error)
be very passionate and determined about what you want to become reality
if you want to learn faster, then write more code
One very important item. You should have fun with what you do is always the best guarantee for success
If you fail at these items then I'm afraid you will never succeed with programming. But then maybe it's like any other field of knowledge.
I experience the same difficulties during my learning journey, too. When I complete challenge tasks I create more complex tasks for myself to see what I can do. It takes me to the next levels of solving problems.
Practice, Practice, Practice!

implementation of recursion and loops at different levels [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've read posts where people say certain compilers will implement recursion as loops but the hardware implements loops as recursion and vice versa. If I have a recursive function and an iterative function in my program, can someone please explain how the compiler and hardware are going to interpret each one? Please also address any performance benefits of one over the other if the choice of implementation does not clearly favor one method like using recursion for mergesort.
Ok, here is a brief answer:
1)A compiler can optimize tail recursive calls. But it is usually not a loop, but rather a stack frame reuse. However, I have never heard of any compiler that converts a loop into recursion(and I do not see any point of doing so: it would use additional stack space, likely to work slower and can lead to the change of semantics(stackoverflow instead of an infinite loop)).
2)I would say that it is not correct to speak about hardware implementing loops, because hardware itself does not implement loops. It has instructions(like conditional jumps, arithmetical operations and so on) which are used to implement loops.

Style of writing conditional operators [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is considered more appropriate style of writing conditional operators?
if(1){
puts("Hello")
}
or
if(1) puts("Hello")
Similar aspects of coding style are welcome too.
That's all depends on your preference, that's why we rarely see people code in the same style.. Moreover, it depends on which programming language you're using.. IMHO, the important thing in coding is code readability and comments, so when your BOSS asks other people to help or develop your code. He /she will spend the least amount of their time to understand your code..
If you ask specifically from your example above, I would prefer the first one.. Because in my OPINION, imagining the WHOLE code, that one will give better readability. HOWEVER, some people may argue that it will spend some of your time typing those brackets over and over..
As per the PSR standards any structure must always enclose the code in parentheses.
The body of each structure MUST be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body.
from the official website
Please have a look under control structures section http://www.php-fig.org/psr/psr-2/

what actually is a loop? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I know that there are a couple ways to loop in various languages - a "for" loop, a "while" loop, a "do-while" loop, and just "loop" in Ruby for example. And I know there are various functions in every language that are pre-written in that language - for example the .each function in Ruby (which I think is based on a "for" loop and written entirely in Ruby and is replicable just using the language).
But what is the logic behind loops? Are they programmed from control flow statements in Assembly or even binary? And in fact, now that i think about it, what is the origin of all programming structures in general (such as variable name/value associations, arrays, hashes, etc - sorry if my terminology is wrong). Can anyone recommend sources to read more on this?
Loops in general are jumps in the program flow. When you compile your code i.e. in C, it gets "converted" to assembly, where you can see that loop structure:
So you start at a certain adress and do some stuff (i.e. ADD) which basically is everything inside your loop. At the very end then is a jump instruction back to the adress where you started. Breaking is is then i.e. made through a conditional jump inside your loop and thus you don't get to the jump instruction again and therefore do not loop again.

What are the advantages and disadvantages of metaprogramming? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
As the title stated, what are the pros and cons of Ruby meta-programming?
So far I know the advantages of Ruby meta-programming give us quite flexible ways to write our code using code that writes code. But what are the disadvantages? Maintainability issues?
I feel that meta-programming helps to scale an app also.
Can anyone can give more details about this?
Almost everything I know about metaprogramming in Ruby comes from "Metaprogramming Ruby: Program Like the Ruby Pros". It's a great read, and I suggest it to everyone who want to understand metaprogramming.
I'd say the main advantage is to have incredibly flexible code, which can adapt swiftly to any changes one could imagine. Things like send, respond_to? or define_method really give you the possibility of writing beautiful code.
On the other hand, I'd say that the main disadvantage is to make code harder to read and harder to debug, since most of the times the code isn't there yet! It will be generated at runtime, so you can't actually read it when you debug it.

Resources