Give and receive constructive feedback from your fellow campers on Algorithm Challenge solutions, Frontend and Backend projects
@abeledovictor I personally feel like the code you posted it too complex for its own good. Your mixing async/await
with normal promises, your own async handler function, and new Promise
, even tho you have async/await
code.
For such a simple case, you should be fine with just the route, service and maybe the model. I also don't believe in using .then
and .catch
if your using async/await
. It's more important to stay consistent than to do "one-liners" for the same of one-liners.
Great code is dirt easy to read and understand. Don't be clever/shorter just for the sake of it.
without getting to verbose (I'm to lazy to write out all your code hehe)
I would have this:
// route
router.get('/user/:username', (req, res) => {
try {
const {username} = req.params;
const user = await getUserByUsername(username);
res.send({status: 200, body: user});
} catch (err) {
res.send({body: err, status: 400});
}
});
// getUserByUsername service
// NOTE: if your using async await, you automatically return promises from this function
export const getUserByUsername =(username) => {
const user = userModelFactory(dbGetUserByUsername(username)); // this function is the same
if (validateUser(user) {
return user;
}
throw Error();
}
The dbGetUserByUsername
function is the same
new Promise
code. I feel like using them saves you a few lines, but were like 1 or 2 lines here or there haha
await
next to the dbGetUserByUsername
call aswell hehe
<Android Development>
My folder and and file not exist in the local storage. I made a helper to create a directory in phone storage. But it Works on Samsung J2 but not the rest.
My Helper : https://gist.github.com/johnmelodyme/c2e1d648df216fe0f168d0985418f4d7
if (FileHelper.saveToFile(readMessage)) {
Toast.makeText(MainActivity.this, "Saved to " + Environment.getExternalStorageDirectory() +
"/Brainwave/"
+ FILE_NAME,
Toast.LENGTH_LONG)
.show();
Log.d(TAG, "Data Saved");
} else {
// something something;
}
Am I doing it wrong ?
import "package:encrypt/encrypt.dart";
import "dart:convert";
class monjo_encryption {
static int theKeyLength = 32;
static int theLengthOfInitialisationVector = 16;
static var key = Key.fromLength(theKeyLength);
static var iv = IV.fromLength(theLengthOfInitialisationVector);
static var AESENCRYPTOR = Encrypter(AES(key));
// AES-Symmetric, padding: random
encrypt_string(String sendMessage) {
var encrypted = AESENCRYPTOR.encrypt(sendMessage, iv:iv);
String encryptedHashes = encrypted.base64;
decrypt_string(encrypted);
return encryptedHashes;
}
decrypt_string(var encrypted) {
var decrypted = AESENCRYPTOR.decrypt(encrypted, iv:iv);
String DecryptedHashes = decrypted;
return DecryptedHashes;
}
}
void main () {
monjo_encryption test = new monjo_encryption();
var a = test.encrypt_string("hahslkjdflkjsdflksjdflksjdfslkdfjsldkfjah");
List <int> b = utf8.encode(a);
print(a);
print(b);
}