coding-style

Why is it assumed that send may return with less than requested data transmitted on a blocking socket?

给你一囗甜甜゛ 提交于 2019-12-17 09:36:06
问题 The standard method to send data on a stream socket has always been to call send with a chunk of data to write, check the return value to see if all data was sent and then keep calling send again until the whole message has been accepted. For example this is a simple example of a common scheme: int send_all(int sock, unsigned char *buffer, int len) { int nsent; while(len > 0) { nsent = send(sock, buffer, len, 0); if(nsent == -1) // error return -1; buffer += nsent; len -= nsent; } return 0; /

Boiler plate code replacement - is there anything bad about this code?

假如想象 提交于 2019-12-17 08:54:14
问题 I've recently created these two (unrelated) methods to replace lots of boiler-plate code in my winforms application. As far as I can tell, they work ok, but I need some reassurance/advice on whether there are some problems I might be missing. (from memory) static class SafeInvoker { //Utility to avoid boiler-plate InvokeRequired code //Usage: SafeInvoker.Invoke(myCtrl, () => myCtrl.Enabled = false); public static void Invoke(Control ctrl, Action cmd) { if (ctrl.InvokeRequired) ctrl

Infinite loops - top or bottom? [closed]

孤街醉人 提交于 2019-12-17 07:37:25
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . In the spirit of questions like Do your loops test at the top or bottom?: Which style do you use for an infinite loop, and why? while (true) { } do { } while (true); for (;;) { } label: ... goto label; 回答1: while(true) {} It seems to convey the meaning of the loop most

Should 'if' statement always have an 'else' clause? [closed]

人盡茶涼 提交于 2019-12-17 07:23:34
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . This may be a religious argument, but it has been debated ad-nauseum here at my work whether all IF statements should include an ELSE clause - even if the ELSE clause only contains a comment stating that it was 'intentionally left blank'. I have heard arguments for both sides

Where should I put @Transactional annotation: at an interface definition or at an implementing class?

寵の児 提交于 2019-12-17 07:05:51
问题 The question from the title in code: @Transactional (readonly = true) public interface FooService { void doSmth (); } public class FooServiceImpl implements FooService { ... } vs public interface FooService { void doSmth (); } @Transactional (readonly = true) public class FooServiceImpl implements FooService { ... } 回答1: From http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html The Spring team's recommendation is that you only annotate concrete classes with the

Where should I put @Transactional annotation: at an interface definition or at an implementing class?

ぃ、小莉子 提交于 2019-12-17 07:05:00
问题 The question from the title in code: @Transactional (readonly = true) public interface FooService { void doSmth (); } public class FooServiceImpl implements FooService { ... } vs public interface FooService { void doSmth (); } @Transactional (readonly = true) public class FooServiceImpl implements FooService { ... } 回答1: From http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html The Spring team's recommendation is that you only annotate concrete classes with the

Do you use attach() or call variables by name or slicing?

我的梦境 提交于 2019-12-17 06:36:46
问题 Many intro R books and guides start off with the practice of attaching a data.frame so that you can call the variables by name. I have always found it favorable to call variables with $ notation or square bracket slicing [,2] . That way I can use multiple data.frame s without confusing them and/or use iteration to successively call columns of interest. I noticed Google recently posted coding guidelines for R which included the line 1) attach: avoid using it How do people feel about this

Are protected members/fields really that bad?

我们两清 提交于 2019-12-17 06:34:45
问题 Now if you read the naming conventions in the MSDN for C# you will notice that it states that properties are always preferred over public and protected fields. I have even been told by some people that you should never use public or protected fields. Now I will agree I have yet to find a reason in which I need to have a public field but are protected fields really that bad? I can see it if you need to make sure that certain validation checks are performed when getting/setting the value

srand function is returning same values

只愿长相守 提交于 2019-12-17 06:18:09
问题 Hey guys take a look at this program. /* The craps game, KN king page 218 */ #include <stdio.h> #include <time.h> #include <stdbool.h> #include <stdlib.h> int roll_dice(void); bool play_game(void); int roll_dice(void) { int roll; getchar(); srand((unsigned) time(NULL)); roll = rand() % 13; if(roll == 0) roll = roll + 1; return roll; } bool play_game() { int sum = 0, wins = 0, loss = 0, point; sum = roll_dice(); printf("You rolled: %d", sum); if(sum == 7 || sum == 11) { printf("\nYou won!\n");

Generate random numbers with a given distribution

我与影子孤独终老i 提交于 2019-12-17 06:14:20
问题 Check out this question: Swift probability of random number being selected? The top answer suggests to use a switch statement, which does the job. However, if I have a very large number of cases to consider, the code looks very inelegant; I have a giant switch statement with very similar code in each case repeated over and over again. Is there a nicer, cleaner way to pick a random number with a certain probability when you have a large number of probabilities to consider? (like ~30) 回答1: This