Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
And then the manager will ask you not to write in such way to preserve readability adding to the frustration
-
pmarius868yOr you could do the cleaner version and go with:
notBar = !foo.isBar();
foo.setBar(notBar); -
Which one is best practice?
Writing readable and long code, or writing short and less readable code. -
Draxes1098yI would think, that the short solution is a common thing. No need to use the first one.
-
jonjo9428y@saifat29 best practice would be to put the simple short hand and clever logic into a function and use that but in this case there is already a function for the job.
-
@vhoyer its
cmp, stor, jump, set, jump
vs
xor, stor, jump, set, jump
I honestly think cmp is expenisve enough to make the second thing faster..
(I ignored the jumps for isbar, they shouldn't play a role) -
Grumpy28828y@saifat29 The short version is actually more readable to anyone who understands what a boolean is.
The first version is more of a sort of explanation to those who aren't familiar with the language or its logic operators. Such explanations belong in tutorials about the language and should not be repeated in code written in the language.
Having said that, I always prefer the simplest code possible, but adding redundant verbosity is not the same as simplifying. -
In my readable code is more important (until working on a time critical situation/ software) since most software end up being the responsibility of someone else...
But, in the above case, the shorter one looks better, but probably, even works better.. (not sure, ask me in the morning since I am nearly been awake for more than 20 hours and my brain might soon go into hibernation)...
In my opinion, to make it readable, we should place the condition into another Boolean variable...
Like someone else suggested on this rant... Let me look at... For a moment... Already told you the reason for my clumsiness..
Yeah, like @pmarius suggested... -
Grumpy28828y@pmarius "notBar = !foo.isBar();"
That roughly reads like "notBar is not isBar". What's the point of that? -
pmarius868y@Grumpy That was a simple case, but in practice there are several conditions for if statements and instead of putting them all in the IF and chaining them there, you can easily declare them just before it.
For example, tell me what can you as an outsider read faster:
if (object.getMainContainer().isSet(randomvar) || (randomVar.length > 0) || ((randomVar.length%2)==1)) {
...
}
versus
varSet = object.getMainContainer().isSet(randomvar);
atLeastOnce = randomVar.length > 0;
oddCount = (randomVar.length%2)==1;
if (varSet || atLeastOnce || oddCount) {
...
}
The second version gives more information to the next guy reading it. Keep in mind that in practice there might be even more conditions. -
@qbalsdon Yes, but that often vary from language to language. Some languages don't need that.
-
yfede1548yI'd write that as
foo.setBar (foo.isBar() ? false : true);
or
foo.setBar (foo.isBar() == false)
depending on what really those methods are.
I expect a modern compiler to be smart enough to avoid redundant comparisons and simply replace that with a NOT -
Grumpy28828y@rephiscorth Because that could be the name of the function containing the code we discuss, but we're discussing the code itself. ;-)
-
@Grumpy if discussing the implemontation, it should be `this.bar = ! this.bar`. No need to call the public getter inside the class
-
Write in the former style then R# it.
<alt>+<enter> = pick option that maks your code look like it was written a smart arse.
Related Rants
There are two kinds of people:
those who write
if foo.isBar() {
foo.setBar(false)
}
else {
foo.setBar(true)
}
Vs
those who write
foo.setBar(!foo.isBar())
Only good programmers will understand😢😊
undefined
good programmers
programmers
boolean