The miles vs. km wasn’t their example. They used converting to uppercase and lowercase instead, and they did recommend setting up different functions, which I think makes sense there.
I’m actually working on some code that calculates distances and will be used by others. I have simple mi_to_km and km_to_mi convenience functions as part of the package, so users could simply write something like km_to_mi(distance(x)) if they want the answer in miles. But I think writing distance(x, miles=False) is a little simpler from the user perspective, especially since the typical use case is miles in which case that argument doesn’t need to be supplied.
Yeah I don’t think there’s any problem with distance(x, miles=False). I forget in python can you have missing parameters like Javascript? I like using them in cases like this.
So for the more common (correct American) case you’d have distance(x) and the (lame Euro) edge case would be distance(x, km=True).
Also I wasn’t thinking about named parameters when I gave the advice above. For most cases named parameters are as good as passing an object imo.
But ask yourself if the user might ever want meters or anything other than those two options. If so, don’t make it a boolean. It’s hard to clean up later when there’s a bunch of code out there and you realize your boolean is actually a multi-choice. That might be what they’re talking about with not using flags. I’ve gotten burned on that many many times.
These are useful. The others are meh. The book " Clean Code: A Handbook of Agile Software Craftsmanship" is strictly inferior to the guide of the previous generation “Code Complete”. “Uncle Bob” does not impress.
I do think the miles option is bad in your example. It’s not the end of the world, but consider:
Do all of your functions (now and for the rest of time) have the same option? If not, it’s confusing. If so, you’re duplicating the same conversion code for no real reason.
You need to put some thought into how to handle weird inputs. What should you do for distance(x, miles=7.5)? Or distance(x, True)? I would call both errors, but properly handling them is nontrivial and perhaps surprising to the user.
You might realize later (or perhaps right now!) that it’s better to provide this feature through some kind of context, as with output_units(Units.MILES): distance(x). If v1 of the API uses a flag, you’re stuck with supporting that feature forever in addition to or instead of the new approach.
Each flag doubles the number of behaviors your function can have that you should be testing. It’s better for each function you provide to do exactly one, well tested thing and let the combinatoric explosion of behaviors occur on the user’s side (because each user is only assembling your tools in one way, so they can analyze and test their particular assemblage thoroughly and without a lot of burden).
Thanks for all the feedback. I may rework it so that the the units option isn’t a Boolean, which should make it easier to extend functionality if needed and simplify error handling.
You might find some inspiration from the way the decimal module in the standard library uses a context object. Unfortunately decimal is a C extension, so there’s no Python implementation to look at.
EDIT: Oooh, but the stdlib has this module to handle all the bookkeeping and thread safety stuff!
the next year of my life is gonna be completely java/spark and I have not programmed java (or OOP really for that matter) in 8 years at least, it’s becoming quite obvious and I need to practice.
i cant emphasize enough that it needs to be fun. I hate this language with every fucking fiber of my being.
I know the prospect of having to write Java is taking a toll on your mental health, but let’s not descend into frank delusion. There is no Santa Claus and there are no “fun” Java projects.
At least you’ll have me to commiserate with, and if you really haven’t written Java in 8 years then you are probably not familiar with JDK 8, which introduced the Streams API, plus various other enhancements since. The language is still horrible, but it’s not quite as soul-destroying as it used to be.
If you are going to have to write new stuff and not just update then you will have to catch up on some stuff. I use Streams API in virtually every function I ever write, java.time has made Date and everything associated with it obsolete, etc.
Why can’t git just say resolve using the branch names - IE - “develop” or “stage” - rather than confusing as hell “mine” and “theirs” that mean different things with rebase vs. merge?
I always have to look it up, and then read it several times before I feel comfortable.
Me: Well I should be done with my only project (that I’ve been deliberately dragging out), and the new project isn’t starting for a while, so if you have anything.
Boss: Hmmm, well there’s this one PHP-wordpress-oauth thing that no one has time for right now (and sounds painful). No rush though.
“No rush though” is the worst thing to say to me. Just tell me you want it. I need to know my work is important and appreciated. Otherwise I struggle with motivation.
oh god i’m not even close to that crap yet. basically right now i’m writing kafka connector task logic that sources from one really disastrous nosql db into a very very strictly typed schema in a RDBMS.The typing is a total nightmare, they can put literally whatever the fuck they want into the source, and it is just one massive json blob I have to parse out the fields from. oh yea and it needs to be “real time” but dont get me started on that yet.
Having written python so long I had quite an embarrassing bug go live that came down to me just unconsciously typing a lazy thing i do sometimes that usually works fine in python and bash:
if (str == "") {
//noop
}
well ok yea duh I understand str is an object and it doesn’t work that way in this language, I just was going fast and forgot. that’s why I want a project so these unconscious mistakes stop happening.
We are working with JDK 8. I will look at streams API for sure. My idea for a project was deploying a minecraft server with my little brother and writing some mods for it.
Feel like the tasks I’m getting here are just things intended to make me miserable - worst on call rotations, most dumpster fire projects, now I gotta be a java dev on a team whos principal thinks his java is great (it is really bad).
luckily my capacity for misery is superhuman, thanks for the advice.
dlk9s jr. wants to learn to code, specifically to develop computer games. He’s 13, heading into 8th grade.
He has some knowledge already. He’s taught himself some things by watching YouTube videos and has made the beginnings of some PC and VR games in Unity. For example, he’s made a rudimentary FPS that does actually have a few weapons, hit detection, and scoring. It looks like nothing, was just his way of figuring things out.
He’s also made some Roblox games. His latest is some idle clicker or something.
Boy has also taught himself Blender and finished 3rd in a 3D modeling competition for 5th and 6th graders last year. He has had fun with that and animation, another interest of his.
So, he has some knowledge. He’s always want to go 0-60 in zero seconds, had never had the patience to want to sit down in a class or with a formal lesson and start from square one, but now he thinks he’s like to try a class or a coding camp over the summer.
He says he wants to do C#. Any thoughts on what to look for? My main concern is that we’ll end up enrolling him in something for “kids” and he’ll be bored out of his mind.
i don’t think i would recommend c#, although i haven’t programmed in it in a while. for one, i would look for something with easy or forced syntax and none of the type hierarchy bs, and some idiomatic features with useful syntactic sugar. c is a bit tto low level, and i want to say js is too high complexity. maybe golang ( can’t believe i’m writing this). maybe python.
ideally i would say the language should be useful and applicable to a project they want to do, have lots of available examples, lots of open source libraries they can read, learn from, and use, and help them learn the next language down the line. i just don’t see how c# wins in these considerations.
if the kid wants to nerd out, my first semester SICP was taught in a variant of Lisp, because the prof felt after such an intensive mindfuck, learning any other language down the line would be a cakewalk. i agree with that philosophy, but i wouldn’t recommend languages like lisp or caml to a middle schooler. too niche
As the resident C# enthusiast I totally disagree. C# has tons of available examples and open-source libraries. He’s already messing around with Unity, C# is the language to use Unity’s built in scripting API. It’s a C-style language, which is obviously a good language style to understand. It teaches you both object-oriented and functional paradigms. It has piles of syntactic sugar (much of which has probably come along since the last time you wrote in C#). It invented async/await, which is useful to learn if you’re going to move on to JavaScript. The IDE gives you tons of help when you write it. It’s widely used - I think among general purpose languages probably fourth after JS, Python, Java.
JavaScript I think is a bit too weird a language to be a good first choice and not well-suited for the types of projects he is looking to do. I don’t speak Python but I assume it would be an OK choice too. Anything much less popular than C# seems like it’s too obscure to be a good choice. And if @dlk9s tells us he’s decided to teach the kid Java, I am going to call the police.
See if you can find some way for him to audit a summer class at Georgia Tech or local community college on C# or Unity or game development. I’d guess that a camp situation is likely to either be really expensive or be too kidsy. Plenty of online choices, but I’d guess that he’s aware of them and that’s not what he’s looking for.
Thanks, I’ll look into local colleges. My wife found a coding camp where one of the learning “trees” has Unity, C#, etc, so I’m taking dlk9s jr. for a free one-hour session on Friday to check it out.
I’ve actually bought him some online lessons in a wide array of coding and game development disciplines the last few years, but yeah, he hasn’t done much with them. Stuff from Humble Bundle and what-not. But at least we have them if he wants them as reference.
He’s one of those kids who just wants to learn on his own terms and hates school (moreso because of stress nowadays), so he’s been very self-motivated to just teach himself when he gets something in his head that he wants to do. For instance, he plays percussion in band, but lately he’s been teaching himself how to play on a real drum kit (we bought him an electronic kit a few years ago that he barely touched until this year) by simply printing sheet music and plugging away. His favorites right now are a couple Weezer songs (Buddy Holly and The Sweater Song), Led Zeppelin (D’yer Maker), and this:
(no, he hasn’t figured out how to play the part when the drummer goes HAM)
But despite his loathing of school, as I mentioned before, he now thinks maybe a formal class might be the way to go for programming.