I prefer function names that more clearly indicate what the truth value of the return value means. So here, I would probably use something more like: isInRange(v,min,max).
Seriously, this one you replied to here is the most important yet under-emphasised tip in coding. You will save everyone who reads and works with your code (note: this especially includes you) a lot of headaches by always taking an extra few seconds to make sure your names are as clear and accurate as possible. Don't worry about how many characters you're using, that's why we have IDEs with autocomplete. If you find you need to use too many words to name a function well though, it's likely a sign that you are doing too many things that aren't necessarily related. If you feel this is the case, you should probably break it up into smaller functions that you can name well without too many words. Then maybe call those functions in a broader function called something like handleMyComplexProcess.
I didn't even notice that until now. This code was for a barycentric coordinate tester to see if a point is within a triangle (wrote it from a article I was reading). This range check failing for 0 and 1 cases prints that a point is on the edge of a triangle when it should say at the vertex of a triangle.
I was messing around with one of those 'coding challenges' sites and noticed that a lot of people seem to think that the shortest most 'simplified' way is always the best way. And often times the readability suffers from that. I'd rather have code someone else (or my future self) can easily figure out just from a first read than have code that takes a minute to figure out but at least has the least amount of lines.
yeah, adding a few lines can do a lot for readability. same with how people that are learning programming benefits from using x = x + 5 instead of x+=5, if(x == true) etc
I need to add more comments in my code. I wrote this during a few week coding binge (?) where I programmed 4 - 5 hours a day, staying up until 1 - 3 am half the time with no caffeine. This doesn't sound like much, but I am just a high school student so I was deprived of a lot of sleep. I only took a break because I reached a point in the main program where I wanted to stop to learn react/redux for a GUI.
I will always support self commenting code (descriptive function and variable names) over relying on comments, but either approach is better than dense, uncommented code
Why not both? Yes, code should be self documenting as much as possible, but realistically it just can't be 100% of the time. It's good practice to explain what your functions are doing for example.
It's readable, it's concise, it works (if it really should be strict inequalities). Code isn't a puzzle with one solution, and shorter isn't intrinsically better. If you can replace 10 complicated lines with a simple one, yeah sure. Replacing 3 simple lines by one equally or sightly less simple one,.. meh. There's some amount of subjectivity of course.
Edit: The only thing I might argue about, is that the function shouldn't exist at all. It's a simple check, just write it inline whenever you need it, and save the reader the trouble of figuring out which parameter is the min, the max, or the value, and especially save them the trouble of figuring out what happens when you give only one argument, because that's not really intuitive.
The only thing I might argue about, is that the function shouldn't exist at all. It's a simple check, just write it inline whenever you need it
Clean code principles suggest using small helper functions to increase the readability of the code specifically so you don't have to figure out what math equations do all the time.
There's a limit to that though, you don't write sum(a,b) functions, do you? This is really a very basic function. And then later if you need another range check with non strict inequalities, you'll need another function... Not a fan.
As said there's always some amount of subjectivity and personal preference involved. In a code review at work, I would probably really only complain about the default parameter values, unless it somehow makes sense in the context.
There's a limit to that though, you don't write sum(a,b) functions, do you?
Of course not. But for example if you are doing a lot of game dev the code will be full of different range checks and becomes an absolute mess if you don't use helper functions.
With your example, the comparison evaluates to true which would be returned in OP's implementation and in my proposed shortening without the if clause.
My first thought was this, and that the min=0, max=1 is unnecessary. You could just return (v > 0 && v < 1)
But then I realized you could provide other values for min and max, which could also lead to other problems, such as min being greater than max, or min=false, max="one" (type differences)
whether the source is compiled to bytecode first or not is an implementation detail independent of the language itself. if you write two js implementations, one a basic tree walk interpreter and one that first compiles to bytecode, are they not both javascript?
Compilers being able to optimize the code is not an excuse for having unnecessary complex control flow.
Having few return statements and less branching is better for readability. Of course this doesn't apply to every cases, sometimes it makes more sense for a human reader to have things separated, but it's not the case here.
Having one if statement is not "unnecessarily complex". Considering how many beginner programmers write code like OP above I would actually claim that writing the control flow makes the code more readable.
Its the very definition of unnecessary complexity. Its an extra step that's entirely unnecessary. It just isn't egregious multiple layers of unnecessary complexity.
Writing code that's understandable to beginners isn't typically the goal. I'd argue most experienced programmers see the if statement and expect something more interesting to happen, and it takes a second of searching for why they used an if else before you think "oh, they should have just returned the result of the comparison". Having a bit of theory of mind for your fellow programmers usually makes it easier to understand what they're doing, but that's tough with beginners because they're still forming the way they think about code.
Its the very definition of unnecessary complexity.
It's the definition of unnecessary but not complex. The code isn't complex by any measure.
And I think that writing code that beginners can understand is a very good target and would make most code in the world much better.
Sure. In this case I would skip the if else tree but I disagree with the principle that having some unnecessary code means that the code is inherently bad. I would much rather have this in my codebase than lot of the shit I've seen "senior" developers write.
Well, Im not arguing that you must write the least possible code to be good. Im arguing that the code you push should follow what someone that's experienced with the language and its features would do rather than what a beginner would do.
Beginners likely dont know that they even can return the result of the comparison, so its natural they return true and false explicitly. Experienced programmers do know, so when they don't it makes you think they're probably trying to do something besides that, requiring more of your time to look for what they're trying to do, only to discover that you haven't missed anything and it was actually the trivial thing they intended to do.
A better example is transforming a list. A beginner typically goes for a for loop and starts doing something to each element and saving the result. You have to read many steps to discover the intention. A more experienced programmer can call some sort of map function, and then you immediately know the result will be a list that has been transformed by some function. Its not that its shorter, its that the standard functionality of map reveals the intentions of the code immediately vs having to put the pieces together and figure out the result. If else is similar especially when there's more than a simple return. As soon as you see it you have to start evaluating what each section will return and under what circumstances. Sometimes its still the best option but the mental cost shouldn't be ignored.
Every now and then Python tries to seduce me with conveniences like this, but then I remember you can’t FUCKING PRE-DE-INCREMENT(?) A COUNT IN A LOOP DECLARATION 🤬🤬😡😤
Good for switching from count based to index based when starting an iterative process - typically something like iterating over an array from the index length-1 to index 0 (in reverse) - a very development oriented feature for a language but not as readability and consistency/clarity (in the written code itself) focused as Python prefers - so not allowed
Right, well if I understand correctly, you can just use for loops, reversed(), and enumerate() to fulfil all of those needs, and still satisfy the readability mission of Python for that slightly unusual use case, without necessitating boilerplate for the far more common use case where you just want to process each item in a list.
Since I started coding in python the only ever reason for using indices of a list instead of the element itself was me thinking about the problem in the wrong way.
I am currently learning Python and yeah, these two things are little bit annoying, but I fell in love with python. No unnecessary brackets, easily readable
Eh i doubt that. Anyone would appreciate having min<x && x<max more. I'm just super picky with my code. I try to keep my code as easy to read as possible. It should be like reading a book or a story.
Never said anything about failing a code review lol. Just saying "readability eh" because you have to put in slightly more brain power (granted almost nothing especially if you see it often).
But that's what I mean about reading a book, I want a developer to just focus on the main logic of the code.
I think I see what you're saying. If you mean make it an arrow function ( => ), then maybe but not really necessary.
What the other person was saying is that the comparison should be less than or equal to ( <= ) or greater than or equal to ( >= ). Those are the correct way to type it in js.
Unfortunately, I'm not familiar with java/c languages so I can't really answer you there.
The simplest way I can describe it is that it's a compact form of writing a function. It's particularly useful in something like a "for Each" on an array. You can write a compact function inline (for each item in this array, follow this set of instructions).
This seems to be something that people struggle with, even with a few years experience. I think the concept of Boolean variables is hard to wrap your head around. I’ve worked with very smart people and good coders who still sometimes do stuff like this.
Not a single comment about rangeCheck(0, 0, 0) returning false? Also how about rangeCheck(69, 100, 1) also returning false? Or are we all adults here (especially at 1 am)? ;)
37
u/angelaslittlebit Dec 26 '21
I prefer function names that more clearly indicate what the truth value of the return value means. So here, I would probably use something more like:
isInRange(v,min,max).78
u/zeroxoneafour0 Dec 25 '21
Can someone tell me the problem here
3
u/[deleted] Dec 26 '21
why is min in arguments highlighted and not max ? Is min an in-built library function ?
287
u/TristanEngelbertVanB Dec 25 '21
I can't stand the max before the min
406
u/HTTP_404_NotFound bool isEven(int num) => num%2==0 Dec 25 '21
Bad, no. Readable, yes.
Can be simplified, yes.
60
u/helloITdepartment Dec 25 '21
Also, shouldn’t it be <= and >=?
20
u/Reaper_Houstan Dec 25 '21 edited Dec 25 '21
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
7
u/99999999977prime Dec 26 '21
It’s ok to write code at 1am as long as you do a code review not at 1am.
4
u/F54280 Dec 26 '21 edited Dec 26 '21
This is not bad code. It is r/inelegantcode/ at best.
I don’t think such content should be allowed, or the sub will become completely worthless.
Edit: typo
2
u/fauzend Dec 26 '21
I would prefer
isRangeBetweenreturn (min <= v && max >= v), after all, it will have boolean output.2
u/[deleted] Dec 27 '21
quit
2
u/[deleted] Dec 26 '21
The real questions is why have a function called rangeCheck in the first place.
1
u/a_v_o_r Dec 26 '21
At least you didn't inverse the operators. God knows I can do the stupidest shit at 1 am.
1
u/Irratix Dec 26 '21
This really isn't bad code, and I'm not sure it's necessarily preferable to make it more concise.
I would recommend semicolons though.
1
u/Tremolat Dec 26 '21
It's good for setting breakpoints 🤷🏻♂️
1
u/AndyWatt83 Dec 26 '21
This seems to be something that people struggle with, even with a few years experience. I think the concept of Boolean variables is hard to wrap your head around. I’ve worked with very smart people and good coders who still sometimes do stuff like this.
The amount of times I’ve seen: If(x==true){…}
1
u/Rajaken Dec 27 '21
Does J's really need that if or can you just state return min<v&&v<max?
1
u/chazmuzz Dec 27 '21
where are the unit tests
1
u/[deleted] Dec 27 '21
i can do this same in 1 line of code
return false;
1
u/babalaban Jan 10 '22
Not a single comment about
rangeCheck(0, 0, 0)returningfalse?Also how about
rangeCheck(69, 100, 1)also returningfalse? Or are we all adults here (especially at 1 am)? ;)1
u/[deleted] Dec 26 '21
It’s 4am here and it all makes a lot of sense…