+ 106

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

28th Jan 2018, 11:04 AM
Chandrakant More
Chandrakant More - avatar
86 Answers
+ 43
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
29th Jan 2018, 10:05 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 27
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
29th Jan 2018, 1:14 PM
jay
jay - avatar
+ 26
last answers are not matching with the question //mine too šŸ˜‚
29th Jan 2018, 1:16 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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
29th Jan 2018, 1:29 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 21
I'll try it soon ā˜ŗ
29th Jan 2018, 10:17 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 19
THANK YOU @ GAWIN STEACY I believe teaching technical issues. YOUR input is so needed and received. .
29th Jan 2018, 10:19 AM
Just -in
+ 19
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?
29th Jan 2018, 11:46 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
29th Jan 2018, 12:57 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
29th Jan 2018, 12:41 PM
Hatsy Rei
Hatsy Rei - avatar
+ 17
welcome šŸ˜ŠšŸ˜Š I'm just a beginner
29th Jan 2018, 10:23 AM
GAWEN STEASY
GAWEN STEASY - avatar
30th Jan 2018, 12:35 PM
Babak
Babak - avatar
+ 16
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
29th Jan 2018, 1:25 PM
Manual
Manual - avatar
+ 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.
29th Jan 2018, 1:33 PM
Hatsy Rei
Hatsy Rei - avatar
+ 16
great post thanks all sololearners for sharing
29th Jan 2018, 3:42 PM
gnaneswari kolathuru
gnaneswari kolathuru - avatar
+ 13
@Vlad Thanks! Termux Seems pretty sweet. Did not know about it. https://termux.com
29th Jan 2018, 2:00 PM
Manual
Manual - avatar
+ 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 )
29th Jan 2018, 12:10 AM
Manual
Manual - avatar
+ 11
thanks for sharing šŸ‘ šŸ˜ƒ
28th Jan 2018, 12:14 PM
Vukan
Vukan - avatar
+ 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.
29th Jan 2018, 1:23 PM
Hatsy Rei
Hatsy Rei - avatar
+ 11
@Vlad I heard Vim is good as well. Not into command line though.
29th Jan 2018, 1:44 PM
Manual
Manual - avatar