|
有几类代码比较恶心:
全局变量很多条件判断太复杂if-else以及各种嵌套太多太深函数太长,大几百行,几千行……
接下来举几个例子给大家感受下。
1)全局变量很多,类似这种:
char hfile_name[MAXARGLEN+1], location[MAXARGLEN*2], *ppm = NULL,
*gif = NULL, tzfile[MAXARGLEN+1], units[MAXARGLEN+1],
units_abbrv[MAXARGLEN+1], tadjust_tzname[MAXARGLEN+1];
char next_ht_text[20], next_ht_date[20], next_lt_text[20],
next_lt_date[20], *geometry = NULL, loadunits[80];
char *fgrise_color_arg = NULL, *fgfall_color_arg = NULL,
*fgtext_color_arg = NULL, *fgmark_color_arg = NULL,
*fgmllw_color_arg = NULL, *fgmiddle_color_arg = NULL;
int Usetadjust=0, Itadjust = 0, tadjust = 0, utc = 0, list = 0, checkyear = 0, text = 0, skinny = 0,
now = 0, graphmode = 0, httimeoff = 0, lttimeoff = 0, tstep = 180,
middle = 0, mark = 0, mllw = 0, lines = 1, PPMWIDTH = 960,
PPMHEIGHT = 300, hinc = 0, tinc = 0, loctz = 0, iscurrent = 0,
curonly = 0, toplines = 0, hincmagic = 0, calendar = 0, banner = 0,
weekday = 0, hairy = 0, linegraph = 0, ps = 0, noampm = 0, uutc = 0,
have_offsets = 0, Ihttimeoff = 0, Ilttimeoff = 0, newload = 0, subproc=1,OnlyTCD=0, datemdy=0, mapZoom=0;
double llevelmult = 1.0, hlevelmult = 1.0, Illevelmult = 1.0, Ihlevelmult = 1.0;
time_t next_ht = 0, prev_ht = 0, next_ht_adj = 0,
prev_ht_adj = 0, faketime = 0, epoch = 0, mark_time_adj = 0;
double amplitude = 0.0, htleveloff = 0.0, ltleveloff = 0.0, DATUM, marklev,
absmax = 0.0, absmin = 0.0, fakedatum = 0.0, fakeamplitude = 0.0,
Ihtleveloff = 0.0, Iltleveloff = 0.0;
double next_ht_amplitude, next_lt_amplitude; //mgh+
int num_epochs = 0, first_year = 0;
unit known_units[NUMUNITS] = {{"feet", "ft", LENGTH, 0.3048},
{"meters", "m", LENGTH, 1.0},
{"knots", "kt", VELOCITY, 1.0},
{"knots^2", "kt^2", BOGUS, 1.0}};
/* Local variables */
static int year = 0, meridian = 0;
/* Tide-prediction storage -- cst == constituent */
static int num_csts = 0, num_nodes = 0;
static double *cst_speeds, **cst_epochs, **cst_nodes, *loc_amp, *loc_epoch,
*work;
这么多全局变量,看代码时,几乎一路都是懵逼的,要不断回来看变量的含义和初值。
2)条件判断太复杂,比如:
if ((sortnearest)
|| ( nItemIndex && (LB_ERR==(SendDlgItemMessage(hwnd, IDL_STATION, LB_SETCURSEL,
nItemIndex, 0L))))
|| (!nItemIndex && (LB_ERR==(SendDlgItemMessage(hwnd, IDL_STATION, LB_SELECTSTRING,
-1, (LPARAM) ((LPSTR) IDX_station_name))))) )
SendDlgItemMessage(hwnd, IDL_STATION, LB_SETCURSEL, 0, 0L);这种在判断条件里写各种各样的调用,小括号排长队,又各种折行,简直没法看。
3)嵌套层级太多,比如这种:
if (IndexFileIO(IFF_OPEN, 0)) {
while (IndexFileIO(IFF_READ, 0)) {
if ((index_line[0] == &#39;#&#39;) || (index_line[0] <= &#39; &#39;)); // Skip comment lines
else if (!have_index && !xref_start) {
if (!strncmp(index_line, &#34;XREF&#34;, 4))
xref_start = IndexFileIO(IFF_TELL, 0);
}
else if (!have_index && !strncmp(index_line, &#34;*END*&#34;, 5)) {
if (num_abv == 0) {
IndexFileIO(IFF_CLOSE, 0);
return(FALSE); // missing at least some data so no valid index
}
if (doing_xref++ == 0) { // First pass through, flag for second pass
IndexFileIO(IFF_SEEK, xref_start); // Position back to start of index
// Allocate memory for the array
if (NULL==(abbreviation_list=
(abbreviation_entry **)malloc((num_abv+1)*sizeof(abbreviation_entry *)))) {
IndexFileIO(IFF_CLOSE, 0);
noMemErr(&#34;Index&#34;);
return(FALSE);
}
else {
for (i=0; i<=num_abv; i++)
if (NULL==(abbreviation_list = (abbreviation_entry *)
malloc(sizeof(abbreviation_entry)))) { // If we can&#39;t allocate..
free_abbreviation_list();
IndexFileIO(IFF_CLOSE, 0);
noMemErr(&#34;Index&#34;);
return(FALSE);
}
abbreviation_list[num_abv]->type = 0; // Flag final entry
abbreviation_list[num_abv]->short_s = NULL;
abbreviation_list[num_abv]->long_s = NULL;
num_abv = 0;
}
}if-else,while,for,各种语句疯狂嵌套,层次又多又深,几乎没法看。
4)函数太长,我见过400多行的,700多行的,2000多行的函数。可能还有的小伙伴见过几万行的函数。这很让人奔溃。 |
|