第一篇:优美的程序需要优美的子程序
LinuxAid.com.cn axing〖 返回〗〖转发〗
| 我见过那种一个main函数写了1000多行的程序员,而且为数还不在少数。难道说他们不懂得用函数吗?不见得,但可以肯定的一点是他们没有编写优美代码的意识。那么我们为什么需要子程序呢?我们来看看STEVE McCONNELL是怎么说的。 |
墨衍会员
·
AI 创作全网分发
墨衍智能分发
本文由作者通过墨衍一键同步至各平台
1分发平台
1.3k累计阅读
1.4k平均单平台
已同步平台
优快云
微信公众号
微博
知乎
掘金
百家号
博客园
抖音
小红书
你的文章也可以这样分发
墨衍会员 ¥399起/年,写一次发全网
我也要 →
码龄26年
第一篇:优美的程序需要优美的子程序
LinuxAid.com.cn axing| 我见过那种一个main函数写了1000多行的程序员,而且为数还不在少数。难道说他们不懂得用函数吗?不见得,但可以肯定的一点是他们没有编写优美代码的意识。那么我们为什么需要子程序呢?我们来看看STEVE McCONNELL是怎么说的。 |
《生成式AI引发的“认知污染”:社交媒体虚假信息的治理困局》
认知污染与生成式AI“认知污染”指信息环境被大量虚假、误导性内容所充斥,干扰公众对事实的认知判断。在社交媒体时代,虚假信息的生产和传播门槛大幅降低,呈现出工业化、规模化的趋势。近年兴起的生成式人工智能(Generative AI)技术,如大型语言模型(LLM)和深度伪造(deepfake),更是加剧了这一问题。AI可以自动生成看似真实的文本、图像、音视频,将虚假信息批量生产并快速散播到社交平台上。
使用子程序的优点
子程序的作用: 1 降低复杂性:使用子程序的最首要原因是为了降低程序的复杂性,可以使用子程序来隐含 信息,从而使你不必再考虑这些信息。 2 避免代码段重复:无可置疑,生成子程序最普遍的原因是为了避免代码段重复。 3 限制改动带来的影响:由于在独立区域进行改动,因此,由此带来的影响也只限于一个或最多几个区域中。要把最可能改动的区域设计成最容易改动的区域。最可能被改动的区域包括:硬件依赖部
Python学习24:中国古代数学问题——物不知数(Python123)
意思是说有一些物品,不知道有多少个,3个3个数的话,还多出2个;5个5个数则多出3个;7个7个数也会多出2个。题目为:今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?现假设物品总数不超过n (n
为什么要使用子程序?
使用子程序,总得来说有两大好处。 第一个好处是,降低代码复杂度。 软件最应该考虑的问题是管理复杂度(5.2 key design concepts)。 使用子程序可以隐藏具体的实现细节,通过一个简单明了的函数名字即可知道函数的功能,有助于开发者集中精力解决主要问题。本质上是降低同一时间需要考虑的问题的复杂度。 第二个好处是减少冗余代码,简化修改难度。 详情请见《代
Node.js内存溢出?一招搞定JavaScript heap out of memory错误(附永久配置方案)
本文深入解析Node.js内存溢出问题,提供从临时调整到永久配置的完整解决方案。重点介绍通过设置`max-old-space-size`参数和`NODE_OPTIONS`环境变量来扩展JavaScript堆内存限制,适用于Webpack、Vite等构建工具的大型项目场景,并涵盖Windows、Linux/macOS多平台配置方法。
CAN开发笔记(TI开发者推荐笔记)
查阅了很多资料没有查阅到关于5728芯片CAN模块寄存器的介绍,希望通过本文能给做CAN驱动的后来者有一点参考意义,我也是刚接触CAN模块有些不足请各位多多指正。 由下图所示是CAN模块初始化流程图 首先要开始初始化,设置CAN位时间,然后配置message objects, 完成初始化。 1.开始初始化 首先要控制DCAN_CTL 寄存器,该寄存器是CAN模块的总控制器,很多配置通过...
创建子程序的好处
小的子程序有许多优点,其一能够提高其可读性。例如以下代码在程序中的十多个地方出现: //伪代码 某种计算 points = deviceUnits * (POINTS_PER_INCH / DeviceUnitsPerInch() ); 这样我们可以重构以下,把其变成一个函数: Point DeviceUnitToPoints(const int deviceUnits) { Point po...
GD32F4系列Keil工程模板搭建全攻略(手把手教学版)
本文提供了一份详细的GD32F4系列Keil工程模板搭建指南。从软件安装、固件库获取、目录规划,到Keil工程创建、文件添加、关键配置(如编译器选项、头文件路径、时钟设置),再到代码定制与常见问题排查,手把手教你构建一个标准化、模块化的开发基础。文章重点介绍了如何利用官方固件库和代码模板,快速建立稳定高效的开发环境,适用于GD32F407等Cortex-M4内核芯片的入门与项目开发。
【转贴】第一篇:优美的程序需要优美的子程序
我见过那种一个main函数写了1000多行的程序员,而且为数还不在少数。难道说他们不懂得用函数吗?不见得,但可以肯定的一点是他们没有编写优美代码的意识。那么我们为什么需要子程序呢?我们来看看STEVE McCONNELL是怎么说的。 以下是关于为什么要生成于程序的一些合理原因,其中有些原因之间可能有互相重叠的地方。 降低复杂性。使用子程序的最首要原因是为了降低程序的复杂性,可以使用子程序来隐含信息
PX4控制云台的两种方式 方法适用控舵机 控灯
云台控制插件是mount_control,运行mavros启动文件的时候需要注意该插件是否正常启动,需要在px4_pluginlists.yaml配置文件中将mount_control从blacklisted中移除后才可启动。后会默认占用AUX端口1-4,此时通过AUX通道1-4输出PWM控制信号至云台,对应的RC通道映射功能失效。控制信号输入为RC 则接收机必须连接对应的通道至AUX辅助通道,并通过MNT_MAN_*参数设置pitch/roll/yaw对应的AUX通道。配置为AUTO或MAVLINK,
46、搭建与配置FTP服务器全攻略
本文详细介绍了如何搭建与配置FTP服务器,以vsftpd为例,涵盖了FTP基础概念、安装步骤、服务控制、安全设置、用户权限管理、文件上传支持、互联网共享配置、客户端使用以及常见问题解决等内容。旨在帮助用户高效、安全地实现文件共享与管理。
使用子程序的原因(第五章)
1.避免重复(比如NBS中工厂的抽象) 2.限制改动的影响(封装需求变化) 3.集中控制,集中修改 4.隐含数据结构(比如具体算法的实现) 5.建立代码资源库 6.提高可读性 7.提高可移植性 8.分隔复杂操作 ...
【数据集处理】中国地面气候资料日值数据集(V3.0)(含MATLAB全代码)
简介数据集数据处理
读后感--欣赏优美的代码
欣赏优美的代码:http://www.vchelp.net/itbookreview/view_paper.asp?paper_id=230这个学期刚刚学过“软件工程”。但是,课堂上老师讲了很多软件开发过程的概念。其中,有程序设计和面向对象的内容没有讲解。看过这个篇文章后,再次感到大学教育的悲哀。怎么能忽略这么重要的东西呢!?其内容从软件工程的角度来理解编程中的一些误区。 重点是:子程序
android 蓝牙通讯demo
android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。
NTscan扫描工具
ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。
如何写出高效优美的C语言代码
如何写出高效优美的C语言代码 转自:http://hi.baidu.com/cusney/blog/item/ac3259ec2b0c852063d09f72.html2009/02/16 10:02程序能跑起来并不见得你的代码就是很好的c代码了,衡量代码的好坏应该从以下几个方面来看1,代码稳定,没有隐患。2,执行效率高。3,可读性高。4,便于移植。
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
uibot在子程序执行js失败_协程概念,原理(c++和node.js实现)
什么是协程wikipedia 的定义:协程是一个无优先级的子程序调度组件,允许子程序在特定的地方挂起恢复。线程包含于进程,协程包含于线程。只要内存足够,一个线程中可以有任意多个协程,但某一时刻只能有一个协程在运行,多个协程分享该线程分配到的计算机资源。为什么需要协程简单引入就实际使用理解来讲,协程允许我们写同步代码的逻辑,却做着异步的事,避免了回调嵌套,使得代码逻辑清晰。code like thi...
Devexpress16.2.6破解工具
Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具
**探索多维度数据的优美路径:B-Spline插值库——Bspline-Fortran**
探索多维度数据的优美路径:B-Spline插值库——Bspline-Fortran 去发现同类优质开源项目:https://gitcode.com/ 在现代科学计算与数据分析领域,精准且高效地处理高维数据是每个研究者和工程师面临的挑战。今天,我们来深入探索一款旨在解决这一挑战的开源利器——Bspline-Fortran。这是一款强大的、使用现代Fortran编写的多维度B-Spline插值工具包,...
汇编实验16 编写包含多个功能子程序的中断例程——浅谈直接地址表
汇编实验16 编写包含多个功能子程序的中断例程——浅谈直接地址表 这是王爽《汇编语言(第三版)》的第16个实验,本章的内容就是介绍了一种编程技巧——直接定址表,可以认为是一种以空间换时间的编程策略,相对于算法竞赛中的“打表法”,可以使程序变得更加简洁优美,避免过于繁琐的分支结构。 好吧,好话就说到这里。本次实验如果要照搬以前的套路,肯定是失败的!具体来说,如果要照搬书中代码(稍微做些修改)...
3次B样条优化:Matlab程序的强力增效剂
B样条曲线是一种在计算机辅助设计和图形学中广泛使用的曲线表示方法。3次B样条曲线具有良好的光滑性和局部控制性。简单来说,它通过一系列控制点来生成一条平滑的曲线,在处理数据拟合、图形绘制等方面有着出色的表现。
如何写出高效优美的C语言代码?
程序能跑起来并不见得你的代码就是很好的c代码了,衡量代码的好坏应该从以下几个方面来看 1,代码稳定,没有隐患。 2,执行效率高。 3,可读性高。 4,便于移植。 下面发一些我在网上看到的技巧和自己的一些经验来和大家分享; 1、如果可以的话少用库函数,便于不同的mcu和编译器间的移植 2、选择合适的算法和数据结构 应该熟悉算法语言,知道各种算法的优缺点,具体资料请参见相应的参考资料,有很多计算机书籍上都有介绍。将比较慢的顺序查找法用较快的二分查找或乱 序查找法代替,插入排序或冒泡排序法用快速排序、合并排序或根
论如何写出高效优美的C语言代码
程序能跑起来并不见得你的代码就是很好的c代码了,衡量代码的好坏应该从以下几个方面来看 1,代码稳定,没有隐患。 2,执行效率高。 3,可读性高。 4,便于移植。 下面发一些我在网上看到的技巧和自己的一些经验来和大家分享,更多C语言开发技巧观看iOS开发培训教学视频-C语言百科全书 1、如果可以的话少用库函数,便于不同的mcu和编译器间的移植
3668

被折叠的 条评论
为什么被折叠?



