Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a list of mouseOver functions, which for other pages will be much longer, so I'm looking for a way to generate the mouseOver functions in a loop.
Here's a small list of 5 functions in an example, by putting that in a single loop functions I should be able to understand how to expand it to more.
function mouseOver1()
{
document.pic.src ="img1.jpg"
}
function mouseOver2()
{
document.pic.src ="img2.jpg"
}
function mouseOver3()
{
document.pic.src ="img3.jpg"
}
function mouseOver4()
{
document.pic.src ="img4.jpg"
}
function mouseOver5()
{
document.pic.src ="img5.jpg"
}
Thanks for your help!!
(Moving discussion from comments)
You don't want to create five different methods. You want one method that can handle all of your situations.
function mouseOver(i) {
document.pic.src="img" + i + ".jpg";
}
And where before you would've called it like this:
mouseOver1();
You now call it like this:
mouseOver(1);
Does that make sense?
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am intrigued with a question that an engineer at the company I work at asked me, about whether or not it is better to have a single function that traverses an array and tests two conditions or to have two functions, with a single condition each.
I came here to ask you guys if my rationale is wrong or not.
The code was something near this:
response := ListObjectsFromS3(bucket)
var filteredEmptyObjectsArray = utils.FilterEmptyObjects(response)
var filteredNonJson = utils.FilterNonJson(filteredEmptyObjectsArray)
With each function being:
func FilterEmptyObjects(arrayToFilter []*Object) []*Object {
var filteredArray []*Object
for _, object := range arrayToFilter {
if *object.Size > 0 {
filteredArray = append(filteredArray, object)
}
}
return filteredArray
}
func FilterNonJson(arrayToFilter []*Object) []*Object {
var filteredArray []*Object
for _, object := range arrayToFilter {
if strings.HasSuffix(*object.Key, ".json") {
filteredArray = append(filteredArray, object)
}
}
return filteredArray
}
Please pardon the repetition in the code above. It is meant as a toy example.
I don't know exactly how does Go optimizes this code, but I was thinking it might "squash" both functions into something like this - of course, not in Go code but the generated machine code would be equivalent to this:
func FilterSquashed(arrayToFilter []*Object) []*Object {
var filteredArray []*Object
for _, object := range arrayToFilter {
if strings.HasSuffix(*object.Key, ".json") && *object.Size > 0 {
filteredArray = append(filteredArray, object)
}
}
return filteredArray
}
And the code of the response - also not really in Go code, but the compiler would generate a machine code equivalent to something like this:
response := utils.FilterSquashed(ListObjectsFromS3(bucket))
The point is that when I made the objdump of the optimized code and the non-optimized one, both had the functions separated and would have a CALL to each function. So, I'm trying to understand what is the depth of optimization that is currently possible or that Go compiler decided to stick with.
Let me know your thoughts
The "squashed" code you show is not equivalent to the original code. The basic rule of code optimization is that the effects of the optimized and non-optimized code must be the same but in your example, you have two functions that apply different logic to filter a list, and a third function that would apply a third kind of logic that in this particular case would give you the composition of the two original functions, but not in the general case. So in short: no compiler would do what you are asking for in this case, because the semantics are different.
There may be cases when some functions are inlined the compiler might discover more optimizations, but I don't see how your example might benefit from inlining.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am tying to download some data from parse (a string) and I get the error
"Value of type 'TableViewController' has no member 'place' on the line that says :
self.place.append(spotdetail.name!)
I have this as a global var :
var place = [""]
and this is my code in the ViewDidLoad:
let query = PFObject.query()
query!.findObjectsInBackgroundWithBlock ({ (objects, error) in
if let skateparks = objects {
for object in skateparks {
if let spotdetail = object as PFObject! {
self.place.append(spotdetail.name!)
self.tableView.reloadData()
}
}
}
print(place)
})
What can I change to make it work as I don't understand why it doesn't recognize the var place as it is in the same view controller (tableView)
thanks!
Everywhere in closure you should use self keyword for properties:
print(self.place)
As originaluser2 pointed out you are using a global variable so you do not need to user self.place. Also i'm not sure what you are subclassing in PFObject, but your func name is findObjectsInBackgroundWithBlock and you are reloading your table data there. Always keep in mind that you can only interact with the UI on the main thread. This will cause errors, so you can either pass back a callback, or do a GCD call to the main queue and then reload the data there.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I often write code such as the following
bool myFunct (...)
{
if (something)
{
return false;
}
// .... more code ....
}
The alternative is
bool myFunct (...)
{
if (something)
{
return false;
}
else
{
// .... more code ....
}
}
Of course, that else block is unnecessary, because the early return means that reaching the else statement in the first place is the equivalent of being inside it. Then there's the fact that, to make the compiler happy, I often have to change the structure of the 2nd implementation to
bool myFunct (...)
{
bool retval = true;
if (something)
{
retval = false;
}
else
{
// .... more code ....
}
return retval;
}
which is extra code and looks stupid. My question is, what do the governing authorities and priests say about this type of situation?
Not only it is OK, it is even encouraged in Spartan Programming. According to Spartan Programming - shorter and simpler code is better, and you achive it (among other ways) by fast terminations and avoiding else statements when possible
Under minimizing use of control:
(2) Simplifying conditionals with early return.
(4) Simplifying logic of iteration with early exits (via
return, continue and break statements).
P.S. It seems #Jeff Atwood also likes the spartan programming way
Of course. You're basically writing a guard condition that will stop you trying to perform unnecessary logic. It's fine.
Like many things, it's personal preference, but I prefer to see code written like your second example for simple cases, and the third example for more complex cases. That doesn't mean any are wrong, though.
There's nothing technically wrong with having an else clause for an if clause that does not terminate naturally (e.g., has a return or throw statement), it's just useless.
Several style guidelines argue against it (and several IDE and analysis tools may produce warnings to support these guidelines), but ultimately, it's just a preference.
The 2nd example looks fine to me because, if the code in the first statement is updated like below, it'll prevent unexpected behavior :
bool myFunct (...)
{
if (something)
{
// ... stuff
if (something_else) return false;
}
else
{
// .... more code ....
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can i GET and POST GLOBAL parameter?
example:
ViewBag.get = 2;
[HttpGet]/or/[HttpPost]
public ActionResult GetString(string? getnumber)
{
...////
}
You want to have global parameter in controller, but not in actions ?
I am not sure I understand you, but the best way to do that is using session. Something like that:
Set:
Session["Number"] = "2";
Get:
int number = (Session["Number"] != null) ? int.Parse(Session["Number"]) : -1;
I hope that helps, if not, specify your question :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is this a proper way to overwite the === method:
def ===(b)
self.venue === b.venue
print " new === !!!!"
end
And how do I call it on objects a and b who (as instances of the same class) both have the variable venue?
I tried puts a.===(b) but it doesn't work. (it says private method called for #<class1:0xsdfsd...>
Yes, it's proper way to overwrite === method.
You can call this method with:
a === b
or
a.===(b)
You have this error probably because you defined === method as private. Define it as a public method (above private keyword) and it should work.
The method should return a true or false, in this case you print something and the return value will always be nil. Try to change the order and use the print first.
Although you can call it using === it is much more common to use the operator in a case-statement or in the grep method.