Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
This page contains some code improvement ideas based on removing redundancy from programs.
General rule: A shorter program is better than a longer program that does the same thing.
Length of variable names does not count.
Useful comments do not count.
Single blank lines to not count - but should group/chunk parts of the program.
2. Adjacent conditionals
Adjacent conditional statements can be combined.
The above can be better written as follows (shorter code, more machine efficient, easier to read).
3. Prompts
Common error (from just looking at the required output): When a user prompt is to be displayed, the input should not be done before the prompt is displayed.
Not good:
Good:
4. Empty else parts
Empty else parts of conditional statements should be removed.
Not good:
Good:
5. Nested conditionals
For output such as debugging output, nested conditionals can make the logic more difficult to understand. It is not important that the code may run a millionth of a second faster.
Not as good:
Better:
6. Input and output
It is best, when possible, to use one scanf statement for each line of input and one printf statement for each line of output.
7. Newlines
It is best for output if the "\n" newline is always of the beginning or always at the end of a printf statement.
Good:
Good: Notice the absence of a newline in the first print. That is why I like to put newlines at the end.
Not good: Since extra or missing newlines happen a lot with this way of doing newlines, you may spend a lot of time working out the logic to get the desired output just right.