编写好的代码是非常困难的,如果你愿意的话,很有挑战性,但是如果你是一个优秀的程序员,那么你应该可以毫无问题地控制事情。但是,对于那些不熟悉编程的人,我们想谈谈一些可能会有所帮助的编程基本原则。
程序员的编程原则(Principles)和指南(Guidelines)
这里的想法是拥有高质量的代码,这意味着从不走捷径。尽职尽责,最终一切都会好起来的。是的,你需要很长时间才能完成代码的编写,但至少在一天结束时它不会出现大量错误。以下是程序员应该遵循的一些最重要的编程原则(Principles)和指南-(Guidelines Programmers)
- 查看和优化您的代码
- 对扩展开放,但对修改关闭
- KISS(保持简单,愚蠢)
- 不要重复自己(干)
- 养成记录代码的习惯
1]审查和优化您的代码
不管你认为自己是多么优秀的程序员,你的代码并不总是完美的。考虑到这一点,我们建议检查您的代码是否存在错误并在必要时进行优化。如果一切按计划进行,代码将更有效率,但仍能提供相同的结果。
现在,人们应该意识到事情在不断变化,因此,重新访问您的代码库以进行更改是完全正常的。在项目的整个生命周期中,您可能必须多次执行此操作,因此请记住这一点。
阅读(Read):什么是编程中的函数?(What is a Function in Programming?)
2]对扩展开放(Open),但对修改关闭
编写代码并发布给他人修改是不好的行为。每当您提供更新时,代码很可能会因为您的更新与修改不兼容而中断。因此,请考虑支持让您的代码库支持扩展的能力,因为它可以更轻松地进行维护。
如果您认为有很好的方法可以毫无问题地支持修改,那么请继续。如果计划失败(Just),请确保制定了一个伟大的计划。
3] KISS(保持简单,愚蠢)
您不应将此视为不尊重,因为它是最流行的编码原则之一。你看,这个想法是让你的代码尽可能简单。不要(Don)最终陷入你做事与众不同的境地。
如果你可以在一行中编写一个脚本,那就去做吧,不要试图将它拉伸成多个。编写简单可确保您始终知道发生了什么,并且也更易于阅读。
阅读(Read):低级和高级编程语言之间的区别(Difference between Low-level and High-level Programming languages)。
4]不要重复自己(干)
编写代码时最重要的事情之一是确保永远不要重复自己,例如两次编写相同的代码。走这条路是一个巨大的错误,因此,人们应该养成不惜一切代价避免重复的习惯。
据我们了解,用户应该找到支持循环的算法,以便多次运行单个代码。请记住,DRY代码更容易维护。
5] 养成记录代码的习惯
不是每个人都这样做,但它应该是任何时候的普遍做法。你看,许多专业程序员已经决定记录他们的代码以供将来参考。如果您不知道这意味着什么,那么它本质上就是在关键方面留下笔记。
想象一下(Imagine)在编写代码一年后返回您的代码,但发现自己对为什么做出某些决定感到困惑。现在,如果留下注释,那么就没有理由混淆,这就是为什么所有程序员都应该记录他们的工作加班。
Best Programming Principles & Guidelines all Programmers should follow
Writing good code is very difficult, challenging if you will, but if you are a good programmer, then you should have no problems getting thіngs under control. Howeνer, for those who are new to programming, we wаnt to talk about some basic principles of programming that will likely help.
Programming Principles & Guidelines for Programmers
The idea here is to have quality code, which means, never taking shortcuts at all. Do your due diligence and everything should work out just fine in the end. Yes, it will take a long while for you to complete the writing of your code, but at least it will lack a ton of errors at the end of the day. Here are some of the est Programming Principles & Guidelines Programmers should follow-
- Review and optimize your code
- Open to extension, but closed to modification
- KISS (Keep it Simple, Stupid)
- Don’t Repeat Yourself (DRY)
- Make a habit to document code
1] Review and optimize your code
No matter how good of a programmer you might think yourself to be, your code won’t always be perfect. With that in mind, we suggest reviewing your code for errors and optimize where necessary. If everything goes according to plan, the code will be more efficient, but still delivers the same results.
Now, one should realize that things are constantly changing, therefore, revisiting your codebase to make changes is completely normal. And you may have to do this multiple times throughout the life of your project, so bear this in mind.
Read: What is a Function in Programming?
2] Open to extension, but closed to modification
It’s not good behavior to write code and release it for others to modify. Whenever you deliver an update, chances are the code will break because your update is not compatible with modification. So, please consider supporting the ability to have your codebase support extensions because it allows for easier maintenance.
If you believe there are great ways to support modifications without problems, then go ahead. Just make sure a great plan is in place if the plan goes sour.
3] KISS (Keep it Simple, Stupid)
You shouldn’t view this as disrespect because it is one of the most popular coding principles. You see, the idea is to keep your code as simple as possible. Don’t end up in a situation where you are doing things out of the ordinary.
If you can write a script in a single line, then do it and do not try to stretch it to multiple. Writing simple ensures you always know what is going on, and it is easier to read too.
Read: Difference between Low-level and High-level Programming languages.
4] Don’t Repeat Yourself (DRY)
One of the most important things when writing code, is to make sure never to repeat yourself, as in, writing the same code twice. It’s a huge mistake to go down this route, therefore, folks should make it a habit to avoid duplicates at all costs.
From what we’ve come to understand, users should locate algorithms that support looping in order to run a single code multiple times. Just bear in mind that DRY code is much easier to maintain.
5] Make a habit to document code
Not everyone does this, but it should be common practice at all times. You see, many professional programmers have decided to document their code for future reference. If you do not know what this means, well, it is essentially leaving notes at key aspects.
Imagine returning to your code a year after writing it, but find yourself confused as to why you made certain decisions. Now, if a note was left, then there would be no reason for confusion, which is why all programmers should document their work over-time.