Delete and return in a single verb? - language-agnostic

Imagine a function that deletes a cookie and returns its value.
What would you call it if you have to use a single verb?
I called it clear but I'm not very fond of the name.

It sounds similar to Pop, except Pop typically acts on the last element in a collection. Perhaps Extract could be a suitable name?

In the Ruby doc for hsh.delete(key), their doc is:
"Deletes and returns a key-value pair from hsh whose key is equal to key. If the key is not found, returns the default value."
So deleteCookie would probably be acceptable, the key is to just document the behavior properly.

I'd do something like this:
public String GetAndDeleteCookie(String cookieName); // C#
function getAndDeleteCookie(cookieName); // JavaScript
get_and_delete_cookie(cookieName); // php (forget exact syntax)

Related

How to identify duplicate keys when parsing a json object with json-cpp?

I am trying to parse a json object read from a file.
I want to identify duplicate keys, as json-cpp doesn't like them (even if they are not illegal in json).
I need to be able to say: ERROR: your json file has duplicate keys and we dont like that.
Json::Reader reader(Json::Features::strictMode());
Using reader in strictMode does not do the trick.
Set
rejectDupKeys
in
void Json::CharReaderBuilder::setDefaults ( Json::Value * settings )
JsonCPP Doc
There is no way out of the box, but you can program that functionality.
Since JsonCPP uses a map to store object keys, you have to add some code to:
Value &Value::resolveReference(const char *key, bool isStatic)
First, you have to be sure you are parsing (and not accessing some Json::Value). Then, you have to add something (like an exception or a flag) to this if:
if (it != value_.map_->end() && (*it).first == actualKey)
{
// key is already present: if parsing, throw!
return (*it).second;
}
Open an issue. That could be added easily. (Sga's idea might be the best way.) We've done a lot of work recently to make it easier to add features while maintaining binary-compatibility.

Method/function return type in call stack

I was trying to find the existence of return type constraint in method call stack. Event any language you use, (java / c++), we specify the return type of method/function. When this method enters into call stack (or in memory, I'm not sure) how does it uses our specified return type?
Another thing is why can't we specify two return types in header? like
public (int, float) myMethod(){
return (1, 2.5);
}
So this function could return two values (one int and one float).
I'm not asking here about returning more than one value from method. That off course i can do using array or creating a custom object. My question is, how this is mapped in the stack so that it take notice of return type constraint and why we can't specify more than one return type?
As you can see, the picture of call stack, I can't see anything about return type here.
So finally I summarize my question.
1) How will you modify this attached image to specify the return type and why?
2) I can not specify more than one return type in any language (that i know), why?
Any help will be appreciated!
I got it by myself.
First question:
1) How will you modify this attached image to specify the return type and why?
Answer: There is no point of return type in call stack. In many languages you don't even specify return type, like python, PHP etc. So there is no relation of return type with call stack.
2nd question:
2) I can not specify more than one return type in any language (that i know), why?
Answer: It depends on requirement. Language designer don't feel any need to return more than one value (Off course they provide alternate like returning array, but value returned only one, that is reference to array). If language designers feel the need to return two values, I think they can. But there is no need of that.

How should I create a clean user-facing slug from an object?

Given the fact that I am using a pre-existing comment/discussion solution that uses a unique string as a thread id, I need to create a user-facing slug from an arbitrary object for the thread id that fits the following constraints:
Short
"Pretty"
Human-readable
Does not reveal internals
Unique per object instance
I thought about using {FQCN}-{id}, but it violates #4 and, when web-encoded, #2. I also considered an md5 hash of the same, but that violates #3 (and potentially #1, depending on the definition of "short").
Since the objects do not have a standardized API (eg, there's no guarantee that they'll all have a getTitle() method, for example), I'm at a loss as to how to come up with a slug that fits those constraints. How would you go about creating one, and if that's not possible, what format would you use that violates as few constraints as possible?
It sounds like if you're using arbitrary objects, you'll want to let the objects decide for themselves. You'd want to have a base class or perhaps an interface that would define that the objects are "sluggable". Everything you're going to have slugs for would need to implement this.
That way, you would have
getObjectFromSlug = function () or function getObjectFromSlug ()
and
getSlug = function () or function getSlug ()
in the interface, and each of the "sluggable" objects would be required to implement this on their own. The only issue there is that you will have to manually require them to be unique on their own.

applying separation of concerns

I wonder if you think that there is a need to refactor this class.( regarding separation of concern)
publi class CSVLIstMapping<T>
{
void ReadMappingFromAttirbutes();
void GetDataFromList();
}
ReadMappingFromAttributes - Reads the mapping from the type T and stores it in the class. Has a name of the list to use and a number of csvMappingColumns which contains the name of the property to set the value in and the name of csvcolumns.
GetObjectsFromList - uses a CVSListreader ( which is passed in via the constructor) to get the data from all row's as KeyValuePair ( Key = csvcolumnName , value = actually value) and after that it uses the mappinginformation( listname and csvMappingColumns ) to set the data in the object.
I cant decide if this class has 2 concerns or one. First I felt that it had two and started to refactor out the conversion from rows to object to another object. But after this it felt awkward to use the functionality, as I first had to create a mappingretriver, and after that I had to retrive the rows and pass it in together with the mapping to the "mapper" to convert the objects from the rows
/w
Sounds like two concerns to me: parsing and mapping/binding. I'd separate them. CSV parsing should be a well-defined problem. And you should care about more than mere mapping. What about validation? If you parse a date string, don't you want to make sure that it's valid before you bind it to an object attribute? I think you should.
Rule of thumb: if it's awkward, it's wrong.
I have to say I'm finding it hard to understand what you've written there, but I think it's likely that you need to refactor the class: the names seem unclear, any method called GetFoo() should really not be returning void, and it may be possible that the whole ReadMappingFromAttribute should just be constructor logic.

What is the term for "catching" a return value

I was training a new developer the other day and realized I don't know the actual term for "catching" a return value in a variable. For example, consider this pseudocoded method:
String updateString(newPart) {
string += newPart;
return string;
}
Assume this is being called to simply update the string - the return value is not needed:
updateString("add this");
Now, assume we want to do something with the returned value. We want to change the call so that we can use the newly updated string. I found myself saying "catch the return value", meaning I wanted to see:
String returnedString = updateString("add this");
So, if you were trying to ask someone to make this change, what terminology would you use? Is it different in different languages (since technically, you may be calling either a function or a method, depending on the language)?
assign the return value to a variable?
Returned values can be assigned or discarded/ignored/not used/[insert synonym here].
There isn't really a technical term for it.
I would say "returnedString is to be initialised with the return value of updateString".
"Catch" makes me think of exceptions, which is a bit misleading. How about something like "use" or "store" or "assign"?
Common ones that I know:
You assign a value to a variable.
You store a value into a variable.
check the function's return value, do not ignore return values
In the example, you're simply assigning the return value of the function to a new variable.
When describing the behavior of that single line of code, it doesn't really matter that the return value is not essential to the use of the function. However, in a broader context, it is very important to know what purpose this "Interesting Return Value" serves.
As others have said there isn't really a word for what you describe. However, here's a bit of terminology for you to chew on: the example you give looks like it could be a Fluent Interface.
I suggest "cache", meaning store it for later.
Maybe there's a subliminal reason you're saying "catch".
It's better too state the purpose rather than the implementation details (because actual implementation can be different in different programming langugages).
Generally speaking:
- Save the return value of the call.
If you know the return value is a result of something:
- Save the result of the call.
If you know the return value is to signify a status (such as error):
- Save the status of the call.
By using the word "save", you can use that same statement across the board, regardless of the mechanism used in that particular language to save the return value.