Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Day-2 content #557

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions docs/day-02/comments-in-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Comments serve several purposes:
- Debugging: Comments can be useful for debugging by providing context or temporarily disabling code segments.
- Documentation: They document algorithms, variable purposes, or clarify unclear code segments.
- Collaboration: Comments help other developers understand your code when collaborating on projects.
- Code Readability: Comments significantly improve code readability, making it easier for others (or yourself) to understand the logic and purpose of your code.
- Maintenance: Well-commented code is easier to maintain and update, as it provides context and explanations that can prevent misunderstandings or errors during modifications.

![comment-in-cpp](../../static/img/day-02/comment-in-cpp.png)

Expand All @@ -35,7 +37,14 @@ int main() {
```
In this program, we use both single-line comments (//) to explain the code and prevent execution of those comment lines

## 4. Types of comments in C++
## 4. Best Practices for Writing Comments
- Be Clear and Concise: While commenting in C++, make sure to stick to the point and comment clearly regarding the essential points only.
- Use Proper Grammar and Spelling: Proper grammar and spelling are very important and they make the comments more professional and easier to read.
- Avoid Redundant Comments: Don't comment about the obvious points. Comments should provide additional insight and not repeat what the code is already saying.
- Keep Comments Updated: As the code is modified, make sure to update or remove outdated comments to prevent confusion. Old comments that no longer match the code can mislead developers and cause bugs. So, always make sure to update comments when changing code.
- Avoid over-Commenting or under-Commenting: Too many comments can clutter your code and make it harder to read. On the other hand, failing to comment on complex logic or non-obvious code can again lead to confusion and errors. Thus, try to make a good balance.

## 5. Types of comments in C++
i. Single Line Comment
- Starts with // and applies to a single line only.
- Compiler ignores any text after //.
Expand Down Expand Up @@ -74,4 +83,4 @@ int calculateArea(int length, int width) {
}
```

By effectively using comments, you can improve the understandability and maintainability of your C++ programs, both for yourself and others who may read your code in the future.
By effectively using comments, you can improve the understandability and maintainability of your C++ programs, both for yourself and others who may read your code in the future.
6 changes: 3 additions & 3 deletions docs/day-02/new-line-in-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ Both `"\n"` and `std::endl` produce the same output, but there are differences i

#### Performance :
In terms of performance, `\n` is generally faster than `std::endl`.
`std::endl` flushes the output buffer, which may impact performance, especially for large outputs. On the other hand, `\n` does not flush the buffer immediately, making it more efficient.
`std::endl` flushes the output buffer, which may impact performance, especially for large outputs. On the other hand, `\n` does not flush the buffer immediately, making it more efficient. Even though,`\n` is widely supported, its behavior might vary slightly across different platforms (e.g., Windows, Linux). This can be important for developers working in multi-platform environments.`std::endl` does not show such differences across platforms. Thus, `std::endl` is a safer choice for cross-platform development.

#### Usage:

- Use `std::endl` when you need immediate display of the output or when you want to ensure that certain messages are visible right away, such as in interactive programs or debugging scenarios.
- Use `std::endl` when you need immediate display of the output or when you want to ensure that certain messages are visible right away, such as in interactive programs or debugging scenarios. `std::endl` is also preferred over `\n` for cross-platform development.
- Use `\n` in most cases where immediate flushing isn't necessary for better performance.

Understanding the differences between `std::endl` and `\n` helps in choosing the appropriate method based on the requirements of your program.

Feel free to use either method based on your specific needs!
Feel free to use either method based on your specific needs!