最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語(yǔ)言容易被忽視的函數(shù)設(shè)計(jì)原則基礎(chǔ)

 更新時(shí)間:2022年04月23日 11:20:53   作者:清風(fēng)自在 流水潺潺  
C語(yǔ)言的設(shè)計(jì)目標(biāo)是提供一種能以簡(jiǎn)易的方式編譯、處理低級(jí)存儲(chǔ)器、產(chǎn)生少量的機(jī)器碼以及不需要任何運(yùn)行環(huán)境支持便能運(yùn)行的編程語(yǔ)言.那么C語(yǔ)言函數(shù)設(shè)計(jì)的一般原則和技巧都是怎樣的呢,下面帶你了解

一、函數(shù)設(shè)計(jì)原則

  • 函數(shù)從意義上應(yīng)該是一個(gè)獨(dú)立的功能模塊
  • 函數(shù)名要在一定程度上反映函數(shù)的功能
  • 函數(shù)參數(shù)名要能夠體現(xiàn)參數(shù)的意義
  • 盡量避免在函數(shù)中使用全局變量
  • 當(dāng)函數(shù)參數(shù)不應(yīng)該在函數(shù)體內(nèi)部被修改時(shí),應(yīng)加上 const 聲明
  • 如果參數(shù)是指針,且僅作輸入?yún)?shù),則應(yīng)加上 const 聲明,如下:

  • 不能省略返回值的類型
  • 如果函數(shù)沒(méi)有返回值,那么應(yīng)聲明為 void
  • 類型對(duì)參數(shù)進(jìn)行有效性檢查
  • 對(duì)于指針參數(shù)的檢查尤為重要
  • 不要返回指向“棧內(nèi)存”的指針
  • 棧內(nèi)存在函數(shù)體結(jié)束時(shí)被自動(dòng)釋放
  • 函數(shù)體的規(guī)模要小,盡量控制在 80 行代碼之內(nèi)
  • 相同的輸入對(duì)應(yīng)相同的輸出,避免函數(shù)帶有“記憶”功能
  • 避免函數(shù)有過(guò)多的參數(shù),參數(shù)個(gè)數(shù)盡量控制在 4 個(gè)以內(nèi)
  • 有時(shí)候函數(shù)不需要返回值,但為了增加靈活性,如支持鏈?zhǔn)奖磉_(dá),可以附加返回值

  • 函數(shù)名與返回值類型在語(yǔ)義上不可沖突

下面來(lái)欣賞一份優(yōu)秀的代碼:

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Kevin Cornell (Rational Software Corporation)
 *******************************************************************************/
/* Eclipse Launcher Utility Methods */
#include "eclipseOS.h"
#include "eclipseCommon.h"
#include "eclipseUtil.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#include <strings.h>
#endif
#define MAX_LINE_LENGTH 256
/* Is the given VM J9 */
int isJ9VM( _TCHAR* vm )
{
	_TCHAR * ch = NULL, *ch2 = NULL;
	int res = 0;
	if (vm == NULL)
		return 0;
	ch = lastDirSeparator( vm );
	if (isVMLibrary(vm)) {
		/* a library, call it j9 if the parent dir is j9vm */
		if(ch == NULL)
			return 0;
		ch[0] = 0;
		ch2 = lastDirSeparator(vm);
		if(ch2 != NULL) {
			res = (_tcsicmp(ch2 + 1, _T_ECLIPSE("j9vm")) == 0);
		}
		ch[0] = dirSeparator;
		return res;
	} else {
		if (ch == NULL)
		    ch = vm;
		else
		    ch++;
		return (_tcsicmp( ch, _T_ECLIPSE("j9") ) == 0);
	}
}
int checkProvidedVMType( _TCHAR* vm ) 
{
	_TCHAR* ch = NULL;
	struct _stat stats;
	if (vm == NULL) return VM_NOTHING;
	if (_tstat(vm, &stats) == 0 && (stats.st_mode & S_IFDIR) != 0) {
		/* directory */
		return VM_DIRECTORY;
	}
	ch = _tcsrchr( vm, _T_ECLIPSE('.') );
	if(ch == NULL)
		return VM_OTHER;
#ifdef _WIN32
	if (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0)
#else
	if ((_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0))
#endif
	{
		return VM_LIBRARY;
	}
	if (_tcsicmp(ch, _T_ECLIPSE(".ee")) == 0)
		return VM_EE_PROPS;
	return VM_OTHER;
}
/*
 * pathList is a pathSeparator separated list of paths, run each through
 * checkPath and recombine the results.
 * New memory is always allocated for the result
 */
_TCHAR * checkPathList( _TCHAR* pathList, _TCHAR* programDir, int reverseOrder) {
	_TCHAR * c1, *c2;
	_TCHAR * checked, *result;
	size_t checkedLength = 0, resultLength = 0;
	size_t bufferLength = _tcslen(pathList);
	result = malloc(bufferLength * sizeof(_TCHAR));
	c1 = pathList;
    while (c1 != NULL && *c1 != _T_ECLIPSE('\0'))
    {
    	c2 = _tcschr(c1, pathSeparator);
		if (c2 != NULL)
			*c2 = 0;
		checked = checkPath(c1, programDir, reverseOrder);
		checkedLength = _tcslen(checked);
		if (resultLength + checkedLength + 1> bufferLength) {
			bufferLength += checkedLength + 1;
			result = realloc(result, bufferLength * sizeof(_TCHAR));
		}
		if(resultLength > 0) {
			result[resultLength++] = pathSeparator;
			result[resultLength] = _T_ECLIPSE('\0');
		}
		_tcscpy(result + resultLength, checked);
		resultLength += checkedLength;
		if(checked != c1)
			free(checked);
		if(c2 != NULL)
			*(c2++) = pathSeparator;
		c1 = c2;
	}
    return result;
}
_TCHAR * concatStrings(_TCHAR**strs) {
	return concatPaths(strs, 0);
}
_TCHAR * concatPaths(_TCHAR** strs, _TCHAR separator) {
	_TCHAR separatorString[] = { separator, 0 };
	_TCHAR * result;
	int i = -1;
	size_t length = 0;
	/* first count how large a buffer we need */
	while (strs[++i] != NULL) {
		length += _tcslen(strs[i]) + (separator != 0 ? 1 : 0);
	}
	result = malloc((length + 1) * sizeof(_TCHAR));
	result[0] = 0;
	i = -1;
	while (strs[++i] != NULL) {
		result = _tcscat(result, strs[i]);
		if (separator != 0)
			result = _tcscat(result, separatorString);
	}
	return result;
}
/*
 * buffer contains a pathSeparator separated list of paths, check 
 * that it contains all the paths given.  Each path is expected to be
 * terminated with a pathSeparator character.
 */
int containsPaths(_TCHAR * str, _TCHAR** paths) {
	_TCHAR * buffer;
	_TCHAR * c;
	int i;
	/* terminate the string with a pathSeparator */
	buffer = malloc((_tcslen(str) + 2) * sizeof(_TCHAR));
	_stprintf(buffer, _T_ECLIPSE("%s%c"), str, pathSeparator);
	for (i = 0; paths[i] != NULL; i++) {
		c = _tcsstr(buffer, paths[i]);
		if ( c == NULL || !(c == buffer || *(c - 1) == pathSeparator))
		{
			/* entry not found */
			free(buffer);
			return 0;
		}
	}
	free(buffer);
	return 1;
}
int isVMLibrary( _TCHAR* vm )
{
	_TCHAR *ch = NULL;
	if (vm == NULL) return 0;
	ch = _tcsrchr( vm, '.' );
	if(ch == NULL)
		return 0;
#ifdef _WIN32
	return (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0);
#else
	return (_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0);
#endif
}
#ifdef AIX
#include <sys/types.h>
#include <time.h>
/* Return the JVM version in the format x.x.x 
 */
char* getVMVersion( char *vmPath )
{
    char   cmd[MAX_LINE_LENGTH];
    char   lineString[MAX_LINE_LENGTH];
    char*  firstChar;
    char   fileName[MAX_LINE_LENGTH];
    time_t curTime;
    FILE*  fp;
    int    numChars = 0;
    char*  version  = NULL;
	/* Define a unique filename for the java output. */
    (void) time(&curTime);
    (void) sprintf(fileName, "/tmp/tmp%ld.txt", curTime);
    /* Write java -version output to a temp file */
    (void) sprintf(cmd,"%s -version 2> %s", vmPath, fileName);
    (void) system(cmd); 
    fp = fopen(fileName, "r");
    if (fp != NULL)
    {
    	/* Read java -version output from a temp file */
    	if (fgets(lineString, MAX_LINE_LENGTH, fp) == NULL)
    		lineString[0] = '\0';
    	fclose(fp);
    	unlink(fileName);
    	/* Extract version number */
    	firstChar = (char *) (strchr(lineString, '"') + 1);
    	if (firstChar != NULL)
    		numChars = (int)  (strrchr(lineString, '"') - firstChar);
    	/* Allocate a buffer and copy the version string into it. */
    	if (numChars > 0)
    	{
    		version = malloc( numChars + 1 );
    		strncpy(version, firstChar, numChars);
			version[numChars] = '\0';
		}
	}  
    return version;
}
/* Compare JVM Versions of the form "x.x.x..."
 *     
 *    Returns -1 if ver1 < ver2
 *    Returns  0 if ver1 = ver2 
 *    Returns  1 if ver1 > ver2
 */     
int versionCmp(char *ver1, char *ver2)
{
    char*  dot1;
    char*  dot2;
    int    num1;
    int    num2;
    dot1 = strchr(ver1, '.');
    dot2 = strchr(ver2, '.');
    num1 = atoi(ver1);
    num2 = atoi(ver2);
    if (num1 > num2)
    	return 1;
	if (num1 < num2)
		return -1;
	if (dot1 && !dot2)   /* x.y > x */
        return 1;
    if (!dot1 && dot2)   /* x < x.y */
        return -1;
    if (!dot1 && !dot2)  /* x == x */
        return 0;
    return versionCmp((char*)(dot1 + 1), (char*)(dot2 + 1) );
}
#endif /* AIX */

二、總結(jié)

  • C 語(yǔ)言的學(xué)習(xí)需要勤思考勤動(dòng)手才能得到
  • 提高難點(diǎn)部分為指針的學(xué)習(xí)
  • 指針的本質(zhì),指針的運(yùn)算,指針和數(shù)組
  • 學(xué)習(xí)過(guò)程可以采用各個(gè)擊破的方法
  • 在一個(gè)特定的時(shí)間段只重點(diǎn)學(xué)習(xí)和練習(xí)某個(gè)主題
  • 在熟練掌握C語(yǔ)言的各個(gè)特性后再進(jìn)行項(xiàng)目練習(xí)

至此,C 語(yǔ)言進(jìn)階剖析學(xué)完,完結(jié)撒花。

但是,以后路還很遠(yuǎn),還需要多加總結(jié),才能真正掌握 C 語(yǔ)言。

到此這篇關(guān)于C語(yǔ)言容易被忽視的函數(shù)設(shè)計(jì)原則基礎(chǔ)的文章就介紹到這了,更多相關(guān)C語(yǔ)言函數(shù)設(shè)計(jì)原則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++11的函數(shù)包裝器std::function使用示例

    C++11的函數(shù)包裝器std::function使用示例

    C++11引入的std::function是最常用的函數(shù)包裝器,它可以存儲(chǔ)任何可調(diào)用對(duì)象并提供統(tǒng)一的調(diào)用接口,以下是關(guān)于函數(shù)包裝器的詳細(xì)講解,包括它的基本用法、特點(diǎn)、限制、以及與其他相關(guān)機(jī)制的對(duì)比
    2024-12-12
  • C語(yǔ)言中的數(shù)據(jù)整除判斷問(wèn)題

    C語(yǔ)言中的數(shù)據(jù)整除判斷問(wèn)題

    這篇文章主要介紹了C語(yǔ)言中的數(shù)據(jù)整除判斷問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 基于C++浮點(diǎn)數(shù)(float、double)類型數(shù)據(jù)比較與轉(zhuǎn)換的詳解

    基于C++浮點(diǎn)數(shù)(float、double)類型數(shù)據(jù)比較與轉(zhuǎn)換的詳解

    本篇文章是對(duì)C++中浮點(diǎn)數(shù)(float、double)類型數(shù)據(jù)比較與轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • c/c++小游戲源代碼

    c/c++小游戲源代碼

    這篇文章主要介紹了c/c++小游戲源代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 淺談C++標(biāo)準(zhǔn)庫(kù)

    淺談C++標(biāo)準(zhǔn)庫(kù)

    C++標(biāo)準(zhǔn)庫(kù)是一組C++模板類,提供了通用的編程數(shù)據(jù)結(jié)構(gòu)和函數(shù),本文簡(jiǎn)單講解C++標(biāo)準(zhǔn)庫(kù)包含的一些內(nèi)容,可能會(huì)對(duì)小伙伴的學(xué)習(xí)或工作有一定的幫助,大家一起來(lái)看看吧
    2021-08-08
  • C++中const應(yīng)放在類型前還是后

    C++中const應(yīng)放在類型前還是后

    之前遇到小伙伴問(wèn)C++中const加在類型名前和變量名前的區(qū)別,今天給大家簡(jiǎn)單分析下。
    2016-05-05
  • C語(yǔ)言讀寫(xiě)配置文件的方法

    C語(yǔ)言讀寫(xiě)配置文件的方法

    這篇文章主要介紹了C語(yǔ)言讀寫(xiě)配置文件的方法,包括C語(yǔ)言讀寫(xiě)ini配置文件所涉及的文件讀寫(xiě)技巧,以及完整的源文件及頭文件實(shí)現(xiàn)方法,需要的朋友可以參考下
    2015-07-07
  • C語(yǔ)言實(shí)現(xiàn)父進(jìn)程主動(dòng)終止子進(jìn)程的方法總結(jié)

    C語(yǔ)言實(shí)現(xiàn)父進(jìn)程主動(dòng)終止子進(jìn)程的方法總結(jié)

    一般的情況,子進(jìn)程自己運(yùn)行完后,執(zhí)行exit 或者return 后,父進(jìn)程wait.  waitpid收回子進(jìn)程,但子進(jìn)程是一個(gè)循環(huán)等待狀態(tài)不主動(dòng)退出,父進(jìn)程可以采用文中介紹的幾種方法,需要的朋友可以參考下
    2023-10-10
  • 詳解C++中十六進(jìn)制字符串轉(zhuǎn)數(shù)字(數(shù)值)

    詳解C++中十六進(jìn)制字符串轉(zhuǎn)數(shù)字(數(shù)值)

    這篇文章主要介紹了詳解C++中十六進(jìn)制字符串轉(zhuǎn)數(shù)字(數(shù)值)的相關(guān)資料,這里提供兩種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-08-08

最新評(píng)論

临夏县| 华容县| 夏邑县| 赫章县| 丰顺县| 泸定县| 吉水县| 广河县| 瓦房店市| 东乌珠穆沁旗| 仙桃市| 大港区| 莱州市| 隆昌县| 长阳| 周口市| 玉树县| 婺源县| 威远县| 祁连县| 安徽省| 惠来县| 洛隆县| 黄骅市| 马山县| 和平县| 鹤峰县| 宁远县| 昭平县| 思茅市| 个旧市| 林周县| 会东县| 蓬溪县| 辰溪县| 长垣县| 进贤县| 固阳县| 营口市| 瑞安市| 永吉县|