1

How would I be able to shorthand the following to one line to keep my code clean?

if ($scope.user.unit) {
    $scope.user.unit = $scope.opts.unit[$scope.user.unit.id - 1];
} else {
    $scope.user.unit = $scope.opts.unit[0];
}
2
  • 5
    What would make a one-liner "cleaner"? Five readable lines is cleaner than one full of ternaries and funky logical operations, IMO. Of course, a single ternary rarely killed anyone.
    – ssube
    Oct 6, 2014 at 21:11
  • 1
    I agree with @ssube, readable code is clean code and often more verbose. Its your minifier's (like uglify) job to mangle and compress it into smaller statements for production, not development. Oct 6, 2014 at 21:15

6 Answers 6

5
 $scope.user.unit =
 $scope.opts.unit[$scope.user.unit ? $scope.user.unit.id - 1 : 0];
4

Use the ternary operator ?:

$scope.user.unit = $scope.user.unit ?
    $scope.opts.unit[$scope.user.unit.id - 1] :
    $scope.opts.unit[0];

You can also make it more readable (and thus cleaner) by playing around with your variables:

var userUnit = $scope.user.unit;
var optsUnit = $scope.opts.unit;

var newUnit = optsUnit[userUnit ? (userUnit.id - 1) : 0];

$scope.user.unit = newUnit;
3

Your code has nothing to be ashamed of as-is, but you could do this:

$scope.user.unit = $scope.user.unit ? 
    $scope.opts.unit[$scope.user.unit.id - 1] : 
    $scope.opts.unit[0];

Concise code is a good thing, in general, but it's important to get a sense (on a language by language basis) of the fine line between "concise" and "obfuscated".

2

Ternary!

$scope.user.unit = $scope.user.unit ? $scope.opts.unit[$scope.user.unit.id - 1] : $scope.opts.unit[0];
1

$scope.user.unit = $scope.opts.unit[$scope.user.unit ? $scope.user.unit.id - 1 : 0 ];

-1

You could use the || operator to provide an alternative object when it is missing:

$scope.user.unit = $scope.opts.unit[($scope.user.unit || {id:1}).id - 1];
5
  • introducing the artificial {id:1} does not make the code shorter and cleaner
    – xmojmr
    Oct 8, 2014 at 17:47
  • @xmojmr: That would be your own personal option... What is it that you think is wrong with the code, that would grant a downvote?
    – Guffa
    Oct 8, 2014 at 18:39
  • OP wanted: shorter equivalent clean code (== faster to read, easy to maintain). What you provide is shorter equivalent code but failing clean part of the constraint. You're adding [($, ||{, }). ciphers. The new anonymous object disables automatic renaming (refactoring) variable name user.unit.id. Introduced new magic constant 1. Shorter but hard to read and difficult to maintain code, not useful. That is my personal opinion and SO grants me the right to use my votes up/down. Solution of @Anton is better
    – xmojmr
    Oct 9, 2014 at 10:31
  • @xmojmr: You are using downvotes as an inverted upvote, which means that you automatically upvote any new answer that is added. Do you regularly revisit all questions where you have downvoted to maintain them?
    – Guffa
    Oct 9, 2014 at 10:37
  • I don't want your code in my code base. It is a working hack but not useful. I gave you 3 reasons. About my voting behavior - is not voting anonymous and a tool of coming to truth using democracy? What are you talking about?
    – xmojmr
    Oct 9, 2014 at 10:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.