C++ Best Practices : | Sololearn: Learn to code for FREE!

+105

C++ Best Practices :

1. Always compile at the highest warning level possible 2.Code that is not used (commented out) shall be deleted 3.If you can exit a function early, you should 4.Avoid methods with too many local variables 5.Avoid types with too many fields 6.Avoid types with too many methods 7.Avoid too big types 8.Avoid too complex methods 9.Avoid methods with too many parameters 10.Avoid too big methods Source: https://www.jarchitect.com/QACenter/index.php?qa=questions&sort=votes Credits to: CoderGears

1/28/2018 11:04:53 AM

Chandrakant More

87 Answers

New Answer

+42

1.) Instead of using scanf() and printf() use  ios::sync_with_stdio(false); and cout/cin. Don`t use scanf() and printf()after writing ios::sync_with_stdio(false);, you will get incorrect output. 2.) Instead of writing vector<long long>::const_iterator cit;, write auto cit=[NameOfVector].cbegin(). 3.) Never use std::list<>. They are only useful for large data types. 4.) Use #include <bits/stdc++.h> instead of writing each header file one-by-one. This only affects compile time. And compile time has nothing to do with competitive programming. Only running time matters! 5.) Use range-based for loops instead of writing full for loop for C++ Containers for accessing all values. 6.) Tired of remembering return type of functions. Use auto. 7.) Learn different functions available in <algorithm> library of C++. They are quite useful in places. 8) use different library like <limits> ex:- <limits> gives much more insight about numeric types, such as whether they're signed, the number of base-10 digits they can handle, whether they can represent infinity or not-a-number sentinel values etc. this all will reduce complexity which is helpful in execution of programs effectively well for more library's http://en.cppreference.com/w/cpp/header

+26

As a side note to Hatsy Rei's request for credit. I would like to bring up/point out the reason plagiarism is frowned upon in learning environments, which is ethics. As learners and aspiring professionals we should strive to be as ethical as possible. Ethics not only builds trust, it builds respect. All professional computer science bodies have a strong code of ethics and expect members adhere to it. I know many of us are not yet professionals. But it doesn't mean we should not be forming or practising ethical behaviour. I will leave these links to two such organisations if any one is interested. https://www.acm.org/about-acm/acm-code-of-ethics-and-professional-conduct https://m.ieee.org/about/ethics/index.html

+26

last answers are not matching with the question //mine too 😂

+25

Some of the best sources for C++ tips, and info on SL. https://www.sololearn.com/Discuss/683525/?ref=app https://www.sololearn.com/Discuss/683501/?ref=app https://www.sololearn.com/Discuss/701288/?ref=app

+22

@Swim well in my all cpp codes i have used this type of library functions and header files you can see in my past codes that's why I pointed out those by which program is run in least time and generate the output and in my some answers too this type of header files and function are included so I'm not copied that from that resources which you have mentioned I'm practically implementing that that's why I posted that if you still think it's copied I can remove it 😊😊 // don't want to extend the unnecessary discussion just if you think that no one can make your mind change because everyone has not know everything from starting practicing is more important

+21

I'll try it soon ☺

+19

THANK YOU @ GAWIN STEACY I believe teaching technical issues. YOUR input is so needed and received. .

+19

@swim Sure. There is this thing called plagiarism. If you can pick up a link and justify that Gawen took the words entirely out of it, *and made it hers*, I will take appropriate action, to prompt her for an external link.

+18

Sounds like a lot of these rules came from this site by user CoderGears. https://www.jarchitect.com/QACenter/index.php?qa=questions&sort=votes Mind to quote your sources?

+18

@swim This is a case wherein the text in this thread is similar to a high degree as the one on the link I provided. Whether or not best practices are widespread is irrelevant. Whether or not the link I posted contains the original written version is irrelevant (although that most probably is the source, and I know because I scrapped the web with my bare hands for it, and you will too if you actually checked). If he did not write it himself, he has to quote the source. Period.

+17

welcome 😊😊 I'm just a beginner

+16

@Chandrakant If you don't want to, I can help you to edit it in. If you remove it afterwards, I'm afraid I will have to remove your thread. I believe I have been very clear with my stance. I beg for your compliance.

+16

great post thanks all sololearners for sharing

+16

My suggestion to all novices and pros in any language is reading these 2 books for at least 2 times. ( Sounds crazy! ) Code Complete 2 [ https://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670 ] Clean Code [ https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=pd_bxgy_14_img_2?_encoding=UTF8&pd_rd_i=0132350882&pd_rd_r=XV08CK4MJR3J7VNW6A09&pd_rd_w=k0pmO&pd_rd_wg=sPw6c&psc=1&refRID=XV08CK4MJR3J7VNW6A09 ] I'm still in the process... !

+14

How about using an IDE over the SL playground to practice. Code:Blocks Cross platform https://www.sololearn.com/discuss/608805/?ref=app Visual Studio - Great for Windows 10 and current Mac OS' https://www.sololearn.com/discuss/410578/?ref=app

+13

@Vlad Thanks! Termux Seems pretty sweet. Did not know about it. https://termux.com

+12

Making use of header files. so the code can be more organized. Not all crammed in the same file containing the main function. ( cannot be avoided in SL )

+11

thanks for sharing 👍 😃

+11

@Swim I agree that I may lack consistency. To me, however, it is as simple as spotting something wrong, and then undoing it. As for whether or not something is considered to be within the bounds of plagiarism, it has to be evaluated according to a per-case basis. (Perhaps due to my lack of ability and skill) I cannot provide you with a general rule as to how this works. @Chandrakant More Please add the source to your original post.

+11

@Vlad I heard Vim is good as well. Not into command line though.