Live data from Hacker News

Want cleaner code? Use the rule of six

davidamos.dev

321–330 of 352 posts

Re: Want cleaner code? Use the rule of six

#321

Earlier quoted context omitted.

Nah, I stand on the shoulders of generations of developers, just like everyone else here. I didn’t claim my opinion was novel, just that it’s mine. I hope others share my opinion, because I’d find codebases that fit my criteria easier to maintain than many other types. Also, do you agree or disagree with any of the ideas I put forth?

I don't disagree, I just think it's only right to credit the original authors when you use their exact phrasing.

Which original authors am I unintentionally plagiarizing? I’m not usually that adept at quoting verbatim, especially when I’ve completely forgotten who I’m quoting.

Re: Want cleaner code? Use the rule of six

#322
post #299

Earlier quoted context omitted.

My opinion is that you can write code that's easy to understand, and it is also good for the computer to run. One thing is not in contradiction with the other. It could lower the reusability of the code, by not having many abstractions, but it will be easy to understand, concise, and it will do what it was written for very well.

It should be written so it's easy for humans to understand first. If that's too slow, then it can be optimized so it's fast, if a bit less readable. 9 times out of 10 it won't be too slow in the first place these days, unless you know beforehand that you need maximum speed for valid reasons.

Yep exactly

Re: Want cleaner code? Use the rule of six

#323
post #239

Earlier quoted context omitted.

> Gah. I've seen the other side of this, a few people far too trigger happy to make FivePlusVeryLongNounVO/DTO for every little thing, and it gave me some new appreciation towards tuples and primitives. Sometimes you really don't want to go into another new file for an object type which is used in only one specific place. The rules are an exercise for a toy project. Like all similar 'rules' they are just hints to mak…

Your example breaks down the moment you put it back in context. SocialSecurityNumber works because it's going to be used across multiple subcontexts. The same reason Vector2 works over using (x, y) tuples everywhere. I'm specifically mentioning one specific place . This now requires you to go to a different file to see what's up, for every time you need HighlySpecificModelWithOnlyStringAndInt, and most naming doesn't…

You seem to be fixated at knowing what objects consist of and you seem to stop at a fairly arbitrary level - primitives provided by the language (int, string etc). Why not go all the way down to bits? I find that a well designed Value object [0] makes me more productive because I specifically don't need to know how it is implemented internally, only the exposed interface. Examples:

ZonedDateTime

PhoneNumber

SocialSecurity

Guid

EmailAddress

Point

Url

ExpiredCoupon

Regarding one specific place, if I only need to know date time and its zone in one specific place, would you recommend Tuple instead of ZonedDateTime?

[0] https://martinfowler.com/bliki/ValueObject.html

[1] https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDat...

Re: Want cleaner code? Use the rule of six

#324
post #138

I have pretty mixed feelings about this. Personally I find it much easier to debug code that: 1) fits entirely on my screen and 2) doesn't involve much state modification Every intermediate variable is a chance for me to miss some modification (e.g. it was passed to a func that modifies its arguments) and consequently misunderstand what is happening. I've been experimenting in Python with the function chaining style…

How do you debug that code?

This particular case is special because it uses 100% library functions. Typically you're composing your own functions, so you just... put breakpoints in your functions.

If you want logging, I've added an example of how to auto-log the composed functions to the gist.

Re: Want cleaner code? Use the rule of six

#325

I see a troubling trend with some coworkers where they seem to stretch the limits of time and space to make every line as dense as possible, usually using lodash. I think it is a point of pride for them, but I think it's obvious that everyone's life would be easier if they just wrote their code out "long form" and, god willing, added some comments for various steps. Instead, I find myself having to re-write ultra-den…

Do your co-workers use lodash's chain functionality? I've found it useful to achieve kouteiheika's ideal from the current top comment, with top to bottom readability without too much mental overhead.

As an example, given:

  const sales = [ {month: "Jan", day: 1, total: 120 }, ... ]
You could determine, say, the highest sales day of a given month as follows:

  const highestSalesDayByMonth = _.chain(sales)
    .groupBy("month")
    .mapValues((salesForMonth) => _.maxBy(salesForMonth, "total"))
    .mapValues("total")
    .value()

  // highestSalesDayByMonth = { Jan: 140, Feb: 90, ... }

Naturally, minimizing the complexity of the iteratee functions and carefully naming of their arguments is very important to ease debuggability.

Re: Want cleaner code? Use the rule of six

#326
Writing "clean" code is more of an art form, you can't really have easy rules.

I think the general idea is that clean code is short code, that's the base guideline. Generally shorter code does less things, reducing cognitive load. It may also have performance benefits. It also takes less space on-screen, which is also a good thing: less scrolling, ability to use bigger, more readable fonts, etc... And as explained, short-term memory is limited. Short code also tends not to repeat itself, another common advise.

But that's the baseline, all the art is in appropriate breaking of that guideline, to have short code that doesn't look like it came out of a minifier.

Splitting lines makes longer code, bad, but sometimes it is justified. So what is your justification? The article focuses on "one liners" being hard to understand, but really, it depends on many things. For example you may use a longer form if you think that it is an essential part of your code and it is critical that you should pay attention to it. On the other hand, you can use a shorter form if it is a common pattern, what is "common" depends on who is going to read your code, or the project you are working on. For example, bit manipulation can make a good part of your code base, or be a one-off thing and it will have an influence on how you write that code.

Moving code into functions is generally a good thing if that function is used often (shorter code). I think it is the origin for the term "refactoring": factoring ax+bx+cx+dx becomes x(a+b+c+d), only a single "x" remains and it is shorter. But if that function is only called once, of if the operation is hard to extract from its context, it can lead to longer, harder to understand code, and again you have to exercise judgment. For example you may want to write a specific function because it is a tricky, specific part that you want to separate from the boilerplate. There are interesting considerations to using functions, because it actually reorders code, for example "a(){do_x}; do_y; a(); do_z" is written as x,y,z and does y,x,z, which is often, but not always unintuitive.

Re: Want cleaner code? Use the rule of six

#328
post #56

Earlier quoted context omitted.

One issue with functional pipelines is that the reader has to keep track of what the types and the data are on each line. It’s fine for 2-3 lines, but it can get non-obvious quite quickly. Assigning intermediate points to named variables can be appropriate, or indeed factoring portions of the pipeline out into separate functions.

That may be true for less capable editors, but IntelliJ (and therefore Rubymine for the cited code block) annotates the stream type variable when it can prove what it is: https://www.jetbrains.com/help/ruby/viewing-reference-inform... regrettably doesn't show an example of what I'm talking about .map { | *String* key_value | key_value.partition... } where String shows up in light grey text indicating that IJ knows `k…

This does nothing for when it's a map of string to array of map of string to string. Shape is not meaning, and naming things helps communicate meaning.

Re: Want cleaner code? Use the rule of six

#329

Earlier quoted context omitted.

I don't disagree, I just think it's only right to credit the original authors when you use their exact phrasing.

Which original authors am I unintentionally plagiarizing? I’m not usually that adept at quoting verbatim, especially when I’ve completely forgotten who I’m quoting.

> programs must be written for people to read and only incidentally for machines to execute.

-- Abelson & Sussman, The Structure and Interpretation of Computer Programs, 1984

FWIW, I already provided the quote, with the source, in these comments, 3 hours before you wrote yours.

(ok, it's not exactly verbatim. But I think it would get counted as plagarism in almost any context reasonably imaginable.)

Re: Want cleaner code? Use the rule of six

#330

Earlier quoted context omitted.

Which original authors am I unintentionally plagiarizing? I’m not usually that adept at quoting verbatim, especially when I’ve completely forgotten who I’m quoting.

> programs must be written for people to read and only incidentally for machines to execute. -- Abelson & Sussman, The Structure and Interpretation of Computer Programs , 1984 FWIW, I already provided the quote, with the source, in these comments , 3 hours before you wrote yours. (ok, it's not exactly verbatim. But I think it would get counted as plagarism in almost any context reasonably imaginable.)

Ahh, I apologize for not thoroughly reading the entire comment thread before posting mine. Otherwise I would've upvoted you and said "+1" or something to that effect. I certainly didn't mean to hurt your feelings.

And also thanks for jogging my memory on the source. SICP remains one of my favorite textbooks from college, although it's been about 20 years and so everything's fuzzier than it used to be.

Have a great weekend.

Post reply on HN