When to use pass by ref or value with methods - methods

I was asked the question below and I am stuck. I understand the difference between value & reference, but do not know when I would use each in a method.
If you were writing a method, which parameter passing method would you choose, if any? Why?
I found this below to help me get a grasp of the differences.
"If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.
If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact."

Keep in mind, passing by value makes a copy. There might be two reasons not to do this. First, if the value you are passing is some large data structure (or anything else that uses a lot of memory), it is probably inefficient and unnecessary to copy the entire thing. Second, if you want any changes to the parameter to be mirrored in the calling function, you must pass by reference. That way, the original value gets modified, not a copy.
When neither of those cases apply, passing by value is usually simpler and a better option.
There's one other thing to consider. Passing by value, since it creates a copy, protects the value in the original function from accidental modification. However, you may want the performance benefits from passing by reference. In this case, it is good practice to pass by reference, but mark the parameter as const (or whatever is appropriate in your language).

Related

Ui path studio arguments? defined as variables or hard coded values

I was doing practice questions for the rpa asociate exam and came across a question I was unsure on how to answer.
Suppose you have some arguments from an invoked workflow. Some are out arguments, some are in and some are in/out. The question asked which types of arguments supported being mapped to variables and which supported being mapped to hard coded values.
I didn't find much on the documentation expect a generic paragraph on arguments as a whole that said that you could map them to both variables and hard coded values regardless of direction.
TL;DR - If you hardcode a value to an Out or In/Out, the Out value produced cannot map to the Out argument and will not be used further down the line.
While it is probably allowed by UiPath Studio to add hardcoded values to Out or In/Out arguments, it would effectively break your automation by the nature of what an Out argument actually does.
When thinking through it, if you were to add a hardcoded value as the result of an Out or In/Out argument, all work done while the workflow is invoked would not be passed back to the invoking workflow when it is finished, since the out value produced is not able to replace a hardcoded value. An In argument, on the other hand, would work just as well whether the value is a variable or hard-coded, since that value is no longer needed as soon as the workflow that is being invoked starts (however, in most cases, it is probably still best practice to use a variable even for an In).

Is it good in Rails view to define constant just to be used once?

To a partial in a HAML file, I am passing parameters whose value is long, or methods whose name is long, for example:
"Some quite long string"
quiteLongMethodNameHere(otherConstant)
To make them shorter, I wrapped them in a constant/variable:
- message = "Some quite long string"
- is_important = quiteLongMethodNameHere(otherConstant)
= render :some_component, msg: message, is_important: is_important
Is this a good practice? Or should I just put the value on the param without wrapping it inside variable/constant?
It's a case-by-case decision. You want to balance the sometimes-competing interests of clarity and conciseness. For me, it depends on the expressiveness of both forms. If the long method name is clear, precise, and expressive, then I would be less interested in using an intermediate variable to hold its result than if it were not.
In other cases where the long form is less expressive, I will often use intermediate variables as "living" documentation, even if they are used only on the next line of code. This more explicitly reveals your intention to the reader (who may someone else, or you at some future point in time).
I find intermediate variables are much better than code comments because code comments can more easily become obsolete, and having the clarification in code makes it available for debuggers, etc. The performance hit of creating an extra variable is minimal, and significant in only the most unusual of cases.
Another factor is if you are aggregating things (in arrays, hashes, etc.) that include these function calls and values, then using the intermediate variable makes the code neater, and possibly easier to understand, as you can customize the name to make the most sense in the context of that collection.
Regardless of the length of the string, it makes sense to assign it to a variable/constant, and not directly refer to it in a view file. If it is a text, it makes more sense to put it in a i18n file.
However, it is not good to do that in the main view file. If you are going to do it, do it in the controller file or a helper file.

Using std::move, and preventing further use of the data

I have been using c++11 for some time but I always avoided using std::move because I was scared that, while reading a library where the user does not have the access to the code, it would try to use the variable after I move it.
So basically something like
void loadData(std::string&& path);
Would not be enough to make the user understand that it will be moved.
Is it expected that the use of && would imply that the data will be moved. I know that comments can be used to explain the use case, but a lot of people dont pay attention to that.
Is it safe to assume that when you see a && the data will be moved, or when should I use std::move and how to make it explicit from the signature.
Is it expected that the use of && would imply that the data will be moved.
Generally speaking yes. A user cannot call loadData with an lvalue. They must provide a prvalue or an xvalue. So if you have a variable to pass, your code would generally look like loadData(std::move(variable)), which is a pretty good indicator of what you're doing from your side. forwarding could also be employed, but you'd still see it at the call site.
Indeed, generally speaking it is extremely rude to move from a parameter which is not an rvalue reference.

How to prevent Silverstripe from changing URLSegment field value to lower case?

I have an URLSegment field in my Silverstripe CMS, and I want to have a link there with upper case. But after saving, Silverstripe changes it to a lower case string. I can't find, where this behavior is triggered. Does anyone know, how to prevent that?
The class URLSegmentFilter is responsible for the lower case conversion of the URLSegment, it calls mb_strtotlower. URLSegmentFilter is invoked in the SiteTreeURLSegmentField via Sitetree's generateURLSegment function.
The important part to take away from this is that the generateURLSegment function actually invokes Silverstripe's extension system so if you do a data extension of SiteTree, you just need to specify a function called updateURLSegment and do your own processing on top of what Silverstripe does.
From the code I linked above, the first argument passed into your updateURLSegement function would be the filtered (and lower case) version of the URL, the second argument would be the untouched value.
When doing this, I would be careful to make sure that you re-implement all the other filtering code like the URLSegmentFilter does (minus the lower case call) otherwise it will likely break the system with invalid characters.
A side note to this, I would be wary of actually doing this change. Having varied case in a URL isn't helpful to anyone, some users will think the URLs are case sensitive and for some systems it quite easily could be. I don't know whether Silverstripe would have that issue though, I haven't come across it with the varied projects I have worked on or helped people with.
One more quick thing, there is another method to implement a similar change using Silverstripe's Object::useCustomClass function and override URLSegmentFilter entirely though only use that as a last resort.

Pointless getter checks when updating an object

Hopefully this will not come across as a silly or pedantic question, but I'm curious.
Occasionally I'll be in a situation where an existing object's properties may need to be updated with new variables, and I'll do it like this (in no particular language):
public void Update(date, somevar){
if(date > this.Date){
this.Var = somevar;
}
}
The idea being that if the date passed to the function is more recent than the date in the current object, the variable is updated. Think of it as like a basic way of caching something.
Now, the interesting part is that I know somevar will never be "old" when compared to this.Var, but it may be the same. So as far as I can see, checking the date is pointless, and therefore a pointless operation for the program to perform.
So what this is really about is whether it's better - in whatever way - to perform a write to this.Var every time Update is called, or getting this.Date, comparing it, then possibly performing the write. And just to throw in something interesting here, what if Update were to be called multiple times?
If the example I've given makes no sense or has holes in it, I apologise; I can't think of another way of giving an example, but hopefully you can see the point I'm trying to make here.
Unless for some reason assignment is an expensive operation (e.g. it always triggers a database write), this isn't going to make your programme faster.
The point of putting checks in your setters is usually to enforce data integrity, i.e. to preserve programme invariants, and thus the correctness of your other code, which is rather more important.

Resources