Hot answers tagged

7

Data-Structure versus Object I think the question you are asking is: Should I provide a data-structure and expect all of its users to follow the rules on updating the data-structure, or should I provide a set of interactions which observe all of the rules on updating a data-structure and hide the data-structure itself? Congratulations; you have just ...


3

There are four obvious grounds for assessing a language feature: aesthetics, implementation cost, benefit, and interaction with other language features. I'll skip aesthetics because it's subjective, and implementation cost because I'm not really qualified. Maybe someone can find a link to the Eric Lippert blog that was mentioned in comments. Interaction with ...


3

A do...while is only different from a while... in one respect - in a do...while the body is executed at least once, whereas in a while...do it may not be executed at all. Your do + while statement can easily be achieved within the existing C syntax as follows: do { body1; if ( !conditions ) break; body2; } while true; The C language is already ...


3

If you create a new object, return it. public int[] square(int a[], int size) { int b[] = malloc(size * sizeof(int)); for (int i=0; i<size; i++) b[i] = a[i] * 2; return b; } If you don't create an object, return void. public void square(int a[], int size) { for (int i=0; i<size; i++) a[i] = a[i] * 2; } Note that ...


3

Generalizing the question, should new code depend on existing code? All new code depends on existing code. If your new code adds 1 and 2 together it depends on existing add code. Even if it’s assembly you end up depending on micro code. The big difference is when you depend on unstable code. Your code is only as stable as the code it depends on. If ...


1

According to Uncle Bob and others: A function should do one thing only and do it well Accordingly, if printError() only prints error and prints errors well, there is no benefit in reinventing the wheel and reimplement the same again in another context. Moreover, if doSomething() would by itself do something and print errors, it would no longer do one thing....


1

The usual explanation is just "someone thought it was a good idea". Another explanation is "we've always done it this way". Another good explanation is: "I didn't want to change existing code, and I didn't want to introduce different code, so I copied the existing pattern". If someone wrote "char** p = malloc(0);" in ...


1

This is a reasonable construct. My preference with looping is either: when we know the count, counted loop, e.g. on count of N: for (int i = 0; i < N; i++ ) ... -or- when we don't know the count, then "infinite loop": for (;;) { // or while (1) { // unconditional stuff that occurs (before) each iteration if ( ...


Only top voted, non community-wiki answers of a minimum length are eligible