DEV Community

praveen raghav ns
praveen raghav ns

Posted on • Edited on

Some easy steps to write UNREADABLE Rails code

Below are some of the easy steps we can follow to make the Rails code hard to debug for others.

1. Reduce the number of lines

Lesser the number of lines, the better developer you are.
It will affect readability for others. But, that's their problem!

x = x - 1 and y +=1 and z = true while is_it_even_legal?() if some_random_condition
Enter fullscreen mode Exit fullscreen mode

2. Use define_method and make it hard to debug

This code defines two methods, enable_feature_a and enable_feature_b. This dynamic function definition makes it difficult to debug code and find the impact areas.

SAMPLE_AR = %w(feature_a feature_b)
SAMPLE_AR.each do |item|
    define_method("enable_#{item}") do |params={}|
        # do something
    end
end
Enter fullscreen mode Exit fullscreen mode

So when your colleague misses impact, you can blame them.

3. Calling dynamic functions

Similar to #2, functions can be called dynamically.

item = 'feature_a'
send("enable_#{item}")
Enter fullscreen mode Exit fullscreen mode

4. Access instance variable, that were set in different file

Do not use function params or return values. Always use instance variables across files, so others have no idea where this instance variable was set.

class SomeController
   include SomeModule

   def calculate_something(num)
       double_of_num(num)
       @result_num
   end
end
Enter fullscreen mode Exit fullscreen mode
modules SomeModule
      def double_of_num(num)
          @result_num = num * num
          puts "set instance variable here and use this in a different file, so Others can find it hard to debug"
      end
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)