Hot answers tagged

39

There are lots of languages which already work this way, e.g. Haskell. In Haskell, every function takes exactly one argument and returns exactly one value. It is always possible to replace a function that takes n arguments with a function that takes n-1 arguments and returns a function that takes the ultimate argument. Applying this recursively, it is ...


37

Robert C. Martin in his book "Clean Code" recommends heavily the use of functions with 0, 1 or 2 parameters at maximum, so at least there is one experienced book author who thinks code becomes cleaner by using this style (however, he is surely not the ultimative authority here, and his opinions are debatable). Where Bob Martin is IMHO correct is that ...


7

I've been spending some time these last few weeks attempting to learn the J computer language. In J, pretty much everything is an operator, so you only get "monads" (functions that have only one argument) and "dyads" (functions with exactly two arguments). If you need to more arguments, you have to either provide them in an array, or provide them in "boxes"...


7

I think it depends on your coding conventions in your project. I personally let eclipse automatically add the final keyword to every variable and parameter. This way you see at the first glance if a parameter is reused. In the project at my job we do not recommend to reuse parameters, but if you just want to call e.g. .trim() or set a default in a null ...


5

Polymorphism with no shared interface will ONLY let you store objects in the same (strongly typed) container, not actually DO anything. You can't disptach method calls or anything like that. In your specific example, I think composition would be more appropriate, no? You already have an abstraction for a specific point in 2d space, so why woudn't you reuse ...


5

The purpose of the factory pattern is to provide an abstraction between object creation and code that needs to create objects. There are many reasons why this may be desirable: Objects might be able to be pooled, and a factory can manage the pool. This is what Java's Integer.valueOf(int) does. Objects might have tricky constructors to use. This is often a ...


4

A language based around how it constrains the developer is dependent on the assumption that the language developer understands the needs of each programmer better than the programmer understands those needs themselves. There are cases where this is actually valid. For example, the constraints on multithreaded programming requiring synchronization using ...


2

I don't think what you are showing in the sample is bad, but there are some alternatives. You can create a type that represents the car kind, for example, minimally enum CarKind { Default, Fancy }; Then have a simple constructor in Car that take the CarKind, so clients do new Car ( CarKind.Default ) instead of CarFactory.createDefault (). The ...


2

The two subclasses does not have any interface in common, so why you do you want to have them inherit from a common class in the first place? The Location class does not provide you any benefit as far as I can tell. To me it would seem to more logical to have the Line contain two Point's.


2

Are they a smell? No, they're not. They make our code better by separating the responsibilities of creating an object and of using it. I don't know if your language has the concept of "using" a resource, then automatically disposing of a resource, but it can hamper testability. Consider the following code from a service implementation. public ...


1

So, You have two issues here 1: Prevent/Resolve Simultaneous actions (withdrawals) on a single resource (bank account) 2: Enforce processing actions (withdrawals) in a particular order In your example, you could resolve 1 in a number of ways, but lets take the simplest. Processing the withdrawal calls ReduceBalance(accountId, amount) on some bank account ...


1

The guiding principle at work here is probably "if you ever need to run a query on the data, and not just display it in its native form, then the data needs to be first-class rows and columns, not some JSON stuffed into a single field."


1

I think you are confusing Minor version and Patch version. From http://semver.org/ : Given a version number MAJOR.MINOR.PATCH, increment the: MAJOR version when you make incompatible API changes, MINOR version when you add functionality in a backwards-compatible manner, and PATCH version when you make backwards-compatible bug fixes. This leads ...


1

I think you alluded to the answer of when it's bad: these kinds of factories usually don't do runtime decision making In my opinion, factories should generally only be used when you don't know at compile time what you need to be creating. While the factory example you gave might actually serve a useful purpose in whatever context its being used for, I ...


1

the classic way is to use a Factory class with your switch Factory.Create(data) { switch data.class case : combo return new Combo(data) .... } this can be made more generic using reflection until you end up with a dependency injection container container.Register<FormElement,Combo>().Named("combo") container.Resolve<...


1

I think you've done a great job in decoupling the dependencies, what I could suggest here is to not return already SortedDataQueue object from DataLoader since it's responsibility is only to retrieve data. In that case, you can create the class: class DataCollector { SortedDataQueue collectIntoQueue(); DataCollector init(Array<Data&...


1

You will need two things: Closure Composite data type I will add a mathematical example to explain the answer written by Jörg W Mittag. Consider the Gaussian function. A Gaussian function has two parameters for its shape, namely the mean (center position of the curve) and the variance (related to the pulse width of the curve). In addition to the two ...



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