Do you practice any principles in writing code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Do you practice any principles in writing code?

Many programmers write that when writing the code they will use the principles that help make the code simple and readable. And what principles do you have?

25th Jan 2018, 7:33 AM
Artem
Artem - avatar
2 Answers
+ 5
I mostly use the DRY (don't repeat yourself) and KISS (keep it simple stupid) principles. DRY means that you should not copy and paste the same code to different parts of your project. If you use a piece of code more than once, write a function, a class or a module, or whatever suits the functionality best, and reuse it wherever you can. KISS means that the code shouldn't get too complex. Simple is often better than complex. It makes the code more readable and also more maintainable. If you have a function that's 100 lines long, you're probably doing it wrong. Look at the pieces of functionality that come together in that function, move every piece of logic into its own function, and call the individual functions in your (previously) big function. That gets you nice, clean and readable code. Separating logic into more sub-problems also makes refactoring much easier, since you can change the internal behavior of one function without affecting the others.
25th Jan 2018, 7:49 AM
SplittyDev
SplittyDev - avatar
+ 3
If you are writing code for yourself by yourself there are but 2 principles you should follow: a) Assume you'll have forgotten everything about the program the next time you start writing on it. b) Assume you'll have to change everything about how you envision the program by the time you finish and make that process easy.
25th Jan 2018, 7:42 AM
Vlad Serbu
Vlad Serbu - avatar