News for hackers. Stuff that matters.
Re:Programming Patterns I Like by baolongtrann on Friday June 28, 2019 @09:53PM (#20310322)
I agree with the 1st pattern.

The 2nd one, there's a caveat about space/GC here.

The 3rd one depends a lot on the situation.

4th one is too obvious for it to be called a pattern, but I agree with it.

5th one is, meh, not really easy to read. I prefer his "bad" example. It could be less verbose, e.g,

if (!conditionA) result = "Not A"

else if (conditionB) result = "A & B"

else result = A

Re:Programming Patterns I Like by foota on Saturday June 29, 2019 @03:47AM (#20311350)
Personally, I'd prefer to go with early return, I think. Depending on what the conditions mean semantically.

It would be:

  if (conditionA && conditionB) {
    return "A & B"
  }

  if (conditionA) {
    return "A" 
  } else {
    return "Not A"
  }
  
  return "neither A nor B"
I think this comes from a general dislike of more than one else on an if and a dislike of nesting.
Re:Programming Patterns I Like by masswerk on Friday June 28, 2019 @11:43PM (#20310694)
Regarding nested ternaries, while not recommended, short circuit evaluations may be even more readable:

return ( (cA && cB && "A & B") || (cA && "A") || (cB && "B") || "neither" );

Re:Programming Patterns I Like by foota on Saturday June 29, 2019 @03:45AM (#20311339)
Quick, it's another case of Javsacript Stockholm Syndrome!
Re:Programming Patterns I Like by masswerk on Sunday June 30, 2019 @03:55PM (#20320145)
:-)

However, this would also work in Perl, etc, (anything featuring short-circuit evaluation and type coercion for evaluating conditions.)

Re:Programming Patterns I Like by scns on Saturday June 29, 2019 @03:52AM (#20311363)
Same concerns about #2 here, prettier code but worse performance, Form over Function.