← Back to R Course | Chapter 12: Best Practices | Lesson 1 of 6

Clean R Code लिखना

Clean R code neat और पढ़ने में आसान है, एक tidy bedroom की तरह जहाँ आप सब कुछ ढूंढ सकते हैं। यह clear names, sensible spacing, और helpful notes इस्तेमाल करता है।

Clean R Code लिखना

Clean R code consistent, descriptive names (typically snake_case या camelCase) इस्तेमाल करता है, functions को एक task पर focused रखता है, और deeply nested logic से बचता है जहाँ एक helper function ज़्यादा clearly पढ़ा जाए।

Comments को intent explain करना चाहिए, obvious code दोहराना नहीं। tidyverse style guide जैसी एक consistent style follow करना code को दूसरों (और future आप) के लिए पढ़ना आसान बनाता है।

Note: एक naming convention चुनें (snake_case R community की सबसे common choice है) और इसे पूरे project में consistently इस्तेमाल करें।

उदाहरण: Writing Clean R Code

markup
# Clear, descriptive names and a focused function
calculate_average_score <- function(scores) {
  mean(scores, na.rm = TRUE)
}

student_scores <- c(85, 90, NA, 78)
cat("Average:", calculate_average_score(student_scores), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. x1 और df2 जैसे single letters से variables name करना, जब descriptive names दिखाते हैं data का मतलब क्या है।
  2. calculateScore और student_scores जैसी naming styles mix करना, जब एक style इस्तेमाल होनी चाहिए।
  3. बहुत सारी चीज़ें करने वाले लंबे functions लिखना, जब short focused functions test करना आसान होते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.