From: Yi, EungJun Date: Sun, 13 Jul 2014 09:51:57 GMT Subject: Re: [PATCH v3] http: Add Accept-Language header if possible Message-ID: In-Reply-To: 2014-07-13 13:26 GMT+09:00 Eric Sunshine : >> + /* Decide the precision for q-factor on number of preferred languages. */ >> + if (num_langs + 1 > 100) { /* +1 is for '*' */ >> + q_precision = 0.001; >> + q_format = "; q=%.3f"; >> + } else if (num_langs + 1 > 10) { /* +1 is for '*' */ >> + q_precision = 0.01; >> + q_format = "; q=%.2f"; >> + } > > It might make sense to have a final 'else' here which sets these > variables for the 0.1 case so that the reader of the code doesn't have > to refer back to the top of the function to figure out what is going > on. > > } else { > q_precision = 0.1; > q_format = "; q=%.1f"; > } > > Better yet, would it be possible to compute these values rather than > having to set them manually via a cascading if-chain? I think it is possible like this: num_langs += 1; /* for '*' */ decimal_places = 1 + (num_langs > 10) + (num_langs > 100); snprintf(q_format, sizeof(q_format), "; q=%%.%df", decimal_places); for (q_precision = 1.0; decimal_places-- > 0;) q_precision /= 10; Does this one look better than before? I'm not sure which one is better. ps. The last line can be simpler by using pow() but I'm not sure it is okay to include math.h.