Tapas Adhikary
Educator @tapaScript | Founder CreoWis & ReactPlay - Writer - YouTuber - Open Source
No offence, each of us must have either seen or (mostly) done/followed some Coding Practices that we are not so proud of 😆.
Let's be honest here and post what those are. It would be great to see those with some examples, if possible!
To start with me, so far most horrible coding practice I had in two places:
&&, ||, ! with if-else. I have made blunders in past where I wrote some if-else along with && and || that finally sounded very illogical and had to re-factor.As Programmers, every day is a new day with new learning. I have learned too from my horrible programming practise mistakes. I am still learning!
Let us know yours. 😄
I wrote a module couple of months ago that was the literal meaning of overhead.
We have a support center and it's running by a handful of trusted, experienced supporters that work with our platform and resolve the issues created by customers. The task was pretty straightforward, disable the skip button for one hour when an operator skips more than three issues.
What I did was to create an entity to store logs of when an operator has exceeded their skip threshold, cache the prohibition timestamp, create a job on queue and set its delay to one hour, create a worker to listen to the queue, create an endpoint to respond to the client current timestamp of the server, a boolean containing that he/she is prohibited or not, the future timestamp that their penalty would be dismissed and an interval that evaluates every single second to increment the time retrieved from server and compare it to the threshold to inform the user whenever they can skip the issues again.
All of which could be done with a simple local storage variable containing these simple info to handle it completely on client-side. I'm wondering why the hell I chose the first approach. Maybe I was bored at work at the moment. :D
Mine was a security issue.
When I first got into backend programming, one of the first things I did was build my own authentication system (sign up, sign in, etc.)
And...I stored those passwords in plaintext. Yep.
But I learned from it later on, and now all my apps use hashes with salt to store the passwords.
It turns out Facebook had the same problem when it was first created...
...and they had one password that could be used by Facebook employees to sign in to any account. Yikes.
Quick tip: If you ever go to a website's "forgot password" page, and they email you your password, that website has terrible security.
Boy... I don't know where to begin. I've broken just about every rule in the book. My main problem i think is I find I use if statements without the else a lot. Like I will do this:
foo = 300
if foo == 200:
print("foo is equal to 200")
if foo is != 200:
print("foo is not 200")
I could of easily used an else there, it also looks extremely messy.
I just give them names off the top of my head.... I'm trying to break that habit but I will literally use "foo" as a variable name if I can't think of anything.
If I listed everything else, I would go on all day. ;D
Good question and several useful answers so far! Here are some of the bad practices I have encountered:
if (x) {
return 1;
}
else {
// Do something else
}
var isPublication = "publicationId";
return (x === 1 ? true : false);
dosomething(10, 12, (data) => {
doSomethingElse(data, (something) => {
doAnotherThing(something, () => {
// More callbacks
});
});
});
Making typos and copying the typos everywhere because you couldn't notice. Sometimes you misspell a variable name and copy-paste it to different places -- it works -- However, when other collaborators glance over the variable name and try to use it in some section, the code simply doesn't work. It wastes a lot of time because we are not able to figure out that the original variable name is incorrect.
Failing to return early by mistake. Some developers accidentally skip return keyword while returning early in case of error.
if (err) {
res.status(400).end(); // This doesn't halt the execution of code
}
// rest of the code
There are many more, but these are the ones I've come across most frequently. I have always highlighted them in the code reviews so far. :D
I'm a React Native Developer so my answer will be in that context.
A function that loops all of the array of objects, then inside of it has also a check if the current iteration of that loop is an array or object, if it's an array it will recursively run itself again, and returns nothing. Imagine you have hundred thousands of data / object that is being passed from that function and it returns nothing, no mutation at all.
I want to pinpoint one project, where I would say that I have never seen the worst coding practices anywhere else.
More of a deployment/version control thing but I've seen people version control the code and then just copy the files to test + staging servers LOL. I've done a couple of bad things in the past but nothing quite like that.
That's probably the worst I've come across.
Hmmm it was with PHP 4. This is just an example how it looks it has been quite a while
function myMethod() {
$var1 = 12;
DifferentObject::myOtherMethod(&$var2);
$var3 = $var2->doSomething();
}
and the fun part was the reference was sometimes passed through 4 - 5 layers.
This was quite a confusing way to do things.
It's not that I am innocent of doing horrible things with code but this example actually kinda scared me ;D ....
I also experienced some bad coding standard ..
a. Very convenient way to declare global variable into program -- Memory leak issue . b. Use ForEach loop like a bread & Jam -- Performance issue c. Always ready to implement everything in UI layer rather then think twice and move to right place . d. Use proper design for implementation , It can avoid lot of bad practice .
Still Counting :)
Emil Moe
Senior Data Engineer
My teacher! Not only was it in Danish, it was also ridiculous. Here is the (english) translated version:
/** * Determine kids gender. */ public isBoy() { if (this.isBoy) { return true; } else { return false; } }I also had another teacher who started out, when he for the first time showed us his code, by saying "this is a little messed and now how you should do it" – he had put all 5-10 classes of the application in the same single file.