I have been developing websites professionally since 1999 and have used a number of technologies and watched the evolution of the web since its early days.
I write about PHP, Laravel, Tailwind, and topics about improving your career as a developer.
Nothing here yet.
The funny thing about this is that I’d say it’s perfect for that. You might need to be a bit careful about what styles you add by default if you want them to be customized (eg, don’t specify a font size if you want to leave that up to the user of a component). I think the easiest way to achieve this is to add !important variants of styles, so they will override the defaults. (Although this may be considered a poor practice, it works very well). Might this approach help? It has worked well for me in the past!
Great post, Lou. This is a topic that is also close to my heart. It's a shame that women had been made to feel excluded from a field that they themselves pioneered. We need more good programmers. Making women feel included and interested in this field will get more diversity in the pool of developers and serve everyone better.
Simon Egersand 🎈 I just found this real function today. It grabs a user's photo from an API, but if you wanted to force the photo to update when it normally wouldn't, I created a boolean option for that. public function saveUserPhoto ( User $user, bool $forceUpdate = false ): void { if ( $this ->userHasUpdatedPhoto($user)) return ; if (! $this ->userHasPhoto($user) || $forceUpdate) { try { $response = $this ->graphServiceClient->usersById($user->id)->photo()->content()->get(); $photoContent = $response->wait(); $this ->savePhoto($user->id, $photoContent); } catch (ApiException $ex) { echo $ex->getMessage(); } } } I just refactored the code into this - the drawback is that I had to check existing implementations to ensure they were calling the correct function, but in the end the code should be cleaner to read and the complexity is probably about the same in exchange for a couple extra lines of code in the file. /** Process and save a single user's photo given a userId. */ public function saveUserPhoto ( User $user ): void { if ( $this ->userHasUpdatedPhoto($user)) return ; try { // Get the photo from the Graph API $response = $this ->graphServiceClient->usersById($user->id)->photo()->content()->get(); $photoContent = $response->wait(); $this ->savePhoto($user->id, $photoContent); } catch (ApiException $ex) { echo $ex->getMessage(); } } /** Process and save a single user's photo unless they already have a photo. */ public function saveUserPhotoIfNoPhoto ( User $user ): void { if ( $this ->userHasPhoto($user)) return ; $this ->saveUserPhoto($user); }
Simon Egersand 🎈 Thank you for the thoughtful response! I don’t have a specific example in mind now, but I guess the kind of scenario where this may make sense to me (to use a boolean argument) is the sort of thing where you have a lot of setup to do, but then you might want to go down a couple of roads with what it returned. I guess the “setup” work could be extracted to another function. I know the Uncle Bob Clean Code approach favours many small, single purpose functions over few large, monolithic ones. I’ve been writing lots of functions with all kinds of boolean arguments for a long time (especially in ColdFusion custom tags), and I only recently heard of this concept that such arguments are a code smell. I guess it’s time for me to have a second look at how I write these sorts of things!
Great article, Simon! I am finding it hard to universally agree with the notion of not making functions with boolean (flag) arguments. If you had to make a different function for every variation of a method, wouldn’t you often end up with multiple functions with a lot of duplicate or similar code in them? I suppose the obvious solution to this is to break such functions up into smaller methods, but then in some cases wouldn’t you end up with several methods where one would do that just accepts a few flags? Would you consider it acceptable to have a parent function with flags and some simple methods to call the parent just for readability’s sake? If you can give further clarifications or examples, that would be great!