This content originally appeared on DEV Community and was authored by Vardana Bhanot
So a little about me, I joined a company as a software developer a month back and it's my first job, in which I am mostly working on PHP and JavaScript specifically jQuery.
So most of the things I am gonna mention here are the once I learnt after writting a lot of jQuery speghetti.
So as I started working on a new feature I was given a new file, so I was just trying to make things work in the front-end and ended up thinking that I will structure these things after I make up things work. So after the things were done and I had to add something new and after adding the new thing everything broke.
And then I realised how difficult it was to read the code I had written.
I was having difficult time reading my own code.
So here I am gonna tell you things I learnt after getting I that situation.
And all the code example are in PHP.
1.Divide the code into functions:
A single function should just do a single thing, and structure your code from the start, as it will make it easy for you to debug the code as well as for someone who might have to make changes to your code in future.
Making your function do just one thing makes it more readable and modular so you can reuse it anywhere you want.
2.Try not to nest if-else much and return early
Nesting if else too much make it difficult to read
for example.
Bad Practice
function is_winters($month){
if(!empty($month)){
if(is_string($month)){
if($month == 'january'){
return true;
}
else if($month == 'december'){
return true;
}
else if($month == 'november'){
return true;
}
}else{
return false;
}
}else{
return false;
}
}
Good Practice
function is_winters($month){
if(empty($month)){
return false;
}
$winter_months = ['november', 'december', 'january'];
return in_array(strtolower($month), $winter_months, true);
}
3.Avoid using for loops
Here I am talking about the basic for loops like
for($i=0; $i< $num; $i++){}
using foreach loops make the code more readable and reduces the need to map the variable in mind in for loops.
for example we have an array of $userProfiles=[...somedata];
using old school for loop
for($i=0; $i<count($userProfiles); $i++){
print_r($userProfiles[$i]['user_name'];
}
using foreach(or any other equivalent form in your language)
foreach($userProfiles as $userProfile){
print_r($userProfile['user_name']);
}
you might agree or disagree with me but this helped me, and you surely sometimes need the old school for loop or even while loops but it depends on what you are dealing with.
4.Don't make unneeded long descriptive variable name
I did this a lot trying to make good descriptive variable names but I ended up making them long.
like $user_profile_color;
Bad Practice
class UserProfile{
$user_profile_color;
$user_profile_picture;
$user_profile_settings;
}
Good Practice
class UserProfile{
$profile_color;
$profile_picture;
$settings;
}
5.Think before you write
Just jumping on to write is not a wise thing to do, it's good to sit back for a while and think about how to do things, how to structure the function, which conditional to use , which loop to use, these small decisions help a lot.
Conclusion
These where the few things that I learnt and helped me write more readable code. Its not that I have mastered the art of writing clean code but if I look a month back I am surely better at writing programs which are more readable.
I knew most of these points before I ended up writing the code still I ended up writing messy code, but after I had faced how a messy code can make your life a hell, I am trying my best to keep things clean.
I would love to know if you have suggestions on how to write better code.
I hope you have an amazing day.
This content originally appeared on DEV Community and was authored by Vardana Bhanot
Vardana Bhanot | Sciencx (2021-04-11T20:15:46+00:00) My first month as a Developer. Retrieved from https://www.scien.cx/2021/04/11/my-first-month-as-a-developer/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.