Tag Info

New answers tagged

0

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 ...


9

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 ...


0

I don't see these necessarily as opposites. I think of over-engineering as providing options or provisions for capabilities that are not yet or not yet know to be necessary. For example, and method parameter that only has one value that is ever used; an abstract class that has only one subclass. These things add complexity to maintenance and cost time ...


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 ...


0

I call the two extremes described "under-engineering" and "over-engineering" They seem to refer to the sentence in the link above: In my experience there are two developer character type extremes: the ones that always seek and settle with the simplest solution, and the ones that seek the perfect solution, perfect in terms of efficiency, readability ...


7

When you're doing too much design to solve a basic problem, you're over-engineering. For example, when you're using an abstract factory pattern “just in case” in order to create a very simple object where the business logic is easy enough to fit in four-five LOC, you're over-thinking the design (and violating KISS and YAGNI). Note that it doesn't ...


2

Generally speaking, an Exception is suited for this and the best practice you should follow is: "If a method can't do it's job, it should throw an exception." Understandably, you may want execution flow to continue without exceptions. The .NET space handles this fairly well and provides a convention for methods that don't throw exceptions. Methods that ...


3

First of all this method is not unit testable. There is nothing in the code you quoted which prevents it from being covered by unit tests. The presence of try/catch is irrelevant: You can test the situation where there is no error and check whether the side effects of this method correspond to the expectations. You can also test the situation where ...


1

This is a natural progress of encapsulation, abstraction, and general code organization. As you extract pieces of code from a function into their own separate functions, you may notice that they share a lot of state that requires passing numerous parameters around. Promoting this state to fields of a class -- and said functions to methods of the class -- ...


2

Uncomfortably long argument lists and multiple return values can usually be addressed by grouping values into structured data types or encapsulating them in classes. If there are no clear groupings, hiding that by sweeping it into object state with other unrelated values is only furtherly detrimental. Methods (even methods intended to be private) should ...


3

Like Ixrec said - put the implementation in another file. But since you need a state that you don't want to make global - put that state in a class! OK, that came up a bit confusing. What I mean is to keep the class, but hide it from the user. The user will only see a global function that internally creates the class and call it's go method. Actually - ...


3

I do not think this practice has a special name, I would call it "encapsulation", "creating an abstraction by utilizing a class", "class design", or simply "OO programming". And it is definitely not an anti-pattern.


0

Put them in a separate module. Assuming your solution is not more bloated than needing you haven't got too many options. You've already split up the functions in different sub functions so the question is where you should put it: Put them in a module with only one "public" function. Put them in a class with only one "public" (static) function. Nest them ...


2

We'd have to ask the author of that style guide to know for sure, but I think the main reason I kind of agree with him is that the connection between struct and method is much looser in Go than other languages. In essence, when you write a method like this: func (m *MultiShape) area() float64 { ... } That's almost exactly the same thing as writing a ...


-1

This is from a perspective of JavaScript where this has actual keyword meaning to the compiler, but from my understanding, if they're okay with two-letter abbreviations for the object type, it should be easy enough to use that instead. The reason for the difference is that in a decently-large block of progressively deeper asynchronous code, it could be very ...


0

The PEP 8 Python style guide shows lists in the form: my_list = [ 1, 2, 3, 4, 5, 6, ] In the indentation section of the pep https://www.python.org/dev/peps/pep-0008/#indentation For this reason I would say style 1 (ensuring not to violate the line length rules).


0

Personally, I'm using style2. Because its very easy for adding new item to list. Also with style2 you can easly delete item with deleting row. But its difficult on style1.


0

Short and simple rule of thumb: IDs are used in DTOs. Object references are usually used in the Domain Logic/Business Logic and UI layer objects. That's the common architecture in larger, enterprisey enough projects. You'll have mappers that translate to and fro these two kinds of objects.


1

It's repetitive because it's redundant. The right way to "organize it" is by removing it. /** * This exception throws under situation1 ... */ public class SomeException extends Exception { "This"? I know that Javadoc refers to this class, and not some other one. "Exception"? I can very well see that this is an Exception. "Throws"? It goes without ...


2

Put your strings into a ResourceBundle, as though you were internationalizing the application. There are a number of tutorials out there for doing this sort of thing, but for the most part they boil down to exactly the problem you're describing: putting text into a common location, where you can later refer to it by an identifier of some kind. People often ...


2

I would perhaps agree with Wilding here, but then I am a bit of a noob at this sort of thing too. I would suggest that the built-in types are often mechanism objects. KeyValuePair for example, is part of the mechanism of a dictionary and hash tables, helping to ensure unique keys and they have properties that have meaningful names within the context of the ...


4

Domain Objects as ids create some complex/subtle problems: Serialization/Deserialization If you store objects as keys it will make serializing the object graph extremely complicated. You will get stackoverflow errors when doing a naive serialization to JSON or XML because of the recursion. You will then have to write a custom serializer that converts the ...


-1

The problem, in general with KR brace style is in code refactoring. When moving code around it is easy to miss that there are no braces around something, move it incorrectly (or move something under it thinking it is conditionally executed) and then either scratch your head when something no longer works, or be unfortunate and be in a code area not well ...


1

Yes, there are benefits to either way, and there's also a compromise. List<int>: Save memory Faster initialization of type User If your data comes from a relational database (SQL), you don't have to access two tables to get users, just the Users table List<Book>: Accessing a book is faster from user, the book has been preloaded into ...



Top 50 recent answers are included