10 good practices to code in modern C++ (ChatGPT)

10 good practices to code in modern C++ (ChatGPT)

First, I welcome back! (?). Secondly, the technical content of this post was generated using ChatGPT! Now, what is ChatGPT? Well, I asked the following:

I wrote on the presentation of the post that the content were generated using ChatGPT, then, which is the answer for the reader about what is ChatGPT?

And, ChatGPT answer to me this:

ChatGPT is a large language model developed by OpenAI. It's trained on a massive dataset of text and is capable of generating human-like text. It's been used in a wide range of applications, such as writing code, composing poetry, and even writing news articles. In this case, it was used to generate technical guidelines and a funny closing for a blog post about good practices in modern C++.

So, my job here is done.. just asking questions and giving answers! Well, not more to say..here the 10 good practices to code using modern code C++:

The Top 10 Commandments of Modern C++ Coding:

  1. Thou shalt not mix tabs and spaces (lest ye be cast into the indentation abyss).
  2. Thou shalt always use nullptr instead of NULL (for the love of all that is holy).
  3. Thou shalt not use raw pointers unless thou art prepared to smite memory leaks with the wrath of a thousand smart pointers.
  4. Thou shalt not use C-style casts (thou shalt use static_cast, dynamic_cast, and const_cast instead).
  5. Thou shalt always initialize variables upon creation (lest thou leave them in a state of eternal mystery).
  6. Thou shalt not use the goto statement (it is an abomination unto the language).
  7. Thou shalt always use const when appropriate (lest ye accidentally change something that should not be changed).
  8. Thou shalt always use auto when appropriate (for the sake of thy sanity).
  9. Thou shalt not fear the Standard Template Library (for it is thy friend).
  10. And lastly, thou shalt not forget to have fun (for coding is not just a job, but a joy).

And a bonus one: 11. Thou shalt not use a single line of code without a proper comment explaining its purpose (lest thou confuse thy fellow coders and thy future self).

Remember, these commandments are not just suggestions, but a sacred code for all C++ developers to live by. Follow them and thou shall code like a pro!

The previous content were generated with the following question:

Can you write a funny and technical entry for my blog located in ecode.dev regarding good practices at using modern C++? The post should be enumerating at least 10 important points for developers

See? It is amazing!! Now, those ones were so general that I push a little bit asking the following:

umm I dont want commandments, I prefer technical guiding

And again, this is the answer:

Sure, here's a list of 10 technical guidelines for using modern C++:

  1. Always use C++11 or later. This version of the language brings many improvements and new features that will make your coding experience much more pleasant.
  2. Use smart pointers to manage memory. Smart pointers, such as std::unique_ptr and std::shared_ptr, are a safer and more efficient way to manage memory compared to raw pointers.
  3. Use const whenever possible. This will help prevent accidental modification of data and improve performance.
  4. Use auto to deduce variable types. This reduces verbosity and improves readability of your code.
  5. Use the auto keyword for range-based for loops. This greatly simplifies iteration over containers.
  6. Use template metaprogramming (TMP) judiciously. TMP can make your code more efficient, but it can also make it harder to understand and maintain.
  7. Prefer standard library functions over writing your own. The standard library provides high-quality, well-tested implementations of many common algorithms and data structures.
  8. Use exceptions for error handling. Exceptions provide a more robust and expressive way to handle errors compared to traditional error codes.
  9. Use std::string instead of char* or char[]. std::string offers many advantages over traditional C-style strings, such as automatic memory management and a rich set of member functions.
  10. Use the final keyword to prevent class inheritance. This can help prevent unexpected behavior and improve performance.

By following these guidelines, you can take full advantage of the power and expressiveness of modern C++ while also maintaining high levels of readability and maintainability in your code.

My last question regarding the same topic, was this:

I like those, but could include more things focused on C++ 14 and c++ 17?

Sure, here are some additional guidelines for using C++14 and C++17 specifically:

  1. Use C++14's std::make_unique and std::make_shared for creating smart pointers. These functions provide a more convenient and safer way to create smart pointers compared to using new.
  2. Use C++14's std::exchange for atomic operations. This function provides a more readable and efficient way to perform atomic operations compared to using std::atomic directly.
  3. Use C++14's std::is_final to check if a class is final. This is a more readable and expressive way to check if a class is final, compared to using the final keyword.
  4. Use C++17's std::optional for optional values. std::optional provides a safer and more expressive way to handle optional values compared to using nullptr or std::unique_ptr with a sentinel value.
  5. Use C++17's std::variant for discriminated unions. std::variant provides a safer and more expressive way to implement discriminated unions compared to using std::tuple and std::visit.
  6. Use C++17's std::string_view for working with string data. std::string_view provides a more efficient and safer way to work with string data compared to using const char* or std::string.
  7. Use C++17's std::invoke for calling callable objects. std::invoke provides a more readable and general way to call callable objects compared to using std::bind or std::mem_fn.
  8. Use C++17's std::apply for passing tuple elements as function arguments. std::apply provides a more readable and general way to pass tuple elements as function arguments compared to using std::tuple_cat or std::tuple_element.
  9. Use C++17's std::filesystem for working with the file system. std::filesystem provides a more modern and expressive way to work with the file system compared to using std::experimental::filesystem or platform-specific libraries.
  10. Use C++17's std::any for holding any type of value. std::any provides a more general and efficient way to hold any type of value compared to using void* or std::function.

By following these guidelines, you can take full advantage of the new features and improvements provided by C++14 and C++17, and write more expressive, efficient, and maintainable code.

The last part, giving that is the same, then, I asked this:

would you change this phrase "By following these guidelines, you can take full advantage of the new features and improvements provided by C++14 and C++17, and write more expressive, efficient, and maintainable code." to look different than previous one... Also, I need a funny closing for the post

And the answer is epic:

Sure, here's a revised closing for the post:

"By adhering to these technical guidelines, you'll be tapping into the full potential of C++14 and C++17. Your code will be expressive, efficient, and a joy to maintain. So go forth and write beautiful, modern C++ code! Just remember, with great power comes great responsibility, so use your newfound C++ skills for good and not for evil."

As for a funny closing, here's one: "And don't forget, a good C++ developer always 'throws' their exceptions, not 'catch' them."

For now, I am totally surprised with the full potential of ChatGPT for text-based content in many ways! No, this part wasn't written by ChatGPT (wasn't?) 🤖

From a geek to geeks.

Share Tweet Send
You've successfully subscribed to The ecode.DEV repository
Great! Next, complete checkout for full access to The ecode.DEV repository
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.