How to use glob notation in a Java-style comment? - comments

This is kind of silly, but it's tripping me up and googling has failed me.
Consider the following:
/*
Example commandline parameters for this class:
--job-name MyJob
--output-path /path/to/output
--input-data /path/to/data/2018/08/*.csv
*/
object DataProcessor {
def main(args: Array[String]): Unit = {
// code here
}
}
The general intent is to use glob notation in a long comment; the above shows an example of why you might want to do that. The problem is that the /* in /path/to/data/2018/08/*.csv turns everything into a comment.
Is there a slick way of "capturing" the /*, short of just adding another */ at the end? I tried enclosing the comment with /** and **/ like in doc strings, but the result is the same.

Related

Pass optional objects as varags in parameter?

I want to pass multiple optional objects in function as varags ?
Optional<ab> ab = Optional.of(ab);
Optional<cd> cd = Optional.of(cd);
Optional<dc> dc = Optional.of(dc);
Optional<ba> ba = Optional.of(ba);
data(ab, cd, dc, ba);
data(Optional<Object>... objects){...}
I am getting error if i don this, any suggestion how can be proceed?
It isn’t related to varargs. You can’t pass an Optional<SomeSpecificType> where an Optional<Object> is expected. They are not compatible.
Assume just (without varargs):
static void data(Optional<Object> object) {
// …
}
Now if we try
Optional<String> ab = Optional.of("");
data(ab);
In my Eclipse I get this error message:
The method data(Optional<Object>) in the type MyClass is not
applicable for the arguments (Optional<String>)
Java generics are defined with this restriction. You also cannot pass, for example a List<String> where a List<Object> is expected.
You can overcome the limitation by declaring the method generic too:
static <T> void data(Optional<T> object) {
// …
}
Or just like this:
static void data(Optional<?> object) {
// …
}
With any of these two declarations the call above is OK.
BTW, #HadiJ is correct in the comment: Optional is meant for return values for from methods that may or may not be there. They have very few other good uses, and as parameters is not one of them. It seems to me that for your use case you should just pass the arguments that are there and leave out those that aren’t. The your data method may receive a longer or shorter argument array, but will just have to handle all elements of the array without caring about Optional. And passing String, Integer, LocalDate, etc, to a method declared void data(Object... objs) is straightforward and poses no problem.

Faker gem generating 2-3 letter strings

The Faker gem generates short, nonsense strings instead of what is described. For example, Faker::Job.title generates "et". If I have a feature test that expects not to find a Faker-generated string on the page, chances are it's going to fail if the string is "et". Surely this is unexpected behaviour, as nobody in the world has the job title "et".
This is my code, the most recent time I checked it the title was as expected, but the role and category were not:
# frozen_string_literal: true
shared_context 'with signatory attributes' do
let(:first_name) { Faker::Name.first_name }
let(:last_name) { Faker::Name.last_name }
let(:email) { Faker::Internet.email }
let(:title) { Faker::Job.title }
let(:mobile) { Faker::Number.number(10) }
let(:employee_num) { Faker::Number.number(10) }
let(:role) { Faker::Job.title }
let(:category) { Faker::Job.title }
end
Looks like Faker isn’t set up to make realistic job titles. But it’s easy to make your own random job titles. I would just sample your own custom array, like this:
let(:title) { %w[Admin Manager Engineer].sample }
You can use regex matcher with word boundaries instead of the short string only, but it is still not bullet-proof.
let(:first_name) { /\b#{Faker::Name.first_name}\b/ }
But maybe it is better to stub the attribute on model itself and raise an Error if it is called.
It seems like it pulls strings from its Lorem Ipsum String Set for some reason. Do you mind sharing your code?

How do I name the function which only does something if condition is true

According to clean code laws, we want to have functions which do only one thing and are on the same "level of abstraction". But how to name function, whose work is just to check some condition and do the work if condition is true. For example, how could this function be named?
public void HowToNameThis(){
if(!ComponentIsInstalled()){
DisableCheckbox();
}
}
I thought about naming it like DisableCheckboxIfComponentIsNotInstalled, but then the name just repeats the code, which effectively means I have created a function but did not create any abstraction.
CleanCode also suggest that you stay as positive as you can in your code. If you reverse the logic within your method, then, naming becomes easier.
public void TryEnableComponent() {
if(ComponentIsInstalled()) {
EnableCheckbox();
}
}
I generally think really hard about if the IF is really deserving it's own function.
And then often end up inlining it:
Like this (pseudo):
void SetupInstallerWindow()
{
LoadLicenseAgreement();
if(!ComponentIsInstalled()){
DisableCheckbox();
}
BringWindowTop();
}
If that really gets to messy, here's an idea for a name might provide more context for the reader:
AllowReinstallationOfComponent()

Method-level SRP vs. interface bloat

I suggested a refactoring a coworker and he countered basically quoting the SRP. Here's the situation.
We have a bunch of helper methods that to me are all a related purpose - html generation. There could be a lot of options to apply, let's call them A B and C, that you can mix and match.
His original code used a separate method for every option, and for the valid combinations. I saw this as bad because the permutations can quickly escalate out of control.
public string MethodWithA() { /* ... */ }
public string MethodWithB() { /* ... */ }
public string MethodWithC() { /* ... */ }
public string MethodWithAandB() { /* ... */ }
public string MethodWithAndC() { /* ... */ }
public string MethodWithBandC() { /* ... */ }
Our situation isn't quite as extreme as this, but I'm asking for the general case.
I said there should be a single method and the options should be passed as parameters or enum flags.
public string Method(SomeOptions flags)
{
/* minimal base processing */
if (/* flag A */)
{
ModifyForA();
}
/* etc for B and C */
}
His response was that switching on flags like that means the method is doing multiple things. I know "Clean Code" does say something about flags or switch statements being a smell, but I don't think it applies to this case. I think that rule is about finding opportunities for polymorphism. In any case, I think that my version is still doing one thing, but I guess it's up to interpretation.
Is there anything not entirely subjective that we can use to resolve which approach is better?
It is hard to tell as your examples are a bit too generic, but I don't like either of these approaches. The first one results in explosion of combinations, as you correctly note, but the alternative will result in a monstrous method which is impossible to test or analyze. I would prefer some kind of builder, or as it is popular now to call it fluent interface. Then you would have something like:
string html = htmlBuilder.WithA().WithC().Build()

How to take advantage of an auto-property when refactoring this .Net 1.1 sample?

I see a lot of legacy .Net 1.1-style code at work like in example below, which I would like to shrink with the help of an auto-property. This will help many classes shrink by 30-40%, which I think would be good.
public int MyIntThingy
{
get
{
return _myIntThingy;
}
set
{
_myIntThingy = value;
}
} private int _myIntThingy = -1;
This would become:
public int MyIntThingy
{
get;
set;
}
And the only question is - where do I set MyIntThingy = -1;?
If I wrote the class from the start, then I would have a better idea, but I did not. An obvious answer would be: put it in the constructor. Trouble is: there are many constructors in this class. Watching the initialization to -1 in the debugger, I see it happen (I believe) before the constructor gets called. It is almost as if I need to use a static constructor as described here:
http://www.c-sharpcorner.com/uploadfile/cupadhyay/staticconstructors11092005061428am/staticconstructors.aspx
except that my variables are not static. Java's static initializer comes to mind, but again - my variables are not static. http://www.glenmccl.com/tip_003.htm
I want to make stylistic but not functional changes to this class. As crappy as it is, it has been tested and working for a few years now. breaking the functionality would be bad. So ... I am looking for shorter, sweeter, cuter, and yet EQUIVALENT code. Let me know if you have questions.
I'm afraid that you have no option.
If you want to use an auto-property with an initial value that differs from the type's default value then you'll need to set the initial value in the constructor(s).
If you just need a stylistic, non-breaking change, consider changing the format a little:
public int MyIntThingy
{
get { return _myIntThingy; }
set { _myIntThingy = value; }
}
private int _myIntThingy = -1;
Isn't that prettier?
And consider using auto-properties for future code only. It sounds too risky to use them on existing code, unless there are no default values.

Resources