r/programming • • Aug 05 '19

Why Declarative Coding Makes You a Better Programmer

http://minborgsjavapot.blogspot.com/2019/08/why-declarative-coding-makes-you-better.html
0 Upvotes

15 comments sorted by

View all comments

23

u/HomeBrewingCoder Aug 05 '19

Why choosing your metrics decides your results, would be a better title.

public final class RestDeclarative implements Rest {

    @Override
    public List<Person> page(List<Person> persons,
                             Predicate<Person> predicate, 
                             Comparator<Person> order,
                             int page) {
        return persons.stream()
            .filter(predicate)
           .sorted(order)
           .skip(RestUtil.PAGE_SIZE * (long) page)
            .limit(RestUtil.PAGE_SIZE)
            .collect(collectingAndThen(toList(), Collections::unmodifiableList));
    }
}

Yes - this does strictly speaking have one statement. But would you call over your coworker and tell them 'hey check this out, I've implemented this in one statement that has zero cognitive complexity!'. Would we be considering some crazy perl one-liner as having similarly a single statement?

Don't get me wrong - I like functional coding, but this article is at best picking only metrics that prove its point.

1

u/scottmcmrust Aug 05 '19

Agreed; this article seems to actually be about how I should never use SonarQube because its metrics aren't useful.

4

u/[deleted] Aug 05 '19

I personally hate SonarQube. I feel like it gives some programmers an easily visible goal to optimize towards (making SonarQube happy) while ignoring other (more) important aspects, like good design and understandable code, which SQ can’t sense at all.

In my experience nobody really critically thinks about if the analysis rules included are even value-adds or make sense, like having accidental tabs included in a file or a copyright header omitted marked as Major issues, and it’s inability to recognize code that is safe from NPEs, as it’ll throw a Critical issue just the same.

And then you have the beyond hope programmers who take stuff like the rule “all numbers should be stored in a field” and replace 2s with private int TWO = 2;

5

u/HomeBrewingCoder Aug 05 '19

As soon as a metric starts being used for scoring, it becomes a bad metric.