[C] The usage of ? and : when cleaning up recursive code

  • Thread starter Thread starter STEMucator
  • Start date Start date
  • Tags Tags
    cleaning Code
Click For Summary
The discussion focuses on the utility of the ternary operator (?:) in recursive C code, highlighting its potential to simplify code. Several examples demonstrate recursive functions for calculating powers, counting digits, and finding occurrences in arrays. Participants debate the clarity and performance of using the ternary operator versus traditional if-else statements. While some argue that the ternary operator can lead to more concise code, others emphasize the importance of readability and maintainability, suggesting that overly compact code can hinder understanding and debugging. Concerns about recursion's overhead and stack limits are raised, with some advocating for iterative solutions instead. The conversation also touches on the balance between syntactical elegance and semantic clarity, with a consensus that clarity should take precedence in code design, especially for future maintainability.
  • #31
jim mcnamara said:
You are correct - I meant x, what is point of all those gyrations to return a 1 (double or not)? I thought the function implied it was calculating some value to a power of n.
x^n = \prod_{i=1}^{n}{x} = \underbrace{x \cdot x \cdot x \cdot \ldots \cdot x}_\text{n times}
The function calls itself n times.
Mark44 said:
This is a very silly reason. To save a few bytes of storage, you're dramatically decreasing the readability of your code, which makes it much more difficult for the poor schlub who has to maintain your code. That's a bad tradeoff, especially when RAM is so cheap. I just bought a 32 GB zip drive for about $30.
It was a joke. Either you are being sarcastic (if so, +1 for you) or you are really bad at getting it. See the post I replied to.
 
Technology news on Phys.org
  • #32
Mark44 said:
This is a very silly reason. To save a few bytes of storage, you're dramatically decreasing the readability of your code, which makes it much more difficult for the poor schlub who has to maintain your code. That's a bad tradeoff, especially when RAM is so cheap. I just bought a 32 GB zip drive for about $30.
mafagafo said:
It was a joke. Either you are being sarcastic (if so, +1 for you) or you are really bad at getting it. See the post I replied to.
No sarcasm - I didn't pick up that you were joking. It's harder to pick up subtleties on the interwebz than in face-to-face conversations where you can see other cues. When people are writing tongue-in-cheek on the Net, they often use smileys to send that cue - :D
 
  • #33
People don't get my sarcasm in real life either. It is not a novelty, I just don't want you or anyone else getting serious about s*** I say.

Unless you are on a machine where your secondary s. cap. is < 8 MB, counting bits is unnecessary.

I think that some embedded stuff also ship the source, in that case reducing size to the extreme be needed (but there are better ways to achieve it than replacing if-then-else by ternaries).

Don't like the smileys. See the one you used? 137 bytes. Keep killing the planet we live in.
 
  • #34
Your return when n=0 should be 1.0 since the function returns doubles.

As far as computing the nth power of a variable, it looks correct otherwise.

y=power(2.0,1)
n=1 --> 2.0*power(2.0,0)
n=0 --> 2.0*1.0
y=2.0
 
  • #35
jedishrfu said:
Your return when n=0 should be 1.0 since the function returns doubles.

As far as computing the nth power of a variable, it looks correct otherwise.

y=power(2.0,1)
n=1 --> 2.0*power(2.0,0)
n=0 --> 2.0*1.0
y=2.0

See my version. What you pointed out was "corrected" there.

Returning 1 instead of 1.0 is UGLY, not WRONG. (#22 explains why)
 
  • #36
For all those talking about 1 instead of 1.0, don't forget about type promotions as mentioned earlier.
 
  • #37
mafagafo said:
See my version. What you pointed out was "corrected" there.

Returning 1 instead of 1.0 is UGLY, not WRONG. (#22 explains why)

My apologies, I never saw your corrected version. In any event ugly or not, you must return the data type indicated. If you want to return some sort of error then it is common in some languages such as Java to return a NaN to indicate some error or to throw an exception to be caught by the application program.

Please be aware, that many of us here have years of experience in programming and look at things far differently from you.

The idea of writing things more compactly in the C or Java world never goes over so well. We are more interested in program maintainability and hard to read expressions compactly designed to eliminate intermediate results is considered bad style for two reasons:

- one is the next programmer may not be as familiar with the expression and

- two if it needs to be debugged the programmer doing it will curse and convert it to intermediate results to see exactly what is going on.

So have some compassion for your fellow programmers and write code stressing clarity over compactness, simplicity of complexity and with useful comments where needed.

Also take advantage of the latest programming tools like NetBeans, Eclipse or IntelliJ IDEs. They have many features that will save you time such as code refactoring, semantic understanding of what you're writing, and immediate error message feedback
 
  • #38
Why the lecture fella?
You thought:
1) I was OP?
2) I write OS code with 300 characters per line?

"In any event ugly or not, you must return the data type indicated."
HE DOES RETURN A DOUBLE.
 
  • #39
mafagafo said:
Why the lecture fella?
You thought:
1) I was OP?
2) I write OS code with 300 characters per line?

"In any event ugly or not, you must return the data type indicated."
HE DOES RETURN A DOUBLE.

My apologies again.
 
  • #40
Closed for moderation
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K