You shall not covet your neighbor's badges. You shall not covet your neighbor's reputation, or his questions or answers, his mug or monkey, or anything that belongs to your neighbor.
Imagine the following code:
begin
main
rescue
puts "Error"
end
def main
puts foo
bar
baz
end
def foo
x = build_foo
y = update_foo(x)
finish_foo(z)
end
def build_foo
"this"
end
def update_foo(foo)
foo + " is"
end
def finish_foo(foo)
foo + " foo!"
end
def bar
puts "ba...
I have an object that represents a PDF file. In the constructor, I pull out various information about the file name and make it available via properties:
public class Invoice
{
//public properties
public string FullPath {get { return this.fullPath;} }
public string FileNameWithoutEx...
Is it a good piece of code?
deserialize: (profileJSON)->
return profileJSON unless profileJSON.productText?
profileJSON.productText = _(profileJSON.productText).reduce(
(acc, el)->
current = _(acc).findWhere(
_(el).pick('sequence', 'textType')
)
unless current?...
I'm integrating a density distribution over an effectively infinite space using a monte carlo integration and importance sampling (have a look here for an idea of the method http://web.ornl.gov/~pk7/thesis/pkthnode19.html)
I'm using a probability distribution with p(r) proportional to r^-3 (so c...
Interesting first time user question, too bad it's off-topic...
> The integral should give a value of about 100 , I've checked both mathematically and using another numerical simulation, but at the moment this code gives 7e−4 ...
The code gives correct output. How do I calculate time complexity for code like this?
The program finds all the combinations of items and sees if the combination gives the max profit. If the last item can't be added or there are no more items left, it checks to see if this value of knapsack is m...
1
/ \
2 3
/ \ \
4 5 8
/ \
6 7
Width of a tree is maximum of widths of all levels.
For the above tree,
width of level 1 is 1,
width of level 2 is 2,
width of level 3 is 3
width of level 4 is 2.
So the maximum width of the tree is 3.
This...
I'm currently working on optimizing a barnes-hut implementation ( connected to a previous post I did ) and I need help to optimize it further.
After some testing, the main problem appears to be in the Insertion algorithm, which takes roughly 3-10 times as long as the second longest part of the ...
@ChristopherRucinski String.format is however re-usable for many operations, it's not limited to one specific expression. Each of your enums is limited to one expression format. Overall, I think it is over-engineered, it shouldn't be necessary to create a new enum for each format I want. Additionally, String.format can use an argument twice, if I specify the argument string %1$d %1$d and the argument 42 then it becomes "42 42". If you want to keep comments down, you can find me in Code Review chat — Simon André Forsberg24 secs ago
The thing with my approach is that it puts an object-oriented way of doing everything you could do with String.format. I can also print out an argument twice
I agree that for simple expressions, my way is overkill. String.format("%s %s", "hello", "world"); does not need this solution. I agree! I am talking about the daily approaches that normal individuals use to perform String.format(). They are not generating code. They are doing simple to kind of complex placeholder formatting
Hi @Christopher, welcome to The 2nd Monitor! I have a working C#-like String.Format implementation in VBA that can serve as an inspiration if you want to take a look: codereview.stackexchange.com/questions/30817/…
@ChristopherRucinski Ok, that would solve part of it. But what if you wanted to do %1$d %2$s %1$d with arguments 42, "hello" so that the output would be "42 hello 42" ?
I was thinking yesterday about insects (as there was a spider in the house, and I couldn't help but think of anything else, even though they aren't insects), and I started to wonder if ants sleep?
After thinking about it for a while I decided that they might sleep, but then what would be the pu...
However, I can do even more advanced things that String.format cannot. Lets say I wanted to print out an argument twice, but the second argument would be augmented (+1 to argument, double it, etc...). ie. pass in 42, and it prints out 42 84
Environment
Mac OS X 10.8.5
Python 3.3.3
While my script is running, it is possible that an error may occur at some point. In that case, all processes should be properly terminated, an error message should be returned, and the script should exit.
The code I have now seems to not fulfill thos...
Yesterday I've tried a sport class to counter the fact that I've done almost nothing in term of sport since I started working in IT. I was so rusty. (I'm sorry if my English is bad, my brain is dead today, I can;t write properly)
@SimonAndréForsberg Simple. Those are 3 separate placeholders, so you would have 3 separate enum instances. You would just call the "state" variable that contains "42" twice.
@RubberDuck Well it's not a "sport" it was more of a physical activity, we were doing "urban parkour" (Translated directly from "parcours urbain"). We were doing exercise as we were running.
Similar code, so similar answer.
Please take these things to heart; I feel like I'm wasting my time otherwise.
return maxElementOfArray (arr);
You have a space between the function name and the parentheses.
Finalize your arguments if you're not gonna do anything with them. Final your loc...
@ChristopherRucinski I admit, not that often, but it does happen.
@ChristopherRucinski I think the usability and reusability of your code is very intertwined. The more usable it is, the more re-usable it will be. And vice-versa.
@ChristopherRucinski I had a feeling you were going to suggest that "3 separate placeholders" approach, and I don't find it a good solution, to be honest. It complicates things.
@ChristopherRucinski Separations of concerns: Your String.format alternative should not alter the arguments that I pass to it. If I pass it 42, it should print 42. If I pass it 42 and 84, then it should print 42 and 84. Your "feature" is not a feature that your code should be responsible for.
My factory is using method injection because I thought this was the best way to make it so far. Besides, I doubt it is a good thing after having to call on its Create method from within a dependent object.
MainPresenter
public class MainPresenter : Presenter<IMainView>, IMainViewUiHandler {
...
This may be a very simple problem but I would greatly appreciate your help. I have created a direction/intensity plot using the wind_rose function in Matlab, however, I would like to change the scaling of the legend. The plot currently produces a legend that has 12 colors that go up in 10 cm bloc...
Regarding “Now, we get taught that reusability is very important in university; however, when I applied what was taught before, I found out by SO users that my code was not reusable” – I recently read an interesting essay on the topic of reusability. To summarize: reusability has no meaning. What matters is usability. — amon1 min ago
@SimonAndréForsberg I am going to pretend I didn't hear that and at least still try. I won't sacrifice what I have now as this has a good niche usage I think that is actually wide spread
Imagine a typical webpage setting where it is possible to choose the display format of say... time and date setting. Then there are a several alternatives... along with a custom format
@SimonAndréForsberg I just wanted to link to that topical essay. It happens to make a good case that “reusability” isn't an useful word in the context of software design: what is code reuse? Isn't what we call re-use the actually intended use?
@ChristopherRucinski When you're code can support that ^^ in a clean and neat way, then you really got something (re-)usable.
@amon good point indeed
To support these things currently, you'd need to define four enums for date format, three for time format and five for post permalink format. Not to mention the "custom structure", which would unfortunately just be impossible...
(custom structure would theoretically be possible if using a custom classloader, but that's like using an atomic bomb to break an egg...)
Would I would love is to throw my idea to the Java community and see if they can bake in my idea with a keyword like stringformat. That could allow for a complete removal of boilerplate code and such. It could look nothing like enums when you are implementing it....just an idea, but very hard to accomplish
@ChristopherRucinski Consider making a StringFormat class instead, that could be useful, reusable, object-oriented, readable, maintainable, and everything that you want.
You could make the StringFormat class be a wrapper for having to learn the string formatting specifications.
@SimonAndréForsberg there was another suggested edit from that user, on an on-hold question - I rejected it arguing that while a good edit, it would put the question into the reopen queue.
KidDiamond is probably just trying to get to 1K though, or, seeing his score, trying to ocd-edit to get to a nice even rep score.. I can't blame the guy ;)
@SimonAndréForsberg I don't think it would be the same. This current implementation can be more powerful I feel than a wrapper class or string.format in general
I'm not really a Javascript guy, but I think the convention is two spaces, but I tend to use four as well. Don't forget to upvote and accept useful answers. =;)- — RubberDuck40 secs ago
printf-style formatting does have two issues: ① The syntax is horrible. ② When a format is provided as a string at runtime, the arguments can't generally be statically typed. Especially the number of arguments is important. Both problems can be solved by a wrapper method that takes the required values and applies some format.
@SimonAndréForsberg Everything you can do with String.format can be done with my approach in a more readable way. You want to add thousands separator - can do - you want to have min/max decimal places - can do - zero placeholders - can do - "semi-colon" like separate to format positive, negative, and zero number - can do ....
@ChristopherRucinski can do, yes. But what is easiest? I find your approach to require a lot of work for very small things. And then there's also the case of the Display.state global that you still have...
@SimonAndréForsberg after just finding out about C# semi-colon specifier, I have to disagree with you on not changing the arugment passed in ie. -42 as (42)
Who says the stress your way can cause with maintainability, debugging, and creating for the format string is easy?
@ChristopherRucinski a) It's my personal opinion. b) String.format is very well known among programmers, and I have not heard of many people who are having a problem with it.
True, and that is the only thing I can constantly guarantee that will be looked up everytime I have to touch String.format. Which being a lazy programmer I hate, along with the debugging, and other issues. It really breaks your "zone"
Do you have a good real-world examples of printing an argument twice? Maybe both one after another, and split (ie 42 hello world 42)? And not contrived examples
You could make your computeWidth() method completely independent from precomputing the "height" of the tree. (minor nitpick: trees are deep and not high).
If performance (and stackframes) is not an issue it's simply a matter of making the function run recursively.
private int computeWidth (Tree...
@ChristopherRucinski Then I really don't understand why you don't make a wrapper for StringFormat. new StringFormat().text(" ").str(someString).text(" ").number(42).toString() (for example)
Although what I just wrote there looks pretty much like a StringBuilder
I have dozens of functions which GET/POST to some URLs and extract data using RegEx. The URLs and regular expressions were hard-coded earlier but now I moved all of them to a dictionary. I then saw that the URLs and regular expressions are used only once (i.e. each one used only in one function)....
If she had taken your stapler, I would suggest you BURN THE WHOLE BUILDING DOWN.
On a slightly more serious note, I cannot begin to fathom as to why you care so much about the fruit or why you are so paranoid that the cleaning lady is some crazy fruit stealer. Was this some kind of magical fruit...
> If she had taken your stapler, I would suggest you BURN THE WHOLE BUILDING DOWN.
Here is what I was able to come up with, thanks to @RetailCoder and some help on StackOverFlow Dealing with the last() function/method not working I was able to add xmlDoc.SetProperty "SelectionLanguage", "XPath" to make it work for me. so here is how I simplified the whole process.
I replace...
I have been using the TryXXX and GetXXX pattern lately to give clients a choice whether to trap an exception or to read a boolean. An example of this concept would be System.Integer.TryParse and System.Integer.Parse.
It is my understanding that you should get two benefits from this: 1) using an ...
And OP wants to argue.... whatever. I've work to do.
"loc1" was location (in the string), but I guess it could be better named. The reason I made BuildBadFractionException was so that callers that use the non-Try version would get a consistent exception without me writing String.Format(message) a bunch of times in the main function. — Mike3 mins ago
I'm coding a Reversi game, with an artificial intelligence using the MinMax as the search algorithm. My concern is that (most) search algorithms needs to store a lot of instances of "states", in my case, board states. What I want is to represent a BoardState with 64 SlotState (Empty, White Disk o...
When a property is essentially a boolean value for whether or not an attribute exists or is set a certain way, is it more appropriate to create an object that does not list the option attribute in its __init__() call, or is it better to simply create the object with the 'falsiest' version of that...
I have been using the TryXXX and GetXXX pattern lately to give clients a choice whether to trap an exception or to read a boolean. An example of this concept would be System.Integer.TryParse and System.Integer.Parse.
It is my understanding that you should get two benefits from this:
using an ...
If I have two .NET websites, each with their own CD jars and config in /bin, and running in different application pools, I understand that there are separate JVMs running Content Delivery for each site. If I put them in the same application pool, do they share a JVM? And if so, how does CD get co...