编程提示经验

芋道框架万字详解(前后端分离)、若依框架、yudao-cloud保姆级攻略 芋道开发框架,低代码平台 阅读详情
一 基础部分
Introduction
I assume you are downloading some source code from time to time. You can learn a lot about a programmer while looking at his source code. There are some tips to help you write clear and effective code. So let's skip this boring introduction and go strait to the article!


Some tips
When naming a variable or a function, try to choose a significant name to help you (and your co-workers) understand the code. You can also use Hungarian notation.


Check the following code. Can you understand what it does?


a=Func1();
b=Func2();
if (a<b)
{
    if (!Func3())
    {
        Func4(1);
        return;
    }
    Func5(2);
}
Now let's write it again:


dtLastBackup=GetLastBackupDate();
dtCurrent=GetCurrentDate();
if (dtLastBackup<dtCurrent)
{
    if (!DoBackup())
    {
        ErrorMsg(IDS_ERRORBACKUP);
        return;
    }
    MessageBox(IDS_BACKUPSUCCEEDED);
}
Now we can understand the code. It is also a good idea to prefix member variables and global variables.


g_szPath=GetPath();
m_nCount=0;
Here we can understand immediately that g_szPath is a global string variable while m_nCount is an integer member variable.


Use uppercase in each word of a variable/name: Use "GetPathString" instead of "getpathstring".


Try to avoid using global variables and static functions. Use them only when necessary. Use member variables instead.


Add a comment to each major function and each block in your code. This way you and your co-workers can understand what is the purpose of a block/function.


Try to write "beautiful" code to be able to understand it.


for (int y=0;y<20;y++) 
  {for (x=0;x<20;x++) cout<<"x";cout<<"\n";}
for (int y=0;y<20;y++)
{
    for (x=0;x<20;x++)
        cout<<"x";
    cout<<"\n";
}
Try to use objects when you need to write initialization code. This way you won't forget to release memory. You can also use smart pointers instead of regular ones.


For example, you can write a class to wrap a database object, so you won't forget to close it. It will be closed automatically when the object is being destroyed.


class DBClass
{
protected:
    DB m_db;
public:
    ~DBClass()
    {
        Close();
    }
    bool Open();
    bool Close();
    DB operator->()
    {
        return m_db;
    }
};


void InsertTable(const char *szTableName)
{
    DBClass db;
    if (db.Open())
        db->Insert(szTableName);
}
Use resources for strings and macros/enums for integers. This way you won't have to follow all your code while changing it.


void ResetBoard(char pBoard[20][20])
{    // Suppose you want to switch to 25x25 board???
    for (int x=0;x<20;x++)
        for (y=0;y<20;y++)
            pBoard[y][x]=0;
}


// We can change the board size by changing only one line
#define BOARD_SIZE 20


void ResetBoard(char pBoard[BOARD_SIZE][BOARD_SIZE])
{
    for (int x=0;x<BOARD_SIZE;x++)
        for (y=0;y<BOARD_SIZE;y++)
            pBoard[y][x]=0;
}
Split your project into several files grouped by functionality. This way you can reuse your code easily. This is also useful when you follow your functions flow.


Use functions/macros instead of copy/paste your code. If you want to change something, you need change it only at one place.


ALWAYS handle errors/exceptions! Don't let your application crash due to an exception you forgot to catch.


Use TRACE/ASSERT as much as possible. They don't affect the release build but they can save you a lot of time while debugging.


Take some time to search for existing code instead of reinventing the wheel. For example: Suppose you want to download a file from the internet. Why open socket, parse headers (HTTP) and read the data when you can use existing objects to download files for you?


When using multithreading programming, don't forget to use critical section/mutex wherever we are using shared variables.


Use _T and text macros so you'll be able to build a Unicode version of your application. For example, use _tcslen/lstrlen instead of strlen.


Use sizeof and don't assume you know the size of your var iable/struct. This creates a portable code. Use fwrite(&nCount,sizeof(nCount)); instead of fwrite(&nCount,2);. An integer may be 2 bytes or 4 bytes long. Let the compiler define it.


Avoid using the heap. Try to use stack based variables. Don't be afraid of "stack overflow". The stack limit is equal to your memory limit. This way you can also avoid defragmentation of your memory.


Try to use standard libraries instead of others. Consider using STL instead of MFC when possible. This way you write a portable code.


Last but not least(最后一条但非常重要): BACKUP, BACKUP and BACKUP! You spend half a year working on your application just to find out that your HD crashed. Consider using CVS to track changes.(这一条我没做到,对于重要的资料,应该保存两份,自己一份,服务器上一份,明天就备份去!妈的!)


These are some basic tips. I am sorry for my poor English but I hope you can understand it anyway. If you have any other tips, you can send me and I'll add it to the article.


二 中级部分
1. Code for human consumption(消费)
It is one of the most pervasive(adj.普遍的) misunderstandings in computing that the source code is for the computer's consumption. Computers work with low-level binary code, a series of impenetrable(adj.费解的) 1's and 0's or hexadecimal numbers, not the structured high level languages we code in. The reason that these languages were developed was to help the programmer.


In practice, coding for human consumption often means coding for clarity first, over efficiency and speed second.


2. Comment often and comment well
The comment is the extreme example of a language element for human consumption. Most compilers will strip(vt. 剥去) the comments from the executable program. The purpose of the comment is to tell you (and any future developer) what the program is intended to do. Write comments with this in mind - and avoid simply restating(vt.换一个方式叙述) the code.


Good comment: Disable button to prevent its activation. 
Bad comment: Set cmd = False 
A good indication that you have got the level of comment right: could someone understand what your program does if all but the comments were removed?


3. Layout code to increase legibility(n.易读性)
Just as it is important for an author to split a book into chapters and paragraphs that aid reading so it is important for the developer to consider the layout of the code and how that can aid readability of the code. In particular, any code branch (an IF..THEN...ELSE construction) and any code repetition (a WHILE...END WHILE construction) should be indented so that it is easy to see where they start and end.


4. Expect the unexpected and deal with it
Before you open a file, make sure that the file is present. Before you set focus to a control, make sure that the control is visible and enabled. Try to work out what conditions could cause your code to fail and test for them before they cause the program to fall over. Also use explicit data type conversion wherever possible.


5. Name your variables to aid readability
There are a number of strategies to variable naming. The key is to be consistent and to be as informative as possible. If you name a variable nMonth, you give the programmer extra information as to what that variable is expected to contain. I personally prefer the Hungarian notation style - but whichever style you use, consistency(n.一致性) is the key.


6. Keep your functions and subroutines simple
A function or subroutine should ideally only do one thing. One of the greatest sources of misunderstandings, in my experience, is a subroutine that does a number of different operations. This should be split into separate functions for each different thing it is doing, so that these in turn are easy to reuse, and the scope of a code change is easy to understand.


7. Scope(限制...的范围) functions and variables appropriately
Functions and variables that are only used in one module should not be visible outside that module. Variables that are only used in a function or subroutine should not be visible outside that function or subroutine. This prevents any use of a variable or function outside of where it makes sense to use it.
 
国民技术N32G45X低功耗问题之更改全局变量和栈在RAM中的地址 最近项目中用的MCU是国民技术的N32G45X,MCU的资源很丰富,RAM也很大,配套的库和例程也比较全,用起来还是不错的,但是在调试低功耗的时候还是费了不少劲。 首先由于项目低功耗需要,只能选择停机模式2(STOP2)。根据数据手册的描述,在此模式下外部低速时钟开启, RTC运行, R-SRAM保持,所有I/O状态保持,独立看门狗处于关闭状态,25℃的条件下电流为10个微安。 用的这款MCU的RAM是144KB,分为SRAM(128KB)和R-SRAM( Retention SRAM – 16KB),如果 阅读详情

相关推荐

AI学习记录 - transformer的位置编码的理解

先看三角函数为什么可以,我们看下三角函数的图像先提一个点:位置信息肯定是唯一的,或者是在很长的文字当中,起码重复度不会太高,x轴代表位置,y轴代表叠加上去的值1、假设只使用一个三角函数sin,我们将第2和第14两个位置输入到sin,他们的值很像,在神经网络的训练中,很容易把第2个位置和第14个位置当成是一个位置,不满足我们上面说到的要求位置唯一性。sin2sin14。

weixin_43954090的博客 1422

Expected ‘,‘ instead of ‘u‘

数据中带有\,用空字符串替换后,提示 Expected ',' instead of 'u'点击格式化,即可以json的形式浏览数据了,简单明了,要的是这个效果。解决方案:把jsonData的值的最外层的双引号去掉,变成下图这样。这种情况可能是json里面嵌套json。

a_15715324986的博客 749

MATLAB实战:用ARMAX模型搞定电力系统惯量评估(附完整代码)

本文详细介绍了如何利用MATLAB中的ARMAX模型进行电力系统惯量评估,解决新能源占比提高带来的系统惯量下降问题。通过系统辨识技术,从电网运行数据中准确提取惯量参数,并提供完整的代码实现,帮助工程师快速应用于实际工程。

peach的博客 642

Postman发送请求时报错Expected ‘,‘ instead of ‘s‘

背景 通过nodejs创建一个简易的http服务器http.createServer,监听到连接后直接返回一个标准格式的json串 Postman发送GET请求,请求成功并正确返回了json串,但是Postman显示错误 Expected ',' instead of 's' 原因排查 在Postman中把返回的串按照Raw或Preview或TEXT格式显示 检查转换后的格式是不是==一个...

performance 9404

浮点数运算的陷阱与启示:FP16加法器/乘法器开发中的常见误区

本文深入探讨FP16加法器和乘法器开发中的常见误区,包括FP16格式解析、阶码对齐陷阱、尾数运算风险及规格化处理。通过Verilog实战代码展示防御性编程技巧,帮助开发者避免边界条件处理和异常状态管理中的错误,提升硬件设计的可靠性和效率。

dell8的博客 257

保姆级教程:在ROS Noetic下,手把手教你将自定义A*全局规划器打包成插件(附避坑指南)

本文提供了一份详细的ROS Noetic下自定义A*全局规划器插件开发指南,涵盖从环境准备、算法优化到插件配置与调试的全流程。通过实战案例和避坑技巧,帮助开发者高效实现机器人导航中的全局路径规划,特别适合需要优化ROS导航栈的开发者。

weixin_29168675的博客 292

Spring Expected ':' instead of 't' 错误原因

最近使用JsonSerializer<>编写自定义Serializer遇到2次序列化报出 Expected ':' instead of 't' 的错误,由于第一次解决办法没进行记录,导致第二次依旧花了点时间,所以在这里记录下。 Debug发现异常发生在IndexedListSer...

chihongsui5708的博客 4315

基于51单片机的电压表【0-20V,ADC0832,4051,自动量程】(仿真)

自动换挡式数字电压表就是一个基于单片机的能自动切换量程的数字电压表,其作用是在使用低位的AD转换实现高精度电压测量,可以测量0~20V的电压,并且具有0~0.2V(待测电玉低于0.2V时),0~2V(待测电压低于2V时)和0~20V三个量程自动匹配测量,当待测电压值发生变化时,电压表可以根据输入电压情况自动选择合适的量程进行测量,并且把测量结果显示出来。(2)实现电压表精度测试,最高精度要求0.01V;(1)实现0~20V分3档式电压表;

qq_63964231的博客 1503

postman的body中报“Expected ‘,‘instead of ‘p‘”的解决方法

一、问题现象 二、解决方法 按照提示检查第6行上面一行的内容,最后发现是第5行最后的双引号符号使用错误,修改后就ok了!!

weixin_46662419的博客 3774

运算放大器电路仿真实践:从零实现完整示例

通过一个完整的运算放大器电路仿真示例,深入掌握电路仿真的关键步骤与技巧,帮助电子爱好者和工程师快速上手实际项目设计。

weixin_34725745的博客 1325

华为OD新系统机试真题 - 准备生日礼物(Py/Java/C/C++/Js/Go)

华为OD机试新系统真题 4.8【准备生日礼物】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【准备生日礼物】

qq_45776114的博客 1504

解决JNI WARNING: expected return type 'L'的问题

程序报JNI WARNING: expected return type 'L'的错误,可能调用方法不对。 比如:返回值是boolean,而调用CallObjectMother,就会报错这个错误,应该调用CallBooleanMother。 所以出现该错误的时候,可以检查下调用的方法是否正确。

善感的人 4661

20.Labview使用子vi进行VI间的数据传递详解(模块化)

子VI(模块化)题主在题注中也提到, 子VI的建立只是为了减少程序的负担,那么我们当使用子VI的时候首先就要明确什么是子VI?什么样的程序可以作为子VI?首先我们来看NI官方给的定义,具体说明如下图所示:题主已经将重点画出来了,在官方的解释中将子VI定义为了模块化,大家可以看到创建模块化子VI的特点在于减少代码重复工作优化了程序的框架结构。把一个代码打包,需要的时候就拎出来让他处理处理,不需要的时候就让它躺在我的列表里就行了也不占用运行空间。那么创建子VI需要需要注意以下几点:①任何一个程序。

关注我,更多技术问题欢迎私聊~ 3524

duboo ERROR 10844 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, d

2021-04-29 10:39:47.148 ERROR 10844 — [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, dubbo-service1 register failed…NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr=‘localhost:8848’, endpoint=’’,

weixin_49723367的博客 3795

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Nacos服务注册失败解决方案

比如,我的springboot版本是3.3.4,那么我的springcloud版本是 2023.0.3,springcloud Alibaba版本是2023.0.3.2,Nacos客户端版本是2.4.3。然后,找到Nacos的配置文件去改配置,找到Nacos的文件夹目录,进入conf目录,找到application.properties文件打开。找到Nacos的文件夹目录,进入bin目录,启动cmd,输入startup.cmd -m standalone命令启动服务。第二步,检查Nacos是否启动。

Chen_beichen的博客 1981

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

解决nacos集群搭建,服务注册失败

nacos集群搭建,微服务注册nacos。

weixin_44197039的博客 1万+

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

c.a.cloud.nacos.registry.NacosServiceRegistry:62 - nacos registry

首先检查你项目中是否配置了类似于 <!-- 如果走配置中心需要添加此依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> 的配置,如果配置了,那就检查你的nacos服务的版本和配置中的

qq_39719302的博客 1545

服务注册到nacos集群失败

服务注册到nacos集群失败

hungteshun的专栏 3209

nacos服务注册失败的坑

报错信息: 2021-08-10 22:42:00.371 ERROR 12560 --- [ restartedMain] c.a.c.n.registry.NacosServiceRegistry : nacos registry, cloudalibaba-sentinel-service register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='local

Lance9的博客 8510

解决Nacos集群搭建,Nginx负载均衡后服务注册失败

搭建Nacos集群后,使用Nginx实现对Nacos集群的负载均衡后,springboot服务通过Nginx方向代理注册进Nacos集群。

m0_50932526的博客 4237

macOS 鼠标增强工具 Mac Mouse Fix 快速上手:3 个自定义玩法

macOS 上的第三方鼠标体验一直别扭:滚轮一格一格地跳,侧键按下毫无反应,窗口管理要靠触控板手势。Mac Mouse Fix 是一款开源的 macOS 鼠标增强应用,用平滑滚动、按键自定义和触控板式手势,把十几块的普通鼠标拉回触控板水准。 ## 一条命令安装并验证 🖱️ 有 Homebrew 环境的话,在终端执行: ```bash brew install mac-mouse-fix `

gitblog_00291的博客 806

alibaba-nacos-discovery-server在nacos中注册不了的问题

描叙:下文为我的pom.xml &lt;parent&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.2.RELEASE&lt;/versi...

qq_23727789的博客 1万+

nacos单机服务注册源码解析

nacos单机服务注册源码解析

m0_54258715的博客 694

记录一次在k8s上nacos2.3.1无法注册微服务的问题

*问题回顾:**在k8s平台上以有状态副本集的方式部署nacos2.3.1,若依微服务无法注册到nacos上。

peng狗头的博客 2629

手把手教你Spring Cloud Alibaba系列教程:Nacos实现服务注册与发现

Nacos致力于帮助您发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos帮助您更敏捷和容易地构建、交付和管理微服务平台。Nacos是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。在接下里的教程中,将使用Nacos作为微服务架构中的注册中心(替代:eurekba、consul等传统方案)以及配置中心(spring cloud config)来使用。

菜鸟路上的小白 960

Nacos使用

文章目录NacosNacos简介 Nacos Nacos简介 为什么叫 Nacos 前面四个字母分别表示 Naming 和 Configuration 的前两个字母, 最后一个s 为 Service Nacos是什么? 一个更易于构建云原生运用的动态服务发现、配置管理和服务管理平台 Nacos: Dynamic Naming and Configuration Service(动态命名和配置服务) Nacos 就是注册中心+ 配置中心,等价于Eureka + Config + Bus Naco

L_Mr_ll的博客 415
上一篇: 《97 Things Every Software Architect Should Know》读书笔记
下一篇: 工作经验总结
backard
博客等级 码龄23年 18粉丝 97原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址