JavaScript Clean Code π
Use getters and setters π‘
Bad :
Good :
ββββββ
#JSTips #CleanCode
@ProgrammingTip
Use getters and setters π‘
Bad :
function makeBankAccount() {
return {
balance: 0,
// ...
};
}
const account = makeBankAccount();
account.balance = 100;Good :
function makeBankAccount() {
let balance = 0;
function getBalance() {
return balance;
}
function setBalance(amount) {
balance = amount;
}
return {
getBalance,
setBalance,
};
}
const account = makeBankAccount();
account.setBalance(100);ββββββ
#JSTips #CleanCode
@ProgrammingTip
JavaScript Clean Code π
Async/Await are even cleaner than Promises π‘
Bad :
Good :
ββββββ
#JSTips #CleanCode
@ProgrammingTip
Async/Await are even cleaner than Promises π‘
Bad :
get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
.then((response) => {
return writeFile('article.html', response);
})
.then(() => {
console.log('File written');
})
.catch((err) => {
console.error(err);
});Good :
async function getCleanCodeArticle() {
try {
const response = await get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin');
await writeFile('article.html', response);
console.log('File written');
} catch(err) {
console.error(err);
}
}ββββββ
#JSTips #CleanCode
@ProgrammingTip
JavaScript Clean Code π
Only comment things that have business logic complexity. π‘
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad :
Good :
ββββββ
#JSTips #CleanCode
@ProgrammingTip
Only comment things that have business logic complexity. π‘
Comments are an apology, not a requirement. Good code mostly documents itself.
Bad :
function hashIt(data) {
// The hash
let hash = 0;
// Length of string
const length = data.length;
// Loop through every character in data
for (let i = 0; i < length; i++) {
// Get character code.
const char = data.charCodeAt(i);
// Make the hash
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash &= hash;
}
}Good :
function hashIt(data) {
let hash = 0;
const length = data.length;
for (let i = 0; i < length; i++) {
const char = data.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash &= hash;
}
}ββββββ
#JSTips #CleanCode
@ProgrammingTip
Refactoring β’GURUβ’ πΏ
Refactoring.Guru is a shiny website where you can find tons of information on refactoring, design patterns, SOLID principles and other smart programming topics. β¨
https://refactoring.guru/
#CleanCode #Refactoring
@ProgrammingTip
Refactoring.Guru is a shiny website where you can find tons of information on refactoring, design patterns, SOLID principles and other smart programming topics. β¨
https://refactoring.guru/
#CleanCode #Refactoring
@ProgrammingTip
refactoring.guru
Refactoring and Design Patterns
Refactoring is a controllable process of improving code without creating new functionality. Design Patterns are typical solutions to the commonly occurring problems in software design.
JavaScript Clean Code π
Use consistent capitalization π‘
Bad :
Good :
ββββββ
#JSTips #CleanCode
@ProgrammingTip
Use consistent capitalization π‘
Bad :
const DAYS_IN_WEEK = 7;
const daysInMonth = 30;
const songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
const Artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
function eraseDatabase() {}
function restore_database() {}
class animal {}
class Alpaca {}
Good :
const DAYS_IN_WEEK = 7;
const DAYS_IN_MONTH = 30;
const SONGS = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
const ARTISTS = ['ACDC', 'Led Zeppelin', 'The Beatles'];
function eraseDatabase() {}
function restoreDatabase() {}
class Animal {}
class Alpaca {}
ββββββ
#JSTips #CleanCode
@ProgrammingTip
Try to group extension methods into static classes dealing with the same extended type. π¦
Sometimes related classes (such as DateTime and TimeSpan) can be sensibly grouped together. βοΈ
But avoid grouping extension methods targeting disparate types such as Stream and string within the same class. β
γ°γ°γ°γ°γ°γ°
#CleanCode #ExtensionMethods
@ProgrammingTip
Sometimes related classes (such as DateTime and TimeSpan) can be sensibly grouped together. βοΈ
But avoid grouping extension methods targeting disparate types such as Stream and string within the same class. β
γ°γ°γ°γ°γ°γ°
#CleanCode #ExtensionMethods
@ProgrammingTip
6 Simple Tips on How to Start Writing Clean Code πΎ
1οΈβ£ Make code readable for people.
2οΈβ£ Use meaningful names for variables, functions and methods.
3οΈβ£ Let every function or method perform only one task.
4οΈβ£ Use comments for clarification ( if really needed )
5οΈβ£ Be consistent
6οΈβ£ Review your code regularly
γ°γ°γ°γ°γ°γ°
#CleanCode
@ProgrammingTip
1οΈβ£ Make code readable for people.
2οΈβ£ Use meaningful names for variables, functions and methods.
3οΈβ£ Let every function or method perform only one task.
4οΈβ£ Use comments for clarification ( if really needed )
5οΈβ£ Be consistent
6οΈβ£ Review your code regularly
γ°γ°γ°γ°γ°γ°
#CleanCode
@ProgrammingTip
Yagni : You Arent Gonna Need It πΎ
Never Add Functionality Early π‘
Even if you're totally, totally, totally sure that you'll need a feature later on, don't implement it now. β³
Usually, what you actually need is quite different from what you foresaw needing earlier. π π»ββοΈ
This doesn't mean you should avoid building flexibility into your code. β
It means you shouldn't overengineer something based on what you think you might need later on. β
πΉπΈπΉπΈπΉπΈ
There are two main reasons to practise YagNi :
1οΈβ£ You save time, because you avoid writing code that you turn out not to need.
2οΈβ£ Your code is better, because you avoid polluting it with 'guesses' that turn out to be more or less wrong but stick around anyway.
This also follows the KISS theorem:
https://t.me/pgimg/13
γ°γ°γ°γ°γ°γ°
#CleanCode #Principles #Tips
@ProgrammingTip
Never Add Functionality Early π‘
"Always implement things when you actually need them, never when you just foresee that you need them."
Even if you're totally, totally, totally sure that you'll need a feature later on, don't implement it now. β³
Usually, what you actually need is quite different from what you foresaw needing earlier. π π»ββοΈ
This doesn't mean you should avoid building flexibility into your code. β
It means you shouldn't overengineer something based on what you think you might need later on. β
πΉπΈπΉπΈπΉπΈ
There are two main reasons to practise YagNi :
1οΈβ£ You save time, because you avoid writing code that you turn out not to need.
2οΈβ£ Your code is better, because you avoid polluting it with 'guesses' that turn out to be more or less wrong but stick around anyway.
This also follows the KISS theorem:
Keep it simple, stupid!
https://t.me/pgimg/13
γ°γ°γ°γ°γ°γ°
#CleanCode #Principles #Tips
@ProgrammingTip
Telegram
Programming Tips Resources
What is Unit Testing & Why You Need to Lean It βοΈ
Unit testing is the practice of writing code to test your code and then run those tests in an automated fashion. β
πΉπΈπΉπΈ
Here is an example. π¨π»βπ»
Imagine you have this function somewhere in your code.
Itβs a basic calculate function that takes an input and depending on some conditions, it returns different values.
If you want to test this function manually :
1οΈβ£ You have to run your application
2οΈβ£ Perhaps you have to login
3οΈβ£ Maybe do a few clicks here
4οΈβ£ There to get to a page where this function is used.
5οΈβ£ You have to fill out a form
6οΈβ£ Submit it
7οΈβ£ Verify if this function returned the right result.
And then you have to repeat all these steps, each time using different values in your form. π€¦π»ββοΈ
πΈπΉπΈπΉ
Manual testing is expensive πΈ
As you can see, this is very time-consuming. βοΈ
This workflow to test this function may take several minutes every timeβοΈ
Now to make matters worse, this is not the only function in your application. π
In a real application, you have tens or hundreds of functions like this βοΈ
As your application grows in size and complexity, the time required to manually test all the different bits and pieces increases exponentially. β±
So, thatβs why we use Automated Testing. β»οΈ
πΊπΉπΊπΉ
https://t.me/pgimg/24
[ Full Article ] : http://bit.do/utdd
γ°γ°γ°γ°γ°γ°
#CleanCode #UnitTest #TDD
@ProgrammingTip
Unit testing is the practice of writing code to test your code and then run those tests in an automated fashion. β
πΉπΈπΉπΈ
Here is an example. π¨π»βπ»
Imagine you have this function somewhere in your code.
Itβs a basic calculate function that takes an input and depending on some conditions, it returns different values.
public float CalculateTax(int input)
{
if (x) return ...;
if (y) return ...;
return ...;
}
If you want to test this function manually :
1οΈβ£ You have to run your application
2οΈβ£ Perhaps you have to login
3οΈβ£ Maybe do a few clicks here
4οΈβ£ There to get to a page where this function is used.
5οΈβ£ You have to fill out a form
6οΈβ£ Submit it
7οΈβ£ Verify if this function returned the right result.
And then you have to repeat all these steps, each time using different values in your form. π€¦π»ββοΈ
πΈπΉπΈπΉ
Manual testing is expensive πΈ
As you can see, this is very time-consuming. βοΈ
This workflow to test this function may take several minutes every timeβοΈ
Now to make matters worse, this is not the only function in your application. π
In a real application, you have tens or hundreds of functions like this βοΈ
As your application grows in size and complexity, the time required to manually test all the different bits and pieces increases exponentially. β±
So, thatβs why we use Automated Testing. β»οΈ
πΊπΉπΊπΉ
https://t.me/pgimg/24
[ Full Article ] : http://bit.do/utdd
γ°γ°γ°γ°γ°γ°
#CleanCode #UnitTest #TDD
@ProgrammingTip
Telegram
Programming Tips Resources
The Art of Comments π¨
[ Article ] : https://css-tricks.com/the-art-of-comments/
γ°γ°γ°γ°γ°γ°
#CleanCode #Comments
@ProgrammingTip
[ Article ] : https://css-tricks.com/the-art-of-comments/
γ°γ°γ°γ°γ°γ°
#CleanCode #Comments
@ProgrammingTip
The Pragmatic Programmer.pdf
2.6 MB
The Pragmatic Programmer π
Authors : Andy Hunt, Dave Thomas π
Level : Advanced π
γ°γ°γ°γ°γ°γ°
#Book #CleanCode #Refactoring
@ProgrammingTip
Authors : Andy Hunt, Dave Thomas π
Level : Advanced π
γ°γ°γ°γ°γ°γ°
#Book #CleanCode #Refactoring
@ProgrammingTip
Quick Tip: How to pass visual alerts with an HTMLHelperβ οΈ
It will display a Bootstrap alert if there is a message passed through the ViewModel. π₯
This message can be a success, error, warning, or informational message. π
The nice thing about the ViewMessage HtmlHelper is that if we don't pass it into our views, it won't display anything. π
https://t.me/pgimg/87
[ Article ] : http://bit.do/alhl
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode
@ProgrammingTip
It will display a Bootstrap alert if there is a message passed through the ViewModel. π₯
This message can be a success, error, warning, or informational message. π
The nice thing about the ViewMessage HtmlHelper is that if we don't pass it into our views, it won't display anything. π
https://t.me/pgimg/87
[ Article ] : http://bit.do/alhl
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode
@ProgrammingTip
Telegram
Programming Tips Resources
Automatic ModelState Validation in ASP.NET MVC π₯
How many times have you seen or written code like this β»οΈ :
Stop repeating ModelState checks in your ASP.NET MVC controller actions. β
Wouldnβt it be nice if all we had to do was like this ? β‘οΈ
https://t.me/pgimg/89
[ Article ] : http://bit.do/msta
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode
@ProgrammingTip
How many times have you seen or written code like this β»οΈ :
[HttpPost]
public ActionResult Index(SomeModel model)
{
if (ModelState.IsValid)
{
return View();
}
// do something
return RedirectToAction("index");
}
Stop repeating ModelState checks in your ASP.NET MVC controller actions. β
Wouldnβt it be nice if all we had to do was like this ? β‘οΈ
[HttpPost]
[ValidateModelState]
public ActionResult Index(SomeModel model)
{
// if we get here, ModelState is valid
// save to db etc.
return RedirectToAction("index");
}
https://t.me/pgimg/89
[ Article ] : http://bit.do/msta
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode
@ProgrammingTip
Telegram
Programming Tips Resources
How we do MVC β View models π
The ViewModel is a central aspect of our MVC architecture. π
One of the first dilemmas facing MVC developers is to decide what the βMβ in MVC means in ASP.NET MVC. π€
In Rails, this is fairly clear, the M is ActiveRecord (by default). π
But in ASP.NET MVC, the βMβ is silentβοΈ
Its out-of-the-box architecture offers no guidelines nor advice on what the M should be. βοΈ
Should it be an entityβ
Data access objectβ
DTOβ
Something elseβ
https://t.me/pgimg/91
[ Website ] : http://bit.do/mvvm
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode #BestPractices
@ProgrammingTip
The ViewModel is a central aspect of our MVC architecture. π
One of the first dilemmas facing MVC developers is to decide what the βMβ in MVC means in ASP.NET MVC. π€
In Rails, this is fairly clear, the M is ActiveRecord (by default). π
But in ASP.NET MVC, the βMβ is silentβοΈ
Its out-of-the-box architecture offers no guidelines nor advice on what the M should be. βοΈ
Should it be an entityβ
Data access objectβ
DTOβ
Something elseβ
https://t.me/pgimg/91
[ Website ] : http://bit.do/mvvm
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode #BestPractices
@ProgrammingTip
Telegram
Programming Tips Resources
ASP.NET MVC Best Practices and Guidelines β
While most developers were used to WebForms and Microsoft's stock controls, when developers moved to ASP.NET MVC, a number of developers were shocked because there wasn't enough meat for this to be a full-featured release. π€
Some of the guidelines list below are based on my own experiences over the years since 2008. π
https://t.me/pgimg/93
[ Website ] : http://bit.do/aspbst
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode #BestPractices
@ProgrammingTip
While most developers were used to WebForms and Microsoft's stock controls, when developers moved to ASP.NET MVC, a number of developers were shocked because there wasn't enough meat for this to be a full-featured release. π€
Some of the guidelines list below are based on my own experiences over the years since 2008. π
https://t.me/pgimg/93
[ Website ] : http://bit.do/aspbst
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode #BestPractices
@ProgrammingTip
Telegram
Programming Tips Resources
Architecting Services with Design Patterns π
As the number of your services expands you're going to need to start thinking about how to organize them. π
Applying these two design patterns can help, provided you understand all their variations. β
With the current set of tools that come with the Microsoft .NET Framework, it's easy to create a service. βοΈ
But it's also easy to create what both Nayaki Nayyar and Benjamin Moreland described as a "junk drawer of services." πΎ
If you're going to avoid that as the number of services in your organization increases, then you need to think about your architecture. π€
https://t.me/pgimg/95
[ Article ] : http://bit.do/asdp
γ°γ°γ°γ°γ°γ°
#CleanCode #DesignPatterns
@ProgrammingTip
As the number of your services expands you're going to need to start thinking about how to organize them. π
Applying these two design patterns can help, provided you understand all their variations. β
With the current set of tools that come with the Microsoft .NET Framework, it's easy to create a service. βοΈ
But it's also easy to create what both Nayaki Nayyar and Benjamin Moreland described as a "junk drawer of services." πΎ
If you're going to avoid that as the number of services in your organization increases, then you need to think about your architecture. π€
https://t.me/pgimg/95
[ Article ] : http://bit.do/asdp
γ°γ°γ°γ°γ°γ°
#CleanCode #DesignPatterns
@ProgrammingTip
Telegram
Programming Tips Resources
Use T4MVC to Remove Magic Strings in ASP.NET MVC Apps π©
ASP.NET MVC has a fascination with magic strings. β
Basically any time you need to specify an action, a view, or controller, you use a magic string β¨ :
The problem with magic strings is the same problem that ViewBag has β οΈ :
1οΈβ£ There's no type checking.
2οΈβ£ The developer won't catch these errors until runtime, and sometimes not at all.
πΉπΈπΉπΈ
T4MVC aims to remove magic strings from MVC and replace them with strongly-typed ActionResults. β
It adds a bunch more overloads to methods such as ActionLink(), BeginForm(), and RedirectToAction() so that they can now accept ActionResults as parameters. π
Thereby making them strongly-typed and removing their dependency on magic strings. π
Most importantly, it means we can take the above code samples and refactor them to look like this π€π» :
https://t.me/pgimg/98
[ Article ] : http://bit.do/t4mvc
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode #T4MVC
@ProgrammingTip
ASP.NET MVC has a fascination with magic strings. β
Basically any time you need to specify an action, a view, or controller, you use a magic string β¨ :
return RedirectToAction("Index", "Home", new { id = 4 });
return View("Details");
@Html.ActionLink("Back to details", "Detals", "User", new {id = 12}, new {@class = "backlink"});
@using (Html.BeginForm("Details", "User", FormMethod.Post))The problem with magic strings is the same problem that ViewBag has β οΈ :
1οΈβ£ There's no type checking.
2οΈβ£ The developer won't catch these errors until runtime, and sometimes not at all.
πΉπΈπΉπΈ
T4MVC aims to remove magic strings from MVC and replace them with strongly-typed ActionResults. β
It adds a bunch more overloads to methods such as ActionLink(), BeginForm(), and RedirectToAction() so that they can now accept ActionResults as parameters. π
Thereby making them strongly-typed and removing their dependency on magic strings. π
Most importantly, it means we can take the above code samples and refactor them to look like this π€π» :
return RedirectToAction(MVC.Home.Index(4));
return View(MVC.User.Views.ViewNames.Details);
@Html.ActionLink("Back to details", MVC.User.Details(12), new {@class = "backlink"});
@using (Html.BeginForm(MVC.User.Details(), FormMethod.Post))
https://t.me/pgimg/98
[ Article ] : http://bit.do/t4mvc
γ°γ°γ°γ°γ°γ°
#AspMvc #CleanCode #T4MVC
@ProgrammingTip
Telegram
Programming Tips Resources
The Clean Coder.pdf
2.8 MB
The Clean Coder : A Code of Conduct for Professional Programmersπ
Author : Robert C. Martin π
Level : Advanced π
γ°γ°γ°γ°γ°γ°
#Book #CleanCode
@ProgrammingTip
Author : Robert C. Martin π
Level : Advanced π
γ°γ°γ°γ°γ°γ°
#Book #CleanCode
@ProgrammingTip
Insidious Dependencies π€·π»ββοΈ
In the last year or so Iβve really seen the light on how to really write loosely-coupled code. π
I thought I knew something about this concept before β I mean, I knew loose coupling was good, generally speaking, and I knew data abstraction was one of the key ways to limit dependencies between classes. β
However, I didnβt realize that I was unintentionally adding all kinds of coupling into my applications despite my best efforts to the contrary. βοΈ
Letβs talk about some dependencies, including some obvious ones, as well as some insidious dependencies that lurk in most applications Iβve seen. π
Insidious dependencies β :
β’ File System
β’ Email
β’ Web Service & Requests
β’ DateTime.Now
β’ Configuration
β’ New ...
https://t.me/pgimg/139
[ Article ] : bit.do/indP
γ°γ°γ°γ°γ°γ°
#CleanCode #Dependency
@ProgrammingTip
In the last year or so Iβve really seen the light on how to really write loosely-coupled code. π
I thought I knew something about this concept before β I mean, I knew loose coupling was good, generally speaking, and I knew data abstraction was one of the key ways to limit dependencies between classes. β
However, I didnβt realize that I was unintentionally adding all kinds of coupling into my applications despite my best efforts to the contrary. βοΈ
Letβs talk about some dependencies, including some obvious ones, as well as some insidious dependencies that lurk in most applications Iβve seen. π
Insidious dependencies β :
β’ File System
β’ Email
β’ Web Service & Requests
β’ DateTime.Now
β’ Configuration
β’ New ...
https://t.me/pgimg/139
[ Article ] : bit.do/indP
γ°γ°γ°γ°γ°γ°
#CleanCode #Dependency
@ProgrammingTip
Telegram
Programming Tips Resources
How to Stop Using Callbacks and Start Living βοΈ
Javascript has two major ways of dealing with asynchronous tasks - callbacks and Promises. π€
In general Promises are considered easier to use and to maintain than callbacks. β
But in reality even Promises alone wonβt make you happy. π€¬
Asynchronous code may still be quite difficult to read and to understand. π€―
Therefore third-party libraries, e.g. co, provided means to write a synchronous-like asynchronous code. π
I personally prefer everything in the world to be as clear and beautiful as redux-saga. β¨
But not everybody is lucky to work with React and Redux to be able to use sagas. βοΈ
This article will show that in modern Javascript it is not difficult to write a well structured and easy to understand asynchronous code without using any third-party libraries.π
https://t.me/pgimg/146
[ Article ] : kutt.it/call
γ°γ°γ°γ°γ°γ°
#JavaScript #CleanCode
@ProgrammingTip
Javascript has two major ways of dealing with asynchronous tasks - callbacks and Promises. π€
In general Promises are considered easier to use and to maintain than callbacks. β
But in reality even Promises alone wonβt make you happy. π€¬
Asynchronous code may still be quite difficult to read and to understand. π€―
Therefore third-party libraries, e.g. co, provided means to write a synchronous-like asynchronous code. π
I personally prefer everything in the world to be as clear and beautiful as redux-saga. β¨
But not everybody is lucky to work with React and Redux to be able to use sagas. βοΈ
This article will show that in modern Javascript it is not difficult to write a well structured and easy to understand asynchronous code without using any third-party libraries.π
https://t.me/pgimg/146
[ Article ] : kutt.it/call
γ°γ°γ°γ°γ°γ°
#JavaScript #CleanCode
@ProgrammingTip
Telegram
Programming Tips Resources