如何在手机上编写c语言代码
When coding, the coding style you follow can be really important. Especially when you are working with a team or you plan on sharing your code.
编码时,遵循的编码样式可能非常重要。 特别是在与团队合作或计划共享代码时。
Most of these guidelines are standard and can be applied to most programming languages. However, here you have applications and snippets with C++ code, so you can get familiar with it more easily.
这些准则中的大多数是标准的,可以应用于大多数编程语言。 但是,这里有带有C ++代码的应用程序和代码片段,因此您可以更轻松地熟悉它。
Remember that these are only recommendations for achieving clarity, which can be a personal preference. So take these pieces of advice into account but don’t take them to the letter. Sometimes breaking some of these rules can lead to cleaner code.
请记住,这些仅是实现清晰度的建议,可以个人喜好。 因此,请考虑这些建议,但不要将其付诸实践。 有时违反其中一些规则可能会导致代码更简洁。
Make sure you create good variable names. For example, if you are creating a game, avoid using the variable “a” and instead use something like “p1” when referring to player 1.
确保创建良好的变量名。 例如,如果要创建游戏,则在引用玩家1时,避免使用变量“ a”,而应使用类似“ p1”的名称。
The hungarian notation is commonly used and can give you some guidelines for declaring variables.
匈牙利符号是常用的符号 ,可以为您提供一些声明变量的准则。
Also, PLEASE, use comments.Not kidding, just try to read some old projects you made without comments… now imagine being someone else who didn’t even code it.
另外,请使用注释。不要开玩笑,只是尝试阅读一些没有注释的旧项目……现在想像是甚至没有编写代码的其他人。
Global variables can be easy to use, and when you're just working with a little code it might look like a great solution. But when the code gets larger and larger, it becomes harder to know when are they being used.
全局变量可以很容易使用,当您只使用一些代码时,它似乎是一个很好的解决方案。 但是,当代码变得越来越大时,就很难知道何时使用它们。
Instead of using global variables you could use variables declared in functions. This can help you tell what values are being passed so you can identify errors faster.
可以使用在函数中声明的变量来代替使用全局变量。 这可以帮助您确定正在传递的值,从而可以更快地识别错误。
#include
using namespace std;
// Global variables are declared outside functions
int cucumber; // global variable "cucumber"
This is a common discussion among programmers. Just like global variables, these types of statements are usually considered bad practice. They are considered bad because they lead to “spaguetti code”.
这是程序员之间的常见讨论。 就像全局变量一样,这些类型的语句通常被认为是不好的做法。 它们被认为是不好的,因为它们导致“ spaguetti代码” 。
When we program we want a linear flow. But when using those statements the flow is modified and lead to a “twisted and tangled” flow.
当我们编程时,我们需要一个线性流。 但是,当使用这些语句时,流程将被修改并导致“扭曲缠结”的流程。
Goto was used in the past. But when while, for, if functions came around, however, with the introduction of those structured programming was created. In general avoid using goto unless you are sure it will make your code cleaner and easier to read. An example might be using it in nested loops.
过去使用过后藤。 但是,当功能出现时,随着这些结构化程序的引入而创建。 通常,除非确定可以使代码更整洁且易于阅读,否则请避免使用goto。 一个例子可能是在嵌套循环中使用它。
The usage of break and continue are practically the same. Use them in switches and try to make functions with one purpose so you only have one exit point.
break和continue的用法实际上是相同的。 在开关中使用它们,并尝试实现某一目的的功能,以便只有一个退出点。
Usually there are work arounds to get around this that look clearer and less confusing, eg. while loops. Do this:
通常,有一些变通方法可以解决此问题,这些变通方法看起来更清晰,更不混乱。 while循环。 做这个:
int i=1;
while (i <= 5)
{
if (i == 2)
i = 4;
++i;
}
Instead of:
代替:
for (int i = 1; i <= 5; i++)
{
if (i == 2)
{
i = 4;
}
// Do work
}
They are usually declared after libraries. This groups them together and makes them easier to read. For local variables it's the same: declare them at the top (Other people prefer declaring them as late as possible in order to save memory see: cplusplus.com).
它们通常在库之后声明。 这将它们组合在一起,使它们更易于阅读。 对于局部变量,它是相同的:在顶部声明它们(其他人更喜欢尽可能晚地声明它们以节省内存,请参见: cplusplus.com )。
Just like I said before, I tend to make only one entry and exit to make the flow clearer.
就像我之前说过的那样,为了简化流程,我倾向于只进行一次进入和退出操作。
Doing this systematically will help you do it faster. And in case you want to change the code in the future you will be able to do it without worries.
系统地执行此操作将帮助您更快地执行此操作。 而且,如果您将来希望更改代码,则可以无后顾之忧。
Instead of:
代替:
for (int i = 1; i <= 5; i++)
//CODE
Do this:
做这个:
for (int i = 1; i <= 5; i++)
{
//CODE
}
Use for
when you know the number of iterations, but use while
and do while
when you don’t.
使用for
时候,你知道迭代次数,但在使用while
和do while
,当你不知道。
Use const, pass by value/reference when suitable. This will help with saving memory.
使用const,在合适的情况下按值/引用传递。 这将有助于节省内存。
Write const in caps, datatypes starting with T and variables in lower case.
用大写形式写const,以T开头的数据类型和小写形式的变量。
const int MAX= 100; //Constant
typedef int TVector[MAX]; //Data type
TVector vector; //Vector
翻译自: https://www.freecodecamp.org/news/how-to-write-clean-code-in-c/
如何在手机上编写c语言代码