Tag Info

Hot answers tagged

29

Add the title to the parameters of the printf: char* title1; switch(gender1){ case 'M': title1 = "Sir"; break; case 'W': title1 = "Madam"; break; case ...etc. } char* title2; switch(gender2){ case 'M': title2 = "Sir"; break; case 'W': title2 = "Madam"; break; case ...etc. ...


21

In the 2nd approach you will never have a half-initialised Foo. Putting all the construction in one place seems a more sensible, and obvious place. But... the 1st way isn't so bad, and is often used in many areas (there's even a discussion of the best way to dependency-inject, either property-injection like your 1st way, or constructor injection like the ...


17

Both approaches bundle the initialization code into a single function call. So far, so good. However, there are two issues with the second approach: The second one does not actually construct the resulting object, it initializes another object on the stack, which is then copied over to the final object. This is why I would see the second approach as ...


17

Radical solution: Let the user specify their own title (from a predefined list that you provide). Your solution (as viewed through English eyes) only appears to cater for Lords ("Sir") and ladies; most men would be addressed as "Mr", most women as either "Miss", "Mrs" or "Ms", depending on their marital status and personal opinions. Then there's a whole ...


10

The only time to use a single-line if statement is when you have a lot of them and you can format your code to make it very clear what is happening. Anything else is not clear and will lead to errors. eg. if (x) do_stuff(); is terrible. When mixed in with other code, it does not clearly show the conditional statement, and some people will confuse the ...


8

Titles really belong in the database, but you stated you have no control over this. You have not specified a language tag but the syntax is in the C family, so this will be pseudocode that is almost C++: map<string, string> titles; titles.emplace("M", "Sir"); titles.emplace("F", "Madam"); cout << "Dear " << titles[gender1] << " " ...


5

I always prefer the version with braces. If I don't always use the braces, then I may forget to put them when there is more than one statement in the block. IDEs used by other team members can reformat the code automatically to move the statement to the new line, thus making it much more error prone in the future. Merging the code across the branches is ...


3

For this particular problem, it's not terrible. What you're doing is basically scripting an import process, which is a fairly straight-forward imperative problem. Java maybe isn't the best tool for that job, but it's fine. An experienced programmer might create an intermediary data structure to represent the data going into the database to help should that ...


3

The point of object-orientation is not to count the user-defined classes and judge via "more == better". Instantiating an object is useful if you really do have multiple things in your problem field that have identical behaviour but a distinct identity. You saw how that works with streams: one reads from one source, the next one from another; reading and ...


3

If putting the functions chronologically improves the readability of the code, it means that you are using functions wrong. The primary purpose of dividing the code to function is to reduce the amount of context you need to hold in your mind at any given time. (functions have other usages when you use recursion or higher-order functions, but that's clearly ...


3

ratchet freak's answer is quite a good idea if the sentences are all the same pattern, but with two insets, one each only dependent on gender1 respective gender2. Phil W.'s answer is probably the most flexible answer as it allows explicit control over the greeting, though he's quite correct it's a radical change. You might not have the data in that form. ...


3

In general, in Python I favor using functions over subclassing internal classes. In the end, if you write your String -> dict method, you end up working with a dict, pure as the driven snow. Just put your functions in a sensible module and import them only where needed; this will also encourage you not to import the functions everywhere, which will help ...


2

There are two speeds in software: the time it takes to write/read/debug the code; and the time it takes to execute the code. If you can convince me (and your code reviewers) that the hashmap function is indeed slower than the if/then/else (after refactoring to make a static hashmap) AND you can convince me/reviewers that its called enough times to make an ...


2

There’s no such thing as information overload, only bad design. — Edward Tufte It's a general rule in graphic design to leave out unnecessary elements and ornamentation to reduce noise. Fewer visual elements on the screen means less work for our brains to parse the actual useful information. let foo = 1 vs. let /* variable */ foo = 1; // EOL ...


2

This sort of thing can always be done with an IDE. I am not familiar with an IDE that supports this exact formatting, but I know that most modern IDEs have highly customizable settings for formatting code. As a Java programmer using Eclipse, I have it set so that I can type Ctrl+A(select all) Ctrl+Shift+F(code-format) and it will prettify all my Java code ...


2

If your language allows you to do it, you can write switch(gender1+gender2) { case "MM": print "Dear Sirs"; break; case "MF": case "FM": print "Dear Sir and Madam"; break; ... It's not necessarily better than your version, since there's still duplication, but it does avoid the nested switch.


1

You can't make a Java program without objects. Everything you instantiate is an object. The static methods you call are called on instances of the Class object of their class. But yes, it's generally a bad idea to use a single big class with just static methods. It gets terribly unwieldy really quickly. Rather than avoid the issue of learning object ...


1

One argument in favor of the "output-parameter" style is that it allows the function to return an error code. struct MyStruct { int x; char *y; // ... }; int MyStruct_init(struct MyStruct *out) { // ... char *c = malloc(n); if (!c) { return -1; } out->y = c; return 0; // Success! } Considering some set of ...


1

Depending upon the contents of the structure and the particular compiler being used, either approach could be faster. A typical pattern is that structures meeting certain criteria can get returned in registers; for functions returning other structure types the caller is required to allocate space for the temporary structure somewhere (typically on the ...


1

You are declaring a local scope function in this case. This means it will only be available withing your scope which is very limited. If you are not planing to reuse it, I wouldn't declare a function all together. if you are planing to reuse it, I would move it outside the callback scope into a separate module/class depending on what the responsibility of ...


1

I would propose an alternative answer. I prefer single liners when the condition inside is really a single line and is relatively isolated from the rest of the conditions. One great example is: public void DoSomething(int something) { // Notice how easily we can state in one line that we should exit the method if our int is 0. if(something == 0) ...


1

"over engineering" means improving a solution when the cost of improving it is higher than the benefits of improving it. And "under engineering" means not improving a solution, when the benefits of improving it would outweigh the cost. Now guess what "right engineering" means... Since a "simple" solution was mentioned, and was somehow correlated to ...



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